Very few programs run from beginning to end without some kind of decisions being taken or branching occurring in C++ language provides a set of keywords, which are used to change the flow of execution in programs.

 

If statements

To execute certain pieces of code in preference to others, an if statement is used. The if keyword is followed by a conditional expression enclosed in parentheses. If this condition is true, then the code immediately following the if is executed; if the condition is false, then execution skips that code.

void ReportIFSmaller(int a, int b)

    {

       if (a < b)

      std::cout << “a is smaller than b”

       // following code will be executed

    }

To control more than one line of code with an if statement, curly brackets are used: the lines of code to be executed on a true condition are enclosed between two curly brackets:

void ReportIfSmaller(int a, int b)

    {

      if (a <b)

          {

           //both of these lines are executed if (a < b)

          std::cout << “a is smaller than b”;

          std::cout << a << “ < “ << b;

          // following code will always be executed

          }

    }

else conditions

The else keyword allows a two-way decision to be made about which code is executed. Now, if the if condition fails, the code following the else keyword is executed instead of that which followed the if .

void ReportiFSmaller(int a, int b)

{

if(a < b)

{           

// only when (a < b) is true

std::cout << “a is smaller than b”;

std::cout << a << “ < “ << b;

}

else

{           

// only when (a < b) is false

std::cout << “a less-than, or equal to, b”;

std::cout << a << “<=” << b;

      }

// following code will always be executed

}

Chaining conditionals

An else statement can contain another if statement. In this way, multiple execution paths can be controlled:

Void ReportIFSmaller(int a, int b) { if(a < b)

{

// only when (a < b) is true

std::cout << “a is smaller than b”;

std::cout << a << “ < “ << b;

}

else

if  (a==b)

{

// only when a is equal to b

std::cout << “a equal to b”;>

std:cout << a << “==” << b;

}

else

{

// only when (a<b) and (a==b) are false

std::cout << “a greater than b”;

std::cout << a << “ > “ << b;

}

// following code will always be executed

}

Switch and case

When a variable must be compared against multiple values, a “switch case” statement can be used. The following example illustrates its structure:

Switch (x)

{

case 0: //code when x ==0 break; case 1: //code when x ==1 break; case 2: //code when x ==2

break;

default: //code for other values of x >

break;

}

The switch keyword is used at the top of the switch case statement, and is followed by the variable to be checked, between parenthesis (x in this example).The body of the switch case statement contains a set of actions, each labelled with the case keyword and a value. The variable in the switch statement is checked against these cases, and execution jumps to the case statement with the correct value.

The default keyword represents a special kind of case. If the switch variable doesn’t match any of the supplied cases, then the code at default is executed. The break keyword is used to exit the switch case statement once the code for the appropriate condition has been executed. Without the breaks, if case 0 was called, then the code for case 0 would be executed, followed by that for a case 1, then case 2 and so on. While the behaviour is a potential cause of bugs, it can be taken advantage of when the same action is to be performed for two or more cases.


Switch (x)

      {

case 0:

case 1:

case 4:

case 7:

            std::cout << “x is 0, 1, 4 or 7”;

            break;

case 2:

            std::cout << “x is  2”;

            break;

case 3:

            std::cout << “x is either 2 or 3”;  

            break;

default:

    break;

}

 There is a special syntax in C++ for sequences such as:

    {

         x = 3;

    }

else

{

x=Square(y);

}

The so-called ternary operator (? :) is used to perform an assignment based on a comparison. The syntax of the ? : operator is as shown blow: >

Comparison ? value if true : value if false       >

Thus, the example above could be rewritten as:

x = (y==4)? 3 : Square(y);

Note: that either of the true or false values may be a function call