Mythili Vutukuru
IIT Bombay
Reference: “C How to Program”, Deitel and Deitel, 8th Edition, Chapter 4
We have seen how to select from two different blocks of
code based on checking a condition using if-else
statements
We have seen two patterns of iterations or looping in
program using while
Counter controlled or definite iteration: number of times the loop is
to be run is known. Counter variable incremented in each iteration,
loop exits when desired value reached
Sentinel controlled or indefinite iteration: number of times to run
loop is not known. Loop ends when a special input is received.
In this lecture: more keywords for structured
programming
For loop: a better way to do counter controlled iteration
Do-while loop: a different way of using while statement
Break and continue: ways to change execution of loops
Switch statement: select from multiple blocks of code by
checking multiple conditions
Counter controlled
iteration:
A loop counter variable
Initial value of counter
Increment or decrement
operation by which
counter is modified in
each iteration of loop
Condition to check how
long the loop should run
and when it should
terminate Note %u format string for unsigned int
For statement: better way to do counter controlled iteration
Careful about “off-by-one” errors: be sure loop runs for desired times only
Counter variable can be declared earlier also, before for statement
Can do increment or decrement even by more than 1
If condition false initially itself, loop will never run
Data type double (more precision and
greater magnitudes than float)
Include math header for math
functions like exponentiation (pow)
Link to math library using –lm
option
Note how output is formatted by
providing field width and precision
Positive width = left aligned
Negative width = right aligned
Execute body of loop before checking condition
Loop executes at least once
Used to
immediately break
out of body of loop
(for, while, do-
while) or switch
Program execution
continues at
statement after
loop or switch
Used to skip current
iteration of a loop
What runs after
continue?
While and do-while: loop
continuation test and
next iteration (if allowed)
For loop: increment
expression, loop
continuation test, next
iteration (if allowed)
Read character from stdin and
assign to grade
Check for End of File (EOF)
Ctrl+d in Linux, Ctrl+Z on Windows
Match against possible values
of grade, update count
Break after checking each case
(else all following cases will match)
Ignore spaces, tabs, newlines
Default case if
nothing else matches
End of file (EOF) differs across systems
Works only for constant
integral expressions, i.e.,
combinations of integers or
characters which evaluate
to some integer value
Seven types of statements in C programs
Sequence statements
Condition statements
Single selection statement: if
Double selection statement: if-else
Multiple selection statement: switch
Iteration statements (sentinel controlled vs. counter controlled)
Check condition and repeat: While
Controlled iteration: For loop
Do iteration and check condition: do-while
Real life programs composed of combining multiple such
statements