How to Write If... Else Statements in C++

Open a new project., Make sure the project is empty., Open a source file., Declare all necessary headers and namespaces., Declare integer x., Ask the user to input value 1 for x., Write the if statement., Write the else statement., Execute the...

11 Steps 2 min read Medium

Step-by-Step Guide

  1. Step 1: Open a new project.

    Click New Project, and open a Visual C++ Console Application.
  2. Step 2: Make sure the project is empty.

    You can check this option off before opening the project. , Go to Project, Add New Item, and make sure to select a .cpp source file. , This will allow you to declare integers and Boolean types without receiving syntax errors. , This will be the integer you base your if...else loop on. , You may use a cout statement to let users know which number to input, but it is essential to declare a cin statement, so they can input a value for x. , This is the first part of the if...else loop.

    By writing if(x==1), and then having a cout statement "True" in the body of the if statement, you can ensure that an input of 1 will return the cout statement you have written. , Since "else" is used for all remaining possibilities, it does not require an expression.

    Output the statement "False" in the else body. , To do this, select debug, and click "Start Without Debugging".

    If it asks you to build the program, press Yes. , To do this, enter 1 in the program and press enter.

    You should receive the output "True". , Enter any number other than
    1.

    You should receive the output "False".
  3. Step 3: Open a source file.

  4. Step 4: Declare all necessary headers and namespaces.

  5. Step 5: Declare integer x.

  6. Step 6: Ask the user to input value 1 for x.

  7. Step 7: Write the if statement.

  8. Step 8: Write the else statement.

  9. Step 9: Execute the program.

  10. Step 10: Test your if statement.

  11. Step 11: Test your else statement.

Detailed Guide

Click New Project, and open a Visual C++ Console Application.

You can check this option off before opening the project. , Go to Project, Add New Item, and make sure to select a .cpp source file. , This will allow you to declare integers and Boolean types without receiving syntax errors. , This will be the integer you base your if...else loop on. , You may use a cout statement to let users know which number to input, but it is essential to declare a cin statement, so they can input a value for x. , This is the first part of the if...else loop.

By writing if(x==1), and then having a cout statement "True" in the body of the if statement, you can ensure that an input of 1 will return the cout statement you have written. , Since "else" is used for all remaining possibilities, it does not require an expression.

Output the statement "False" in the else body. , To do this, select debug, and click "Start Without Debugging".

If it asks you to build the program, press Yes. , To do this, enter 1 in the program and press enter.

You should receive the output "True". , Enter any number other than
1.

You should receive the output "False".

About the Author

O

Olivia Simmons

Specializes in breaking down complex creative arts topics into simple steps.

105 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: