Pages

CONTROL STRUCTURES UNIT 4 PART 2


THE switch STATEMENT
Introduction
You can use a switch statement when you want to check multiple conditions. It can also be done using an if statement but it will be too lengthy and difficult to debug.
Program/Example
The general format for a switch statement is
switch (expressions)
{
case constant expressions
}
Example of a case constant expression and column:
switch (i/10)
{
    case 0: printf ("Number less than 10");    // A
                break;
    case 1: printf ("Number less than 20");     // B
                break;
    case 2: printf ("Number less than 30");    // C
                break;
    default: printf ("Number greater than or equal to 40");    // D
                break; }
Explanation
  1. The switch expression should be an integer expression and, when evaluated, it must have an integer value.
  2. The case constant expression must represent a particular integer value and no two case expressions should have the same value.
  3. The value of the switch expression is compared with the case constant expression in the order specified, that is, from the top down.
  4. The execution begins from the case where the switch expression is matched and it flows downward.
  5. In the absence of a break statement, all statements that are followed by matched cases are executed. So, if you don't include a break statement and the number is 5, then all the statements A, B, C, and D are executed.
  6. If there is no matched case then the default is executed. You can have either zero or one default statement.
  7. In the case of a nested switch statement, the break statements break the inner switch statement.
Point to Remember
The switch statement is preferable to multiple if statements.

THE break STATEMENT

Introduction

Just like the switch statement, break is used to break any type of loop. Breaking a loop means terminating it. A break terminates the loop in which the loop body is written.

Program/Example

For example,
i = 0;
while (1)
{
    i = i + 1;
    printf(" the value of i is %d\n");
    if (i>5) break;
}

Explanation

1.      The while (1) here means the while condition is always true.
2.      When i reaches 6, the if condition becomes true and break is executed, which terminates the loop.

THE continue STATEMENT

Introduction

The break statement breaks the entire loop, but a continue statement breaks the current iteration. After a continue statement, the control returns to top of the loop, that is, to the test conditions. Switch doesn't have a continue statement.

Program/Example

Suppose you want to print numbers 1 to 10 except 4 and 7. You can write:
for(i = 0, i < 11, i++)
{
   if ((i == 4) || (i == 7)) continue;
   printf(" the value of i is %d\n", i);
}

Explanation

1.      If i is 1 then the if condition is not satisfied and continue is not executed. The value of i is printed as 1.
2.      When i is 4 then the if condition is satisfied and continue is executed.
3.      After executing the continue statement, the next statement, (printf), is not executed; instead, the updated part of the for statement (i++) is executed.


THE while LOOP
Introduction
The while loop is used when you want to repeat the execution of a certain statement or a set of statements (compound statement).
Program/Example
The general format for a while loop is
    while (condition)
    simple or compound statement (body of the loop)
    For example,
i = 0;
while (i<5)
{
    printf(" the value of i is %d\n", i);
    i = i + 1;
}
Explanation
  1. Before entering into the loop, the while condition is evaluated. If it is true then only the loop body is executed.
  2. Before making an iteration, the while condition is checked. If it is true then the loop body is executed.
  3. It is the responsibility of the programmer to ensure that the condition is false after certain iterations; otherwise, the loop will make infinite iterations and it will not terminate.
  4. The programmer should be aware of the final value of the looping variable. For example, in this case, the final value of the looping variable is 5.
  5. While writing the loop body, you have to be careful to decide whether the loop variable is updated at the start of the body or at the end of the body.

THE do-while LOOP
Introduction
The do-while loop is used when you want to execute the loop body at least once. The do-while loop executes the loop body and then traces the condition.
Program/Example
The general format for a do-while loop is
do
    simple or compound statement
    while (condition)
    For example,
i = 0;
do
{
    printf(" the value of i is %d\n", i);
    i = i + 1;
}
while (i<5)
Explanation
  1. The loop body is executed at least once.
  2. The condition is checked after executing the loop body once.
  3. If the condition is false then the loop is terminated.
  4. In this example, the last value of i is printed as 5.

THE for LOOP
Introduction
The for loop is used only when the number of iterations is predetermined, for example, 10 iterations or 100 iterations.
Program/Example
The general format for the for loop is
    for  (initializing; continuation condition; update)
    simple or compound statement
    For example,
for (i = 0; i < 5; i++)
{
    printf("value of i");
}
Explanation
  1. The for loop has four components; three are given in parentheses and one in the loop body.
  2. All three components between the parentheses are optional.
  3. The initialization part is executed first and only once.
  4. The condition is evaluated before the loop body is executed. If the condition is false then the loop body is not executed.
  5. The update part is executed only after the loop body is executed and is generally used for updating the loop variables.
  6. The absence of a condition is taken as true.
  7. It is the responsibility of the programmer to make sure the condition is false after certain iterations
THE for LOOP WITH A COMMA OPERATOR
Introduction
You may want to control the loop variables in the same for loop. You can use one for loop with a comma operator in such situations.
Program/Example
for (i = 0, j = 10; i < 3 && j > 8; i++, j-)
printf (" the value of i and j %d %d\n",i, j);
Explanation
  1. First i is initialized to 0, and j is initialized to 10.
  2. The conditions i<3 and j>8 are evaluated and the result is printed only if both conditions are true.
  3. After executing the loop body, i is incremented by 1 and j is decremented by 1.
  4. The comma operator also returns a value. It returns the value of the rightmost operand. The value of (i = 0, j = 10) is 10.