Page 174 - Cs_withBlue_J_C11_Flipbook
P. 174

For example:

                  if(a>b)
                  {
                      if(a>c)
                      {
                          max=a;
                      }
                      else
                      {
                          max=c;
                      }
                  }
                  else
                  {
                      if(b>c)
                      {
                          max=b;
                      }
                      else
                      {
                          max=c;
                      }
                  }
              Here, among the three numbers, the greatest number is to be found. For this, the nested if statement is used. If the
              value of ‘a’ is greater than the value of ‘b’, then the inner code will be checked to find out the greater of a and c and
              the variable ‘max’ is assigned the value accordingly. On the other hand, if the outermost (first) condition which checks
              the greater of a and b is false, then the inner if statement under the else part will be checked and the variable ‘max’
              is assigned the value accordingly.

              We can represent nested conditional statements as ternary operators (already discussed in the previous chapter). The
              above example can be written using Ternary Operator as:

                                                 max= (a>b)? ((a>c)? a: c) : ((b>c)? b : c);

              Switch Case
              Switch Case is a multiple-branch selection statement in which multiple conditions can be checked at once. It uses a
              variable that selects one of many codes (cases) to be executed.
              The value of the variable or the expression is checked with the value of each case. When a match is found, the associated
              block of code is executed. If no case is matched, the code of the default case will be executed which is provided at the
              end of the switch statement.

                      Note:  The data type of the expression or the variable provided in the switch statement must match with
                      the data type of the case value or expression.

              Syntax:

                  switch(choice)
                  {
                      case value1: sequence of statement1;
                          break;
                      case value2: sequence of statement2;
                          break;


                172172  Touchpad Computer Science-XI
   169   170   171   172   173   174   175   176   177   178   179