home
C TUTORIALS

Decision Making Instruction

if-else
switch
the conditional operator

The if Statement

The general form of this statement is:

if(this condition is true) execute this statement;

The condition following the keyword if is always enclosed in pair of parentheses.Here the expression can be any valid expression including a relational expression.We can even use arithmetic expressions in if statement.
For example:

  
if(3+2%5) printf("this works");
There can be two cases:
1. Condition is True:
Then the statement is executed.
2. Condition is Not True:
Then the statement is not executed,instead the programs skips past it.
NOTE :Use parentheses when multiple statement are to be executed.
For example:
if(condition) { statement1; statement2; ... ... }
After the execution of if block the statement next to it is executed i.e. normal execution of program takes place.

The if-else Statement

We use if-else statement when we want to execute one group of statement if the conditions evaluates to true and another group of statement if expression evaluates to false, whereas in case of if,nothing is done when expression evaluates to false.
The general form of this statement is:

if(this condition is true) execute this statement; else execute this statement;
Few points worth noting...
(a)The group of statements after the if upto and not including the else is called an 'if block'.Similarly, the statements after the else form the 'else block'.
(b)else is written exactly below the if.
(c)As with the ifstatement,the default scope of elseis also the statement immediately after the else.To override this default scope,a pair of braces can be used.

Nested if-elses

We can even write an entire if-else construct withinin either the body of if statement or if-else statement.This is called 'nesting' of ifs.
For example:
if(condition 1) { if(condition 2) statement1; else statement2; } else { if(condition 3) statement3; else statement4; }
NOTE: C allows usage of three logical operators, namely, &&,|| and !. These are to be read as 'AND' 'OR' 'NOT' respectively.These can be used to in conditional expression of if statement.

The switch Statement

The switch statement is used where we are required to make a choice between number of alternatives rather than only one or two.
The general form of this statement is:

 
switch(integer expression) { case constant 1: do this; break; case constant 2: do this; break; case constant 3: do this; break; default: do this; } Any number of cases can be used.
The integer expression following the keyword switch is any C expression that will yield an integer value or could be an integer constant like 1,2,or 3.The keyword case is followed by an integer or a character constant.Each constant in each case must be different from all the others.
How execution takes place?
  1. Integer expression following the keyword switch is evaluated.
  2. The value it gives is then matched, one by one, against the constant values that follows case statements.
  3. When a match is found, the program executes the statements following only that case.
  4. NOTE:If break statement is not used after the cases then all the statements after valid case will be executed.
  5. If no match is found with any case statements, only the statements of the default are executed.
Some tips about the usage of switch
(a)You can use cases in any order unlike given in above example.
(b)You are also allowed to use char values in case and switch.
(c)You can also execute a common set of statement for multiple cases.
like
switch(integer expression) { case 1: case 2: statement1; case 3: case 4: statement2; ... ... ... default: statement; }
(d)If we have no default case, then the program simply falls through the entire switch and continues with the next instruction (if any,) that follows the closing brace of switch.
(e)a switch may occur within another, called nested switch statements.

The Conditional Operators

The conditional operators ? and : are sometimes called ternary operators since they takes three arguments.
The general form is,

expression 1? expression 2: expression 3
What this expression says is:"if expression 1 is true(that is,if its value is non zero),then the value returned will be expression 2,otherwise the value returned will be expression 3".