Flow of Control

Statements

If-Else

if (expression)

statement or block #1

else

statement or block #2

  • The else is optional.
  • If the expression is true, a non-zero value, statement or block #1 is executed. If it is false, or zero, statement or block #2 is executed.
  • If-Else statements can be linked together providing a multi-way decision. The syntax is:
  • if (expression)

    statement or block #1

    else if (expression)

    statement or block #2

    else

    statement or block #3

    Switch

    switch (c) {

    case ‘a’:

    case ‘e’:

    case ‘i’:

    case ‘o’:

    case ‘u’:

    vowel++;

    break;

    default:

    other++;

    }

    Loops

  • C defiines three different loops: for loops. while loops and do-while loops.
  • The syntax of a while loop is:
  • while (expression)

    statement or block

    for (expression #1; expression #2; expression #3)

    statement or block

    expression #1;

    while (expression #2) {

    statement or block

    expression #3;

    }

    long factorial(int n)

    {

    int i;

    long fact = 1;;

    for (i=n; i> 0; i--)

    fact = fact * i;

    return fact;

    }

    do

    statement or block

    while (expression);