if ... else if .... else statement

Purpose : Enable your program to execute one out of two or more blocks of statements depending on certain condition(s).

Example :

   if ( s > 100 )
   {
      cout << "Your time outside a prison is probably limited.You are way too fast. \n" ;
   }
   else if ( s > 55 )
   {
      cout << "You are driving above the speed limit\n" ;
   }
   else
   }
      cout << "You are saving gas. Congrats.\n" ;
   }

Depending on the value of the variable "s" one or the other of the three cout-statements will be executed. Here "s > 100" and "s > 55" are simple forms of a logical expression which can take on meriads of different forms. Once the program arrives at the if-statement the logical expression, "s > 100", will be evaluated. If it comes out to be "true" all the statements inside the first pair of curly brackets will be executed and the program proceeds with the statements below the entire "if-else if-else" block.
If it evaluates to "false", the logical expression "s > 55", will be tested. If that one evaluates to "true" all statements inside the "else if" block will be executed and the program proceeds with the statements below the entire "if-else if-else" block.
If "s > 55" evaluates to false the stements inside the "else" block will be executed and the program proceeds with the statements below the entire "if-else if-else" block.

Some rules you must know :

  1. There are no restrictions on the number and type of statements ( including other if-else statements ) inside either the if-block nor the "else if" nor the "else" block.
  2. If the logical of your program demands no statements inside the else-block the "else" and its curly brackets can be omitted.
  3. The number of "else -if" blocks is un-restricted and even can be entirely omitted if the logic of your program demands that.
  4. The types of logical expressions used for the "if" statement and the "else if" statements (if present) can be of widely different forms without any restrictions other than those which govern the forms of logical expressions.
Last revised: 08/23/13