|   | 
  | 
 
  | 
|   | 
  | 
C Control Flow
 | 
 
 | 
 | 
The term flow of control refers to the order in which a program's statments are executed. Following are the control flow statements in C language, which can be used to control the flow of a program.  | 
|   | 
  | 
 
  | 
 | 
 | 
-  if-else-if
 
-  ternary
 
-  switch
 
-  while 
 
-  for
 
-  break
 
-  continue
 
 
 | 
|   | 
  | 
 
  | 
|   | 
  | 
if-else-if
 | 
 
 | 
 | 
The if statement has the same function as other languages. It has three basic forms:  | 
|   | 
  | 
 
  | 
 | 
 | 
- if  (expression) { statement }
 
- if  (expression) { statement } else  { statement }
 
- if  (expression) { statement } else if  { statement } else  { statement }
 
 
 | 
|   | 
  | 
 
  | 
|   | 
  | 
ternary
 | 
 
 | 
 | 
The ? (ternary condition) operator is a more efficient form for expressing simple if statements. It has the following form:  | 
|   | 
  | 
 
  | 
 | 
 | 
expression1 ? expression2:  expression3  | 
|   | 
  | 
 
  | 
 | 
 | 
It simply states:   | 
|   | 
  | 
 
  | 
 | 
 | 
if expression1 then expression2 else expression3  | 
|   | 
  | 
 
  | 
|   | 
  | 
switch
 | 
 
 | 
 | 
The C switch is similar to Pascal's case statement and it allows multiple choice of a selection of items at one level of a conditional where it is a far neater way of writing multiple if statements:  | 
|   | 
  | 
 
  | 
 | 
 | 
  1 switch (expression) {
  2  case item1:
  3 		 statement1;
  4 		 break;
  5  case item2:
  6 		 statement2;
  7 		 break;
  8                  .
  9                  .
 10                  .
 11                  .
 12                  .
 13                  .
 14                  .
 15  case itemn:
 16 		 statementn;
 17 		 break;
 18  default:
 19 		 statement;
 20 		 break;
 21 }
You could download file switch.c here
 | 
|   | 
  | 
 
  | 
 | 
 | 
In each case the value of itemi must be a constant, variables are not allowed.  | 
|   | 
  | 
 
  | 
 | 
 | 
The break is needed if you want to terminate the switch after execution of one choice. Otherwise the next case would get evaluated. Note: This is unlike most other languages.  | 
|   | 
  | 
 
  | 
 | 
 | 
We can also have null statements by just including a ; or let the switch statement fall through by omitting any statements (see e.g. below).  | 
|   | 
  | 
 
  | 
 | 
 | 
The default case is optional and catches any other cases.  | 
|   | 
  | 
 
  | 
|   | 
  | 
while
 | 
 
 | 
 | 
The while statement is similar to those used in other languages although more can be done with the expression statement -- a standard feature of C.  | 
|   | 
  | 
 
  | 
 | 
 | 
while(expression)  | 
 | 
 | 
  statement  | 
|   | 
  | 
 
  | 
 | 
 | 
Because the while loop can accept expressions, not just conditions, the following are all legal  | 
|   | 
  | 
 
  | 
 | 
 | 
-  while (x-)
 
-  while (x=x+1)
 
-  while (x+=5)
 
 
 | 
|   | 
  | 
 
  | 
 | 
 | 
Using this type of expression, only when the result of x-, x=x+1, or x+=5, evaluates to 0 will the while condition fail and the loop be exited.   | 
|   | 
  | 
 
  | 
|   | 
  | 
for
 | 
 
 | 
 | 
The C for statement has the following form:  | 
|   | 
  | 
 
  | 
 | 
 | 
for  (expression1; expression2; expression3)  | 
 | 
 | 
  statement;  | 
 | 
 | 
  or {block of statements}  | 
|   | 
  | 
 
  | 
 | 
 | 
expression1 initialises; expression2 is the terminate test; expression3 is the modifier (which may be more than just simple increment);   | 
|   | 
  | 
 
  | 
|   | 
  | 
break
 | 
 
 | 
 | 
We have already met break in the discussion of the switch statement. It is used to exit from a loop or a switch, control passing to the first statement beyond the loop or a switch.  | 
|   | 
  | 
 
  | 
 | 
 | 
With loops, break can be used to force an early exit from the loop, or to implement a loop with a test to exit in the middle of the loop body. A break within a loop should always be protected within an if statement which provides the test to control the exit condition.   | 
|   | 
  | 
 
  | 
|   | 
  | 
continue
 | 
 
 | 
 | 
This is similar to break but is encountered less frequently. It only works within loops where its effect is to force an immediate jump to the loop control statement.  | 
|   | 
  | 
 
  | 
 | 
 | 
-    In a while loop, jump to the test statement.
 
-    In a do while loop, jump to the test statement.
 
-    In a for loop, jump to the test, and perform the iteration. 
 
 
 | 
|   | 
  | 
 
  | 
 | 
 | 
Like a break, continue should be protected by an if statement. You are unlikely to use it very often.  | 
|   | 
  | 
 
  | 
|   | 
  | 
 
  |