Title: switch and do while
1Lecture 9
- switch and do while
- Reference Section 4.7, 4.8
2Review of C Structure
- All programs could be written in terms of THREE
control structures - Sequence structures Built into C. Programs are
executed sequentially by default. - Selection structures C has three types if,
ifelse, and switch - Repetition structures C has three types while,
dowhile and for
3switch Multiple-Selection Statement
- switch
- Useful when a variable or expression is tested
for all the values it can assume, and different
actions are taken - This is called multiple selection
- Format of switch statement
- Series of case labels and an optional default
case - It tests whether an expression matches one of a
number of constant integer values. If a case
matches, execution starts at that case.
- Remarks
- Each case is labeled by one or more
integer-valued constants or constant expressions - All case expressions must be different
- The case labeled default is executed if none of
the other cases are satisfied - Cases and the default clause can occur in any
order
switch ( expression ) case const-expr
statements case const-expr
statements default statements
4Example
int choice printf(1 Beijing\n) printf(2
Shanghai\n) printf(3 Guangzhou\n) printf(E
nter your choice of destinations
\n) scanf(d, choice) switch( choice )
case 1 printf(The flight to Beijing
takes 3 hours.\n) break case 2
printf(The flight to Shanghai takes 2
hours.\n) break case 3
printf(The flight to Guangzhou takes 1
hour.\n) break default
printf(Wrong Destination!\n) break
The break statement breaks out of the switch
construct. E.g., if the user inputs 2, the switch
finds a match with case 2. Hence the execution
starts from the statement after label case 2.
Once the break statement has been executed, the
whole switch is finished.
5Example
int choice printf(1 Beijing\n) printf(2
Shanghai\n) printf(3 Guangzhou\n) printf(E
nter your choice of destinations
\n) scanf(d, choice) switch( choice )
case 1 printf(The flight to Beijing
takes 3 hours.\n) break case 2
printf(The flight to Shanghai takes 2
hours.\n) / break / case 3
printf(The flight to Guangzhou takes 1
hour.\n) break default
printf(Wrong Destination!\n) break
If the break in case 2 is removed ./a.out 1 -
Beijing 2 - Shanghai 3 - Guangzhou Enter your
choice of destinations 2 The flight to Shanghai
takes 2 hours. The flight to Guangzhou takes 1
hour.
6Character Type
- char data type
- Used to store a single character (slide 20 of
Lecture 3) - In C, the size of char is one byte, which is
enough for ASCII code - Character constant
- Enclosed by single quotation marks , such as
A, b, , \t, 1 - String constant
- Enclosed by double quotation marks , such as
Hello, OK, COMP1000, A
char ch1, ch2, ch3, ch4, ch5 ch1 a / lower
case letter / ch2 1 / digit / ch3 \n
/ escape sequence / scanf(c, ch4) ch5
getchar() printf(c, ch5)
- Remarks A is different from A
- A is a single character
- A is a string, which happened to have only one
character
char ch A
7(No Transcript)
8(No Transcript)
9(No Transcript)
10(No Transcript)
11getchar() function
- getchar() is a C library function defined in
stdio.h - It is used to read one character from the
keyboard - Once your program is running getchar() function,
and if the user doesnt input anything, your
program will stop at the getchar() statement - In C, characters can be stored in any integer
data type - printf( The character (c) has the value d.\n,
a, a) - The result is
- The character (a) has the value 97.
- 97 is the character as numerical representation
in the computer. - ASCII code (Slides 24, 25 of Lecture 3)
12Assignments have a value!
- while ( ( grade getchar() ) ! EOF )
- grade getchar() is an assignment
- getchar() will get a character from the user, and
assign this character to variable grade - The whole assignment ( grade getchar() ) has a
value that is equal to the character returned by
getchar() - The condition in while loop in fact compares the
value of grade to EOF - EOF is a symbol standing for End of File, used as
a sentinel - It is an integer constant defined in stdio.h
- define EOF (-1)
- On Linux/UNIX system, the EOF indicator is
entered by typing - d
- On Microsoft Windows, the EOF indicator is enter
by typing - z
13break in switch ()
Controlling Expression
- switch ( grade )
- case A
- case a
- aCount
- break
- case B
-
- default
-
-
Falling through cases if there is no break
statement
The break statement causes program control to
continue with the first statement after the
switch statement.
If no match occurs, the default case is executed.
14switch multiple-selection statement with breaks.
15Good Programming Practices
- Provide a default case in switch statements.
Cases not explicitly tested in a switch are
ignored. The default case helps prevent this by
focusing the programmer on the need to process
exceptional conditions. - Although the case clauses and the default case
clause in a switch statement can occur in any
order, it is considered good programming practice
to place the default clause last.
16Common Programming Error
- Not processing newline characters in the input
when reading characters one at a time can cause
logic errors. - Remember to provide processing capabilities for
newline (and possibly other white-space)
characters in the input when processing
characters one at a time.
17dowhile Repetition Statement
- The dowhile repetition statement
- Similar to the while structure
- Condition for repetition only tested after the
body of the loop is performed - All actions are performed at least once
- Format
- do
- statements
- while ( condition )
18dowhile Repetition Statement
- Example (letting counter 1)
- do
- printf( "d ", counter )
- while (counter
- Prints the integers from 1 to 10
- Some programmers always include braces in a
do...while statement even if the braces are not
necessary. This helps eliminate ambiguity between
the do...while statement containing one statement
and the while statement.
19(No Transcript)
20Flowcharting of the do...while repetition
statement
21Comparison of while and dowhile
Get input from user until y or n is input
/ method 2
using do while
/ char ch do
printf(Input y or n ) scanf(c, ch)
while (ch ! y ch ! n)
/ method 1 using
while / char
ch printf(Input y or n ) scanf(c,
ch) while (ch ! y ch ! n)
printf(Input y or n ) scanf(c, ch)
22Another Example of dowhile
- Problem Check for a palindrome. E.g., 12321,
1342431 are palindromes. - Solution reverse the integer and then compare!
int num, n, digit, rev0 scanf(d, num) n
num / make a copy of num / do digit n
10 rev rev 10 digit n / 10
while (n ! 0) if (num rev) printf(d is
a palindrom.\n, num) else printf(d is not
a palindrom.\n, num)
23Common Programming Error
- Infinite loops are caused when the
loop-continuation condition in a while, for or
do...while statement never becomes false. - To prevent this
- make sure there is not a semicolon immediately
after the header of a while or for statement. - In a counter-controlled loop, make sure the
control variable is incremented (or decremented)
in the loop. - In a sentinel-controlled loop, make sure the
sentinel value is eventually input.
24Appendix C Keywords
- auto break case char
- const continue default do
- double else enum extern
- float for goto if
- int long register return
- short signed sizeof static
- struct switch typedef union
- unsigned void volatile while