07 - Branching and Loop (Part 2)
07 - Branching and Loop (Part 2)
Pre-test loop
• Two forms of loop:
– Pre-test loop
– Post-test loop
Condition
• Pre-test loop Here must be a
Post-test loop
• Post-test loop
– The condition is tested later,
after executing the body. True Body
– If the condition is true, the
loop repeats, otherwise it
terminates.
– The body is always executed at Condition
least once.
The iterating part
must be a “True”
False
Parts of a Loop
• Beside the body and condition, a loop may have two other
parts: initialization and updating.
Pre-test loop Post-test loop
Initialization Initialization
Body
Condition
True
True
Updating
Body
False
Condition
Updating
False
Parts of a Loop
• Initialization Post-test loop
– Is used to prepare a loop before
it can start usually, here we Initialization
• Updating
– Is used to update the condition. Condition
Print
is n>0? n
True
True
n = n -1
Print
n
False
n = n -1
Every time the loop is n>0?
repeats, n is updated
False
Loop Statements
while flowchart
while(condition)
{
Repeated_actions; Condition
}
True
Repeated_Actions False
while Statement
False
n = n-1;
} n = n -1
Output:
10 9 8 7 6 5 4 3 2 1
for Statement
for flowchart
for(Initialization;
Condition; Updating) Initialization
{
Repeated_actions; Condition
} True
Repeated_Actions
False
Updating
for Statement
Print
n
Output: False
n = n -1
10 9 8 7 6 5 4 3 2 1
for vs. while Statements
do..while flowchart
do
{
Repeated_actions;
True Repeated_Actions
} while(condition);
Condition
False
do..while Statement
{
n = n -1
cout << n << “ ”;
n = n-1;
} while (n > 0);
is n>0?
Output:
False
10 9 8 7 6 5 4 3 2 1
Loop Statements
• If the body part has only one statement, then the
bracket symbols, { } may be omitted.
• Example:
These two for statements are equivalent.
True
“True”
}
Repeated_Actions False
Translating Flowchart to C++ Code
• Example of pattern 1: Calculate the average of odd numbers 1 to 9.
sum = 0
i =1 sum = 0;
i = 1;
i < 11 while (i < 11)
True
{
sum = sum + i sum = sum + i;
i = i+2
False
i = i + 2;
}
avrg = sum / 5.0;
av rg = sum /5
Translating Flowchart to C++ Code
• Pattern 2
do
True Repeated_Actions
{
Repeated_actions;
} while(condition);
condition
False
Translating Flowchart to C++ Code
• Example of pattern 2: Prints numbers 1 to 10.
i =1 i = 1;
do
PRINT
{
True
i
cout << i << endl;
i = i +1
i = i + 1;
} while (i < 11);
i <11
False
Translating Flowchart to C++ Code
• Pattern 3 for(initialize;
condition; update)
initialize
{
Repeated_Actions;
}
condition
True
initialize;
while(condition)
Repeated_Actions
False
{
update Repeated_Actions;
update;
}
Translating Flowchart to C++ Code
• Example of pattern 3: Print the total of numbers 1 to 10.
total = 0
total = 0;
for (i = 1; i < 11; i++)
i=1
{
total = total + i;
}
cout << total;
i<11
True total = 0;
total = total + i
i = 1;
False
while (i < 11)
i=i+1 {
total = total + i;
i++;
PRINT }
total
cout << total;
Start
Exercise 4 count = 0
positive= 0
• The following flowchart negative = 0
presents an algorithm of a
False
program that counts the count < 30
Display
End
positive,
number of positive and negative
True
negative numbers entered by
the user. The user will enter Input
number
30 numbers. Translate the
flowchart into its
corresponding C++ program True
number > 0 positive = positive + 1
using an appropriate loop
statement. False
True
negative = negative + 1
number < 0
False
count = count + 1
Exercise 4 (cont.)
• Write your code in the incomplete program given below:
#include <iostream>
using namespace std;
int main()
{
int count = 0; //The total count of numbers entered
int positive = 0; //The count of positive numbers
int negative = 0; //The count of negative numbers
int number; //The number entered