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.