CS151 -- Administration
Midterm -- Reminder July 2 -- Thru Ch. 7
-
Practice by doing problems at the end of chapters...self-test
exercises at the end of each section.
Project 2 due, this week... June 28
CS151 -- Shawn Stoffer
Looping
-
while(expression)
statement
CS151 -- Shawn Stoffer
Looping
-
while(expression)
statement
-
Reading the statement:
while expression is true, do statement
CS151 -- Shawn Stoffer
Looping
-
while(expression)
statement
-
Breaking apart the loop:
test (expression == true) ->
execute statement -> go back to test.
CS151 -- Shawn Stoffer
Looping
-
do statement while(expression);
CS151 -- Shawn Stoffer
Looping
-
do statement while(expression);
-
Reading the statement:
do statement until expression is false
CS151 -- Shawn Stoffer
Looping
-
do statement while(expression);
-
Breaking apart the loop:
execute statement ->
test (expression == true) ->
go back to execute statement
CS151 -- Shawn Stoffer
Looping
What's the difference?
-
while(expression)
statement
-
do statement while(expression);
CS151 -- Shawn Stoffer
Looping
What's the difference?
-
while(expression)
statement
-
do statement while(expression);
CS151 -- Shawn Stoffer
Looping
-
for(initialization; expression; update)
statement
CS151 -- Shawn Stoffer
Looping
-
for(initialization; expression; update)
statement
-
Initialization: Any statement...
-
used to initialize variables used in the loop.
CS151 -- Shawn Stoffer
Looping
-
for(initialization; expression; update)
statement
-
expression is the same as for a while loop.
CS151 -- Shawn Stoffer
Looping
-
for(initialization; expression; update)
statement
-
statement is executed next!
CS151 -- Shawn Stoffer
Looping
-
for(initialization; expression; update)
statement
-
update is used to update variables used in the loop.
-
executed last, right before test
CS151 -- Shawn Stoffer
Looping
-
for(initialization; expression; update)
statement
execute initialization ->
test (expression == true) ->
execute statement -> execute update -> go back to test
(expression == true)
CS151 -- Shawn Stoffer
Looping
CS151 -- Shawn Stoffer
Looping
Writing loops as different types...
-
for(initialization; expression; update)
statement
-
initialization;
while (expression) {
statement;
update;
}
CS151 -- Shawn Stoffer
Looping
Writing loops as different types...
-
Why one type of loop over another?
-
while loops
-
do...while loops
-
for loops
CS151 -- Shawn Stoffer
Looping
Writing loops as different types...
-
Why one type of loop over another?
-
for loops
-
used for situations where you update something, and then
check the condition. (incrementing....)
CS151 -- Shawn Stoffer
Looping
Writing loops as different types...
-
Why one type of loop over another?
-
do...while loops
-
When you have to do something before you check the condition
the first time (such as getting a password, where you have to get the password
the first time, before you check whether the password is correct...)
CS151 -- Shawn Stoffer
Looping
Writing loops as different types...
-
Why one type of loop over another?
-
while loops
-
catchall for the rest... whenever a for loop is not natural,
but do...while is not necessary.
CS151 -- Shawn Stoffer
Looping
Writing loops as different types...
-
Why be able to write loops as different types of loops?
-
Sometimes you may have to change what type of loop is there
because it is no longer natural... instead of using arrays (a natural for
loop situation), you begin using linked lists, or files...
CS151 -- Shawn Stoffer
Looping
Directing flow of control
CS151 -- Shawn Stoffer
Looping
Directing flow of control
-
break
-
gets out of the current loop immediately!
-
NOTHING else done before loop exit... like a 'mini'-return.
-
only exits the current loop it is in... see nesting.
CS151 -- Shawn Stoffer
Looping
Directing flow of control
-
continue
-
go to the test statement, skip the rest of the loop.
-
sometimes you don't want to execute the entire loop...
CS151 -- Shawn Stoffer
Looping
Directing flow of control
-
nesting, and goto
-
sometimes, you have to have loops within loops
-
break only gets out of ONE loop (block).
-
goto goes directly to label.
-
goto label;
CS151 -- Shawn Stoffer
Looping
Directing flow of control
-
nesting, and goto
-
while (fin >> ch) {
for (int i = 0; i < 5; i++) {
if (ch == '\n') break;
if (ch == ' ') continue;
if (ch == EOF) goto abnormalTermination;
ch += 15;
}
}
abnormalTermination:
return 0;
CS151 -- Shawn Stoffer