Flow Control Statements in C++



Flow Control Statements in C++

Control Flow Statements
When a program runs, the CPU begins execution at main(), executes some number of statements, and then terminates at the end of main(). The sequence of statements that the CPU executes is called the program’s path.  Straight-line programs have sequential flow — that is, they take the same path (execute the same statements) every time they are run (even if the user input changes).
However, often this is not what we desire. For example, if we ask the user to make a selection, and the user enters an invalid choice, ideally we’d like to ask the user to make another choice. This is not possible in a straight-line program.
C++ provides control flow statements (also called flow control statements), which allow the user to change the CPU’s path through the program.
Different C++ flow control Statements
§if statement
§else if construct
§switch statement
§break statement
§while loop
§do while loop
§for loop

if-else statement
if ( condition )
  {
   statement true;
  }
else
  {
   statement false;
  }

if-else statement example
#include <iostream.h>
int main()
{
    int a = -10;
    if ( a > 0 )
    {
     cout << "a is a positive Integer";
    }
   else

    {
     cout << "a is a negative or zero";
    }
return 0;
}
else-if construct
(else-if ladder)
    if ( condition-1 )
    {
      statement; // condition-1 is true
    }
    else if ( condition-2 )
    {
       statement; // condition-2 is true
    }
   else if ( condition-3 )
    {
        statement; // condition-3 is true
    }
   else
    {
        // default case:
        statement; // all above conditions were false
    }
else-if ladder example
#include <iostream.h>
int main()
{
    int a = -10;
    if ( a > 0 )
    {
     cout << "a is a positive integer";
    }
    else if ( a < 0 )
    {
     cout << "a is negative";
    }
    else
    {
    cout << "a is zero";
    }
return 0;
}
Switch statement
The switch statement provides a alternative to the if when dealing with a multi-way branch. Suppose we have some integer value called test and want to do different operations depending on whether it has the value 1, 5 or any other value, then the switch statement could be employed:-
 switch ( test expression ) {
  case 1 : 
    // Statements for test = 1
    ...
    break;
   case 5 : 
    // Statements for test = 5
    ...
    break;
   default : 
    // Statements for all other.
    ...
}
It works as follows
The expression, just test in this case, is evaluated.
The case labels are checked in turn for the one that matches the value.
If none matches, and the optional default label exists, it is selected, otherwise control passes from the switch compound statement
If a matching label is found, execution proceeds from there. Control then passes down through all remaining labels within the switch statement. As this is normally not what is wanted, the break statement is normally added before the next case label to transfer control out of the switch statement.
Switch Statement Example
#include <iostream.h>
int main()
{
    cout << "Do you want to continue (y or n)?\n";
    char ans = 0;
    cin >> ans; // get ans from user
    switch ( ans )
    {
    case 'y':
    cout<<"Your Ans is True";
    break;
    case 'n':
    cout<<"Your Ans is False";
    break;
    default:
    cout << "Wrong Input\n";
  
    }

return 0;
}
Output :if user press 'y' than
it will display "Your Ans is true"
Switch Statement Consideration
1.case constants must be distinct
2.default is optional
3.cases and the default can occur in any order
4.break statement causes an immediate exit from the switch
5.put a break after each case
Nested Switch Statement
It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.
Syntax:
The syntax for a nested switch statement is as follows:
switch(ch1) {
   case 'A': 
      cout << "This A is part of outer switch";
      switch(ch2) {
         case 'A':
            cout << "This A is part of inner switch";
            break;
         case 'B': // ...
      }
      break;
   case 'B': // ...
}
Example of nested switch Statement
#include <iostream.h>

int main ()
{
   // local variable declaration:
   int a = 10;
   int b = 20;
 
   switch(a) {
      case 10: 
        cout << "This is part of outer switch" << endl;
         switch(b) {
           case 20:
        cout << "This is part of inner switch" << endl;
         }
   }
   cout << "Exact value of a is : " << a << endl;
   cout << "Exact value of b is : " << b << endl;
 return 0;
}
Output:
This is part of outer switch
This is part of inner switch
Exact value of a is : 10
Exact value of b is : 20

No comments:

Post a Comment