Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
CPS120 Introduction to Computer Science Iteration (Looping)
-
Upload
baldwin-morgan -
Category
Documents
-
view
221 -
download
0
Transcript of CPS120 Introduction to Computer Science Iteration (Looping)
Looping
Loops allow your program to repeat groups of statements a specified number of times. Loops are an example of an iteration structure.
Iterate
A program loop is a form of iteration. A computer can be instructed to repeat instructions under certain conditions.
No
Looping with Keyword goto
Uses concepts covered in decision making discussion Implemented with a simple if..then structure
loop: counter ++;cout <<"Processing: Counter = "<< counter << endl;if (counter < 5)
goto loop;
Iteration Control Structures
Iteration control structures are looping mechanisms.
Loops repeat an activity until stopped. The location of the stopping mechanism determines how the loop will work: Leading decisions Trailing decisions
Leading Decisions
If the stop is at the beginning of the iteration, then the control is called a leading decision.
The command WHILE performs the iteration and places the stop at the beginning.
‘For’ Loops
A for loop always executes a specific number of iterations.
Use a for loop when you know exactly how many times a set of statements must be repeated A for loop is called a determinant or definite
loop because the programmer knows exactly how many times it will iterate
Syntax of a for Loop
for (initializing expression; control expression; step expression){ // one or more statements}
The initializing expression sets the counter variable for the loop to its initial value.
The control expression ends the loop at the specified moment.
The step expression changes the counter variable Semi-colons, not commas, divide the expressions
Things to Remember About For Loops
It is possible to have variable increase by a value other than one for (num = 1; num <= 10; num = num + 3)
It is possible to initialize the loop variable within the initializing expression. But I recommend against it Declare loop variables at the top of the function where
they belong If an if statement only contains one body statement,
the compiler doesn't require the use of curly braces
Looping Statements
The while statement is used to repeat a course of action
Let’s look at two distinct types of repetitions
Looping Statements
Count-controlled loops Repeat a specified number of times Use of a special variable called a loop control variable
Flow of control of while statement
Looping Statements
Event-controlled loops The number of repetitions is controlled by an event that
occurs within the body of the loop itself
WHILE Loop
NoNo
YesYes
EntryEntry
ExitExit
Test Test condition pcondition p
Loop Loop statement astatement a
While Loops
A while loop does not necessarily iterate a specified number of times As long as its control expression is true, a while loop
will continue to iterate An indeterminate or indefinite loop because only at
run-time can it be determined how many times it will iterate
While is considered a top-checking loop The control expression is located on the first line of
code If the control expression initially evaluates to FALSE,
the loop will not execute even once.
While Loop Syntax
while (control expression){ // one or more statements}
The control expression must evaluate to TRUE in order for the while loop to iterate even once
While (true) Loops
This type of loop will never end unless you put a break in it
while (true) {
counter ++;if (counter > 10)
break;}
Trailing Decisions
If the stop is at the end of the iteration, the control mechanism is called a trailing decision.
The command DO / WHILE performs the iteration and puts the stop at the end of the loop.
DO WHILE Loop
Loop Loop statement astatement a
NoNo YesYes
EntryEntry
Test Test condition pcondition p
ExitExit
Do While Loops
As with a while loop, a do while loop does not necessarily iterate a specified number of times
A do while loop will iterate at least one time because its control expression is placed at the end of the loop Considered to be an indeterminate or indefinite loop Considered to be a bottom-checking loop, since the
control expression is located after the body of the loop
Do While Syntax
do{ // body statements would be placed here}while (control expression);
Don't forget to include the required semicolon after the control expression
Break and Continue Statements
A break statement is used to stop the execution of a loop immediately and to continue by executing the statement that comes directly after the loop
A continue statement is used to stop the execution of the statements in the loop's body on that particular iteration and to continue by starting the next iteration of the loop
Nested Loops
A loop of any kind may be placed in another loop (of any kind).
Be sure to entirely encapsulate the inner loop inside of the outer loop, otherwise an error is sure to occur.
Two loops are considered to be nested loops if one is enclosed within the other
In Summary
Loops goto loops -- simple if…then structure for -- fixed number of loops while -- may never be run
while (true) -- always true, needs break do … while -- always run once
continue causes while, do… while, and for loops to start over
break causes while, do … while, for and switch statements to end