Transcript Loop

Loop
3.1 Introduction to Repetition
Structures
• Loop – a block of code that, under certain conditions will
be executed repeatedly.
Do
Prompt for and input a number, Num
Write num
While Num != 0
Write “Done”
– The body of the loop is executed repeatedly until the
user enters a 0. At that point the loop is exited, and
the statement that follows the loop is executed.
– Note the indentation.
Pre-test and Post-test Loops
 This is a Post-test loop, the condition occurs after the body of
the loop is executed.
Do
Prompt for and input a number, Num
Write num
While Num != 0
Write “Done”
 This is a pre-test loop, the condition appears a the top
Input Num
While Num != 0
Write Num
Input Num
End While
Trace (Walk through) a Loop
• It is impossible to see what a loop is doing
without tracing it to see how it works. Suppose
the user enters 3, 1, -1.
Input Number
While Number > 0
Write Number ^ 2
Input Number
End While
Number
Output
3
9
1
1
-1
Introduction to Loops
• Execute a block of statement(s) repetitiously as long
as the condition is true (non-zero)
a)
b)
counter loop
conditional loop
• It requires 3 steps to design a loop:
1)Initial the condition before the loop
2)Design the condition (Boolean expression) for
the loop
3)Design the body of the loop.
3a)The action you want to repeat
3b)Update the condition
3.2 Counter-controlled Loops
• The body of the loop is executed a fixed
number of times (N times) where N is
known prior to entering the loop for the
first time.
Suppose the user enters 4.
Prompt for an input the positive integer N
N Count
Output
Set Count = 1
While Count <= N
Write Count, Count ^ 2
Set Count = Count + 1
End While
4
1
1
1
2
2
4
3
3
9
4
4
16
Built-in Counter-controlled
Loops
• Most programming languages contain a
statement that makes is easy to construct a
counter-controlled loop.
For Counter = InitialValue Step Increment To LimitValue
Body of loop
End For
Examples:
Count
Output
For Count = 1 Step 1 To 5
1
1
1
2
2
4
3
3
9
4
4
16
5
5
25
N
Output
1
1
3
3
5
5
Write Count, Count ^ 2
End For
For N = 1 Step 2 To 20
Write N
End For
…
17
17
19
19
3.3 Applications of Repetition
Structures
•
•
•
•
Sentinel controlled loops to input data.
A loop will exit when a sentinel value is entered.
A sentinel value is an end-of-data marker
The value chosen for the sentinel should be a
value that could not be mistaken for actual input
data
• If the data entered is known to be positive
integers, the programmer may ask the user to
enter –1 when there is no more data, and the
loop should exit
Sentinel controlled loop example
• Suppose that the input data for a program that computes
employee salaries consists of the number of hours worked and
the rate of pay
Test Data
Write “Enter the hours worked”
Write “Enter –1 when you are done:
Input Hours
While hours != -1
Write “Enter the rate of pay.”
Input Rate
Set Salary = Hours * Rate
Write Hours, Rate, Salary
Write “Enter the hours worked.”
Write “Enter –1 when you are done.”
Input Hours
End While
40
38
-1
10
12
Trace (Walkthrough)
Hours Rate Salary Output
40
10
400
12
456
38
-1
Enter the hours worked
Enter –1 when you are done.
Enter the rate of pay
40 10 400
Enter the hours worked
Enter –1 when you are done.
Enter the rate of pay
38 12 456
Enter the hours worked
Enter –1 when you are done.
Data Validation
• Users may enter erroneous data by mistake
• Programs should include statements that check,
or validate that the value is in a proper range,
and request the user to re-enter invalid data.
Write “Enter a positive number ->”
Input Num
While Num <= 0
Write”The number entered must be positive”
Write “Enter a positive number ->”
Input Num
End While
… the program continues with a valid number in the variable Num
Demonstrate a counter loop
1)initial Num to 1
Star
t
2)While Num is less or equal to 10
2a)Write Num and Num*Num
initialize the
Num to 1
2b)increment Num by 1
End while
No
Num <= 10
Yes
T
Output Num and
Num*Num
Increment Num by 1
Stop
C++ Coding using while loop
// This program displays the numbers 1 through 10 and
// their squares.
#include <iostream>
using namespace std;
int main()
{
int Num = 1;
cout << "Number Number Squared\n";
cout << "-------------------------\n";
while (Num <= 10)
{
cout << Num << "\t\t" << (Num * Num) << endl;
Num++;
}
return 0;
}
1
2
3a
3b
C++ Coding using For loop
// This program displays the numbers 1 through 10 and
// their squares.
#include <iostream>
using namespace std;
int main()
{
int Num;
cout << "Number Number Squared\n";
cout << "-------------------------\n";
for (Num = 1; Num <= 10; Num++)
{
cout << Num << "\t\t" << (Num * Num) << endl;
}
return 0;
}
Demonstrate a counter loop
1)initial Num to 1
Star
t
2)Do
2a)Write Num and Num*Num
2b)increment Num by 1
initialize the
Num to 1
While Num is less or equal to 10
Output Num and
Num*Num T
Increment Num by 1
Num <= 10
No
Stop
Yes
C++ Coding using D0 While loop
// This program displays the numbers 1 through 10 and
// their squares.
#include <iostream>
using namespace std;
int main()
{
int Num = 1;
cout << "Number Number Squared\n";
cout << "-------------------------\n";
do
{
cout << Num << "\t\t" << (Num * Num) << endl;
Num++;
}
while (Num <= 10)
return 0;
}
Program Output
Number
Number Squared
------------------------1
1
2
4
3
9
4
16
5
25
6
36
7
49
8
64
9
81
10
100
• In C++, there are feature that interrupt the
flow of the loop
break
The break statement causes a loop to terminate early.
Break out from the loop immediately
continue
The continue statement causes a loop to stop its current iteration and
begin the next one.
// break.cpp
#include <iostream>
using namespace std;
int main()
{
int i;
for(i = 1; i <= 10; i++)
{
if (i == 5)
break;
cout << i << endl;
}
return 0;
}
Output
1
2
3
4
// continue.cpp
Output
#include <iostream>
using namespace std;
1
int main()
{
int i;
for(i = 1; i <= 10; i++)
{
if (i == 5)
continue;
cout << i << endl;
}
return 0;
}
3
2
4
6
7
8
9
10
3.4 Nested Loops
• A loop may be contained entirely within
another loop.
• Outer loop. Inner loop.
For OutCount = 1 Step 1 To 2
For InCount = 1 Step 1 to 3
Write “Outer:”, OutCount, “Inner:”, InCount
End For (Incount)
Trace the Nested Loop
Write
Outer:1 Inner:1
End For (OutCount)
Outer:1 Inner:2
Outer:1 Inner:3
Outer:2 Inner:1
Outer:2 Inner:2
Outer:2 Inner:3
Nesting other Kinds of Loops
• Any style of loop may be nested. Each
nested loop must be indented to indicate
which statements are controlled by which
looping construct.
Do
Other code here
While <condition>
More code here
End While
While <condition>
// nestloop.cpp
BEGIN
Outer loop: i = 1
Inner loop: j = 1
#include <iostream>
using namespace std;
Inner loop: j = 2
Inner loop: j = 3
Inner loop: j = 4
Outer loop: i = 2
Inner loop: j = 1
Inner loop: j = 2
Inner loop: j = 3
Inner loop: j = 4
Outer loop: i = 3
Inner loop: j = 1
Inner loop: j = 2
Inner loop: j = 3
Inner loop: j = 4
END
int main()
{
int i,j;
cout << "BEGIN\n";
for(i = 1; i <= 3; i++)
{
cout << " Outer loop: i = " << i << endl;
for(j = 1; j <= 4; j++)
cout << " Inner loop: j = " << j << endl;
}
cout << "END\n";
return 0;
}
Pseudocode Language (Ch 3)
In this chapter we added relational operators and
repetition.
Input
Input Variable
Assignment
Set Variable = 10
Output
Write “literal text”, Variable
Arithmetic Operations
() ^ */+-
Relational Operators
= <> < > >= <=
Create a module Call a sub-module
ModuleName
….
Repetition
End
While condition
Repeat
End While
Until condition
For Counter = InitialValue Step Increment To LimitValue
End For
…
Call ModuleName