4.4. Conditional expression 2: If-Else

The control-flow statements  of a language specify the order in which computations are performed and are therefore extreme necessary to master.

An expression becomes a statement when it is followed by a semicolon, as in x = 0; i++; printf(…);

In C the semicolon is a statement terminator.

Braces { and } are used to group declarations together into a compound statement or block. To avoid any bug in the code, brackets should therefore always use with loop and decision making. With nested ifs and loops it is necessary and compulsory.

The syntax of if-else statement is

if (expression)
statement1
else
statement2

where else is optional. In if statement there may be more conditions than only one.
Example:

if (n>0){
  if (a>b)
    z=a;
  else 
    z=b;

Above else goes with the inner if ( n must be bigger than zero and a must be smaller than b). Shorter way:

 
if (n>0 && a>b){
    z=a;
  else
    z=b;

What happens if n is negative?

 
if (n>0 || a>b){
    z=a;
  else
    z=b;

Is it now ok?
Example how braces are used to force the proper association:

if (n>0){
  if (a>b)
    z=a;
}
  else 
    z=b;

NOTE! Exercises for that chapter  can be found in quizzes Exercises 4 and P4-4 where  these skills will also be tested. It is also important that the student does these examples above.