Title: Lecture 11:Control Structures II Repetitioncont.
1Lecture 11Control Structures II
(Repetition)(cont.)
- Introduction to Computer Science
- Spring 2006
2(No Transcript)
3The while Loop
- The general form of the while statement is
- while(expression)
-
- statement1
- statement2
-
-
4The for Loop
- The general form of the for statement is
- for(initial statement loop condition update
statement) - statement
- for(initial statement loop condition update
statement) - statement1
- statement2
-
-
- The for loop executes as follows
- initial statement executes (initial statement
initializes a variable) - loop condition is evaluated
- If loop condition evaluates to true
- Execute for loop statement
- Execute update statement
- Repeat previous step until the loop condition
evaluates to false
5Example
include using namespace std int
main() int i for (i0 ii) cout
6(No Transcript)
7The for Loop (continued)
- initial statement in the for loop is the first to
be executed and is executed only once - If the loop condition is initially false, the
loop body does not execute - The update expression changes the value of the
loop control variable which eventually sets the
value of the loop condition to false - The for loop executes indefinitely if the loop
condition is always true - A semicolon at the end of the for statement is a
semantic error - In this case, the action of the for loop is empty
- If the loop condition is omitted
- It is assumed to be true
8The for Loop (continued)
- In a for statement, all three statements (initial
statement, loop condition, and update statement)
can be omitted - The following is a legal for loop
- for()
- cout
9The dowhile Loop
- The general form of a do...while statement is
- The procedure is
- statement executes first, and then the expression
is evaluated - If the expression evaluates to true, the
statement executes again - As long as the expression in a do...while
statement is true, the statement executes
do statement1 statement2
While(expression)
do statement While(expression)
10(No Transcript)
11Example for dowhile loop
include using namespace std int
main() int i0 do cout" ii5 while (irn(0)
12Break Continue Statements
- break and continue alter the flow of control
- break statement
- syntax break
- The break statement is used for two purposes
- In a repetition structure (e.g. while, for, and
dowhile loop), it immediately exits from a loop - In a switchcase structure, skip the remainder of
the switch structure (an immediate exit) - After the break statement executes, the program
continues with the first statement after the
structure - The use of a break statement in a loop can
eliminate the use of certain (flag) variables
13Example for break statement
include using namespace std int
main() int num, sum 0 cin
num while(cin) if (numgative number found!"umnum cinnum cout"
14Break Continue Statements (continued)
- continue statement
- Syntax continue
- used in a loop (while, for, and do-while
structures) - Skips remaining statements and proceeds with the
next iteration of the loop - In a while and do-while structure
- Expression (loop-continue test) is evaluated
immediately after the continue statement - In a for structure, the update statement is
executed after the continue statement - Then the loop condition executes
15Example for continue statement
include using namespace std int
main() int num, sum 0 cin
num while(cin) if (numgative number found!"msumnum cinnum cout"
16Nested Control Structures
- Suppose we want to create the following pattern
-
-
-
-
-
- In the first line, we want to print one star, in
the second line two stars and so on - Since five lines are to be printed, we start with
the following for statement - for(i 1 i
- The value of i in the first iteration is 1, in
the second iteration it is 2, and so on
17Nested Control Structures (continued)
- The syntax is
- for(i 1 i
-
- for(j 1 j
- cout
- cout
-
18Nested Control Structures (continued)
- What pattern does the code produce if we replace
the first for statement with the following? - for (i 5 i 1 i--)
- Our program becomes
-
for(i 1 i j) cout
19 20End of lecture 11