Decision Statements


  • C++ programs are a collection of functions each having a declaration part and a computational part which are executed sequentially, that is statement by statement.
  • In practice, it becomes necessary to make some changes in the order of execution and you need the in-built decision making mechanism. This is accomplished by a set of control constructs. In accordance with the decision, control will be transferred to another part of the computational structure, inside the function.
  • This decision may be, 1. One way branching. 2. Two way branching.


    One way branching.

  • One way branching means evaluating a condition and then branching. In accordance with the test advocated, set of statements are executed. This type of decision making technique takes the form:
        If (test expression)
        {
        statement-block
        }
        statement-n;
    
  • This syntax is for a simple if statement. That is, the decision is made based on a condition,
  • Braces { and } are used to group declarations and statement together into a compound statement. This set of compound statement together enclosed within parenthesis is called a block.


    One way Branching

  • he statement block can be a single statement or group of statements. If the logical expression is true then statement block is executed otherwise the statement block will be skipped and the statement-n will be executed. When the condition is TRUE that is when logical expression evaluates to true both the statement and the statement-n are executed.
  • The test expression is made up of relational and logical operator.
    example
    # include < iostream.h > 
    void main()
    {
    int n1 ,n2;
    cout << "Enter 2 random numbers \t"; 
    cin >> n1							 
    cin >> n2;
    if (n1 == n2)
    cout << "Numbers are equal" << endl;
    }
    

    Two way branching

  • Two way branching is used in situations, wherein you need to trace two mutually exclusive sets of actions. This is accomplished by if ..then ..else construct of C++, which takes the form:
        If (test expression)
          {
          statement(s) for the condition being true
          }
         else
         {
         statement(s) for the condition being false
         }
          statement-n;
    

    Two way branching


  • If the test expression is true then the block of statement, immediately following the if statement is executed; otherwise the second block of statement is executed.
  • Note that, in either case, either the true-block of statements or the false-block of the statement alone is executed and not both.
    Example
           void main()
            {
           int a, b;
          cout << Enter Two Number; 
          cin >> a >> b;
         if(a==b)
          {
           cout << "Both number is equal";
          }
         else
          {
           cout << "Both number is not equal";
          }
        }
    

    Nested if-then-else

  • The else clause, like the if clause, may contain a compound statement. This is a more flexible control construct. Further, a clause of if statement may itself contain another if statement. This is known as nested if statement.
  • The following example illustrates the nested if construct.
        If (x == 3)
          {
           if (x> 5)
            cout << "The numbers lies between 3 and 5" << endl;
           }
         else
            cout << "the number does not lie between 3 and 5" << endl;
    

    Multi-way branching

  • The multiway branching can be achieved by a nested if-then-else clause.
  • Thereby any number of mutually exclusive statement blocks can be accommodated.
    Example:
       if (marks> 80)
         grade = 'A';
          else if (marks > 60)
           grade = 'B';
            else if (marks> 50)
             grade = 'C';
             else
              grade = 'F';
    


C++ operator << Previous

Next >> Switch-case

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus
Footer1