Selection Statements

Image result for c++ logo

The selection statements allow to choose the set of instruction for execution depending upon a condition or expression's truth value.

C++ provides two types of selection statements : if and switch. The selection statements are also called conditional statements or decision statements.

Remember - if, else, else if, switch, case, break all of them are keywords, so they cannot be used as identifiers.

1. The simple if statement
-> An if statement tests a particular condition, if the condition evaluates to true, a statement or set-of-statements is executed.
-> syntax :
                 if (condition)
                 {  statements ;  }   and if there is only one statement for execution inside the if or else or else if statement then its optional to use curly braces ( {} ) but its mandatory when there is more than one statement.
-> example :   if (grade=='A')
                       { cout << "outstanding" ; }

2. The if-else statement
-> If the condition inside an if statement is true, set of statements associated with if will execute and if it is false, set of statements associated with else will execute and never both.
-> Always remember the else statement never test a condition or expression.
-> syntax :
                if (condition)
                {  statements ; }
                else
                { statements  ; }
-> example :      if (marks >33)
                           cout << "pass";
                          else
                             cout << "fail ";

3. The if-else if ladder
-> If the condition inside an if statement is true, set of statements associated with if will execute and if it is false, set of statements associated with else-if will execute based on a test condition and if non of the test condition evaluates to true then the statement associated with else will execute.
-> syntax :
                 if (condition)
                {  statements ; }
                    else if (condition)
                    { statements  ; }
                        else
                            { statements ; }
-> example :     
                         if ( marks>80 && marks<=100)
                           cout << "grade A " ;
                         else if (marks >=50  && marks<=80)
                           cout << "grade B " ;
                         else
                          cout <<"grade C " ;         

4. Nested if
-> Occurrence of an if inside another if is known as nested if. if can occur either in an if block or an else block or in both if and else blocks.
-> This type of nested statement is least common and used in very less programs, so just take a look on there syntax.

a) if occurring in if block
            if (condition)
              {
                 if (condition)
                    { statements ; }
                  else
                     { statements ; }
               }
                  else
                   { statements ; }

b) if occurring in else block
               if (condition)
               { statements ; )
                else
                 { 
                   if (condition)
                    { statements ;}
                 else
                   { statements ; }
                  }

c) if occurring in both
               if (condition)
                {
                   if (condition)
                  { statements ;}
                   else
                    { statements ; }
                 }
             else
              {
                  if (condition)
                 { statements ; }
                  else
                  { statements ; }
               }

5. The switch statement
-> This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed.
-> syntax :
              switch (expression)
              {
                   case constant1 :      statement ;
                                                      break;
                   case constant2 :      statement ;
                                                      break;
                   case constant3 :      statement ;
                                                      break;
                   case constant n-1 :  statement ;
                                                      break;
                   default             :       statement ;
               }
-> example :
              switch (num)
              {
                 case 1 : cout << " monday ";
                              break;
                 case 2 : cout << " tuesday ";
                              break;
                 case 3 : cout << " wednesday ";
                              break;
                 default : cout << " no day ";
              }
               
-> The expression is evaluated and its values are matched against the values of the constants specified in the case statements. When a match is found, the statements sequence associated with the case is executed until the break statements or the end of the switch statement is reached.
-> Its necessary to have break statement in each case statement, otherwise each statement present in each case statement will execute until a break is encountered or end of switch is reached.
-> example : program without a break statement


output -  as aspected


-> The default statement gets executed when no match is found. The default statement is optional and, if it is missing, no action takes place if all matches fail.
-> example : when no match is found or when user enter an invalid option
    
output - 






                 
     

  

Comments

Popular Posts