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
- The switch expression should be an
integer expression and, when evaluated, it must have an integer value.
- The case constant expression
must represent a particular integer value and no two case expressions
should have the same value.
- The value of the switch expression is compared with
the case constant expression in the order specified, that is, from the top
down.
- The execution begins from
the case where the switch expression is matched and
it flows downward.
- 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.
- If there is no matched case
then the default is executed. You can have either zero or one default
statement.
- 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 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
- Before entering into the
loop, the while condition is evaluated. If
it is true then only the loop body is executed.
- Before making an iteration,
the while condition is checked. If it
is true then the loop body is executed.
- 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.
- 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.
- 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 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
- The loop body is executed at
least once.
- The condition is checked
after executing the loop body once.
- If the condition is false
then the loop is terminated.
- In this example, the last
value of i is printed as 5.
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
- The for loop has four components;
three are given in parentheses and one in the loop body.
- All three components between
the parentheses are optional.
- The initialization part is
executed first and only once.
- The condition is evaluated
before the loop body is executed. If the condition is false then the loop
body is not executed.
- The update part is executed
only after the loop body is executed and is generally used for updating
the loop variables.
- The absence of a condition
is taken as true.
- It is the responsibility of
the programmer to make sure the condition is false after certain
iterations
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
- First i is initialized to 0, and j is initialized to 10.
- The conditions i<3 and j>8 are evaluated and the
result is printed only if both conditions are true.
- After executing the loop
body, i is incremented by 1 and j is decremented by 1.
- The comma operator also
returns a value. It returns the value of the rightmost operand. The value
of (i =
0, j = 10) is
10.