C++ The if statement and switch statement



In this C++ programming tutorial we take a look at the “if statement” and “switch statement”. Both are used to alter the flow of a program (if the specified test condition is true).

The if statement

The if statement can be used to test conditions so that we can alter the flow of a program. In other words: if a specific statement is true, execute some instructions. If not true, execute these instructions.

In the following example the user can input a number. The number is stored in the variable A. Now take a look at the “if statement”: if the number stored in the variable A is equal to ten, then “is equal” is printed on the screen. Simple, isn’t it. If the number is not equal to ten, then nothing is printed.

Take a look at the example:
#include<iostream>
using namespace std;
int main()
{
int A;
cin >> A;
if ( A == 10 )
cout << "is equal" << '\n';
return 0;
}

Now take another look at the “if statement”: look at the placement of the semi-colon. As you can see there is no semi-colon behind the “if statement”. If there is just one instruction (if the statement is true), you can place it after the “if statement” (with an indentation). Are multiple instructions necessary then you will have to use curly brackets, like so:
if ( A == 10 )
{
cout << "Hello" << '\n';
cout << "Hello" << '\n';
}

Now we like to also print something if the “if statement” is not equal. We could do this by adding another “if statement” but there is an easier / better way. Which is using the so called “else statement” with the “if statement”.
#include<iostream>
using namespace std;
int main()
{
int A;
cin >> A;
if ( A == 10 )
{
cout << "is equal" << '\n';
cout << "closing program" << '\n';
}
else
{
cout << "not equal" << '\n';
cout << "closing program" << '\n';
}
return 0;
}

Note: Take a look at the placement of the curly brackets and how the indentations are placed. This is all done to make reading easier and to make less mistakes in large programs.

Nested if statements

If you use an “if statement” in an “if statement” it is called nesting. Nesting “if statements” can make a program very complex, but sometimes there is no other way. So use it wisely. Take a look at a nested “if statement” example below:
#include<iostream>
using namespace std;
int main()
{
int a;
cin >> a;
if ( a <= 10 )
{
cout << "Below 10" << '\n';
}
else
{
if ( a < 60 )
{
cout << "Below 60" << '\n';
}
}
return 0;
}

Note: Again take a look at the placing of the curly brackets and the placing of the indentations.

So let’s walk through the example above: If the input is ten or below ten, “below 10″ will be printed. If the input is above ten, the program will go into the “else statement”. In the “else statement” there is another “if statement” (this is nesting). This “if statement” checks the input again. If the input is below sixty, “below 60″ will be printed.

Multiple condition testing

It is possible to test two or more conditions at once in an “if statement” with the use of the AND (&&) operator. Example:


if ( a > 10 && b > 20 &&  c < 10 )

If a is greater then ten and b is greater then twenty and c is smaller then ten, do something. So all three conditions must be true, before something happens. With the OR ( || ) operator you can test if one of two conditions are true.

Example:

      if ( a == 10 || b < 20 )

If a equals ten or b is smaller then twenty then do something. So if a or b is true, something happens.

The switch statement

The switch statement is almost the same as an “if statement”. The switch statement can have many conditions. You start the switch statement with a condition. If one of the variables equals the condition, the instructions are executed. It is also possible to add a default. If none of the variables equals the condition the default will be executed. See the example below:
#include<iostream>
using namespace std;
int main()
{
char myinput;
cin >> myinput;
switch (myinput)
{
case 'a':
cout << "Run program 1\n";
break;
case 'b':
{
cout << "Run program 2\n";
cout << "Please Wait\n";
break;
}
default:
cout << "Invalid choice\n";
break;
}
return 0;
}

Note: break is used to exit the switch. See the next tutorial for more details.

That’s all for this tutorial.

Source : http://www.codingunit.com/cplusplus-tutorial-the-if-statement-and-switch-statement