Lecture #13: Statements 4
November
1, 1999
Bob
Pitha
review
Weve
talked about:
expression
statements
any valid
expression with a semicolon stuck on the end
compound
statements
one or more
statements (and optional declarations) surrounded by braces
selection
statements
if-else
switch
iteration
statements
while
for
one more iteration statement: do-while
do statement while ( expression
);
A
lot like the while
statement
except the
expression is tested after statement, not before
the
difference is that statement is always executed at least once, even if expression
is never true
with the while statement, the body would
not be executed even once in that case
do-while example

jump statements: break and continue
break
break
exits a loop or a switch statement immediately
but only
from the innermost loop if its a nested loop
This
example code repeatedly reads a number from the keyboard and prints its square
root. If the number is negative, it stops.

break example

continue
continue
ends the current iteration of a loop
the control
expression is evauated
depending on
the value, the next iteration starts
Unlike
break, continue doesnt
end the loop, it just skips to the end of the current cycle
its not
used a whole lot, but sometimes it comes in handy
goto
Theres another jump statement, called goto
it is almost
universally deplored, and rarely necessary
we will not
use it in this class