quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif 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.

   

space.gif

  • if-else-if
  • ternary
  • switch
  • while
  • for
  • break
  • continue
   

space.gif

  ../images/main/bulllet_4dots_orange.gif if-else-if

The if statement has the same function as other languages. It has three basic forms:

   

space.gif

  • if (expression) { statement }
  • if (expression) { statement } else { statement }
  • if (expression) { statement } else if { statement } else { statement }
   

space.gif

  ../images/main/bulllet_4dots_orange.gif ternary

The ? (ternary condition) operator is a more efficient form for expressing simple if statements. It has the following form:

   

space.gif

expression1 ? expression2: expression3

   

space.gif

It simply states:

   

space.gif

if expression1 then expression2 else expression3

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif 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:

   

space.gif


  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
   

space.gif

In each case the value of itemi must be a constant, variables are not allowed.

   

space.gif

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.

   

space.gif

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).

   

space.gif

The default case is optional and catches any other cases.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif 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.

   

space.gif

while(expression)

statement

   

space.gif

Because the while loop can accept expressions, not just conditions, the following are all legal

   

space.gif

  • while (x-)
  • while (x=x+1)
  • while (x+=5)
   

space.gif

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.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif for

The C for statement has the following form:

   

space.gif

for (expression1; expression2; expression3)

statement;

or {block of statements}

   

space.gif

expression1 initialises; expression2 is the terminate test; expression3 is the modifier (which may be more than just simple increment);

   

space.gif

  ../images/main/bulllet_4dots_orange.gif 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.

   

space.gif

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.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif 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.

   

space.gif

  • 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.
   

space.gif

Like a break, continue should be protected by an if statement. You are unlikely to use it very often.

   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com