The do-while Loop, The switch Statement


Lecture 7

The do-while Loop

The do-while loop looks like-
There is a minor difference between the working of while and do-while loops. This difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. In contrast, the do-while tests the condition after having executed the statements within the loop. do-while would execute its statements at least once, even if the condition fails for the first time. The while, on the other hand will not execute its statements if the condition fails for the first time.
Here, since the condition fails the first time itself, the printf( ) will not get executed at all. Let's now write the same program using a do-while loop.
In this program the printf( ) would be executed once, since first the body of the loop is executed and then the condition is tested.
break and continue are used with do-while just as they would be in a while or a for loop. A break takes you out of the do-while bypassing the conditional test. A continue sends you straight to the test at the end of the loop.

The switch Statement

In real life, we often have to make decisions from number of alternatives- which country to visit, which hotel to stay, whom to marry! In C, similar conditions may appear where we will have to find out the decision from number of alternatives. C provides a special control statement that allows us to handle such cases successfully rather than using a series of if statements.

The control statement that allows us to make a decision from the number of choices is called a switch. The general form of switch statement is-
The integer expression following the keyword switch is any C expression that will yield an integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer. The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others. The “do this” lines in the above form of switch represent any valid C statement.

What happens when we run a program containing a switch?
  1. The integer expression following the keyword switch is evaluated.
  2. The value it gives is then matched, one by one, against the constant values that follow the case statements.
  3. When a match is found, the program executes the statements following that case, and all subsequent case and default statements as well.
  4. If no match is found with any of the case statements, only the statements following the default are executed.

The output of this program would be:
I am in case 2
I am in case 3
I am in default
We didn’t expect the second and third line in the above output. The program prints case 2 and 3 and the default case. Well, yes. We said the switch executes the case where a match is found and all the subsequent cases and the default as well. If you want that only case 2 should get executed, it is up to you to get out of the switch then and there by using a break statement. Note that there is no need for a break statement after the default, since the control comes out of the switch anyway.
The output of this program would be:
I am in case 2

Notes and Observations for switch Statement

The earlier program that used switch may give you the wrong impression that you can use only cases arranged in ascending order, 1, 2, 3 and default. You can in fact put the cases in any order you please.
The output of this program would be:
I am in case 22

You are also allowed to use char values in case and switch as shown in the following program:







The output of this program would be:
I am in case x

In fact here when we use ‘v’, ‘a’, ‘x’ they are actually replaced by the ASCII values (118, 97, 120) of these character constants.

At times we may want to execute a common set of statements for multiple cases.
Even if there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces.

Every statement in a switch must belong to some case or the other. If a statement doesn’t belong to any case the compiler won’t report an error. However, the statement would never get executed. For example, in the following program the printf( ) never goes to work.
 
If we have no default case, then the program simply falls through the entire switch and continues with the next instruction (if any,) that follows the closing brace of switch.

The disadvantage of switch is that one cannot have a case in a switch which looks like:
case i <= 20 :
All that we can have after the case is an int constant or a char constant or an expression that evaluates to one of these constants. Even a float is not allowed.
The advantage of switch over if is that it leads to a more structured program and the level of indentation is manageable, more so if there are multiple statements within each case of a switch.
We can check the value of any expression in a switch. Thus the following switch statements are legal.
switch ( i + j * k )
switch ( 23 + 45 % 4 * k )
switch ( a < 4 && b > 7 )
Expressions can also be used in cases provided they are constant expressions. Thus case 3 + 7 is correct, however, case a + b is incorrect.
          The break statement when used in a switch takes the control outside the switch. However, use of continue will not take the control to the beginning of switch as one is likely to believe.

In principle, a switch may occur within another, but in practice it is rarely done. Such statements would be called nested switch statements.

The switch statement is very useful while writing menu driven programs. 

1 comments:

Unknown said...

This is great blog! Keep it up. aircon price

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by CelebrityDisk | Written by Alamin - link | Grants For Single Moms