The break Statement
You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed.
The program in Listing 10.5 does a similar job as the one in Listing 10.4, but this time, the break statement is used.
TYPE
Listing 10.5. Adding the break statement.
1: /* 10L05.c Adding the break statement */
2: #include <stdio.h>
3:
4: main()
5: {
6: int day;
7:
8: printf("Please enter a single digit for a day\n");
9: printf("(within the range of 1 to 7):\n");
10: day = getchar();
11: switch (day){
12: case `1':
13: printf("Day 1 is Sunday.\n");
14: break;
15: case `2':
16: printf("Day 2 is Monday.\n");
17: break;
18: case `3':
19: printf("Day 3 is Tuesday.\n");
20: break;
21: case `4':
22: printf("Day 4 is Wednesday.\n");
23: break;
24: case `5':
25: printf("Day 5 is Thursday.\n");
26: break;
27: case `6':
28: printf("Day 6 is Friday.\n");
29: break;
30: case `7':
31: printf("Day 7 is Saturday.\n");
32: break;
33: default:
34: printf("The digit is not within the range of 1 to 7.\n");
35: break;
36: }
37: return 0;
38: }
OUTPUT
With the help from the break statement, I can run the executable file 10L05.exe and only obtain the output of the selected case:
C:\app>10L05
Please enter a single digit for a day
(within the range of 1 to 7):
1
Day 1 is Sunday.
C:\app>
ANALYSIS
This program has seven case labels followed by the constant expressions of `1', `2', `3', `4', `5', `6', and `7', respectively. (See lines 12, 15, 18, 21, 24, 27, and 30.)
In each case, there is a statement followed by a break statement. As mentioned, the break statements help to exit the switch construct after the statement in a selected case is executed.
For example, after the int variable day is assigned the value of 1 and evaluated in the switch statement, the case with `1' is selected, and the statement in line 13 is executed. Then, the break statement in line 14 is executed, which breaks the control of the switch statement and returns the control to the next statement outside the switch construct. In Listing 10.5, the next statement is the return statement in line 37, which ends the main function.
The printf() function in line 13 outputs a string of Day 1 is Sunday. on the screen.
Note that in a switch statement, braces are not needed to group the statements within an individual case into a statement block.
Breaking an Infinite Loop
You can also use the break statement to break an infinite loop. As you saw in Hour 7, the following for and while loops are all infinite loops:
for (;;){
statement1;
statement2;
.
.
.
}
while (1){
statement1;
statement2;
.
.
.
}
The program in Listing 10.6 shows an example of using the break statement in an infinite while loop.
TYPE
Listing 10.6. Breaking an infinite loop.
1: /* 10L06.c: Breaking an infinite loop */
2: #include <stdio.h>
3:
4: main()
5: {
6: int c;
7:
8: printf("Enter a character:\n(enter x to exit)\n");
9: while (1) {
10: c = getc(stdin);
11: if (c == `x')
12: break;
13: }
14: printf("Break the infinite while loop. Bye!\n");
15: return 0;
16: }
OUTPUT
The following is the result I got after running the executable file (10L06.exe) on my machine:
C:\app>10L06
Enter a character:
(enter x to exit)
H
I
x
Break the infinite while loop. Bye!
C:\app>
ANALYSIS
There is an infinite while loop in Listing 10.6, which starts in line 9 and ends in line 13. Within the infinite loop, the characters the user entered are assigned, one at a time, to the integer variable c. (See line 10.)
The relational expression c == `x' in the if statement (see line 11) is evaluated each time during the looping. If the expression returns 0 (that is, the user does not enter the letter x), the looping continues. Otherwise, the break statement in line 12 is executed, which causes the computer to jump out of the infinite loop and start executing the next statement, which is shown in line 14.
You can see in the sample output, the while loop continues until I have entered the letter x, which causes the infinite loop to be broken and a piece of message, Break the infinite while loop. Bye!, to be displayed on the screen.
- 15 views