CSC 126 CH4
CSC 126 CH4
STRUCTURE
CSC126|Fundamental of Algorithms & Computer Problem Solving
TABLE OF CONTENT
• Introduction
• Requirement of repetition structure
• while statement
• Basic iterative problems
• for statement
• Nested loops
INTRODUCTION
INTRODUCTION
Solution ?
display “The total is“, average
End
counter = 0 number = 2
number != 999
REQUIREMENT OF REPETITION
STRUCTURE: UPDATE
number = number – 2
REQUIREMENT OF REPETITION
STRUCTURE
Begin
counter = 1
evaluate LCV
Depend on type of loop Repeat (counter <= 5)
structure use ( while, output “I love C++”
do … while or counter ++
for) update LCV
End
Flow Chart
Loop body
Start
Initialize Counter = 1
LCV
Counter T
<= 5
F I love C++
Evaluate
LCV (loop condition) Counter ++
End Update
LCV
REPETITION STRUCTURES
The while and do … while loop can be used to implement both type of
repetition control structure.
COUNTER CONTROL LOOP
FOR LOOP
INTRODUCTION
• The initial statement, loop condition, and update statement are called for loop
control statements
• Items in square brackets ([ ]) are optional.
INTRODUCTION
Result:
1
2
3
FOR LOOP – EXAMPLE 1
Start
For( set i to 1; i less than or equal to 3; add 1 to i)
display “welcome to C++”
Endfor
End
FOR LOOP – EXAMPLE 1
Start
• Flowchart
i=1
F
i <= 3 End
Display “welcome to
C++”
i ++
for LOOP – EXAMPLE 2
• Exercise 1: create a program that display the first 10 positive odd integers.
for LOOP – EXERCISES
• Exercise 1 - answer
for LOOP – EXERCISES
• Answer:
for LOOP – EXERCISES
• Answer:
for LOOP
A semicolon at the end of the for statement (just before the body of the
loop) is a semantic error. In this case, the action of the for loop is empty.
for (;;)
cout << "Hello" << endl;
• List of iterative problems that can be solved using for loop structures:
✓ Calculate total/ sum
✓ Calculate average
✓ Find maximum or minimum value
✓ count
B ASIC ALGORITHM USING for LOOP
STRUCTURE
total = 0;
*Accumulator used to calculate total
for(i = 1; i <= 5; i++){
cin>>number;
total = total + number;}
cout<<“ The total is :”<<total;
for LOOP – EXERCISES 1
( CALCU LAT E TOTAL)
❖ Exercise 4: Suppose j, sum, and num are int variables, and the input values are 26, 34, 61,
4, and -1. What is the output of the code below? Produce tracing table.
❖ Exercise 4: answer
for LOOP – EXERCISES 2
( CALCU LAT E TOTAL)
MAR 2016
B ASIC ALGORITHM USING for LOOP
STRUCTURE
• Calculate average:
Task:To calculate average of 5 numbers
total = 0;
average = 0;
cin>>number;
max = number; max = -1;
cin>>number;
min = number; min = 1000;
MAR 2017
B ASIC ALGORITHM USING REPETITION
STRUCTURE
• Count
Task: user requires to input 5 numbers. Calculate and
display how many even numbers in a group of 5 numbers entered.
condition
NO
YES
Statement(s) to repeat
• General syntax:
• Loopcount
body executes how many times?
count < 3 Output
(count = count + 1)
0 0 < 3 (T) Hi
1 1 < 3 (T) Hi So, loop body
executes 3 times
2 2 < 3 (T) Hi
3 3 < 3 (F) exit
EXERCISES
MAR 2017
B ASIC ITERATIVE PROBLEMS
total = 0;
i = 1;
while(i <= 5){ *Accumulator
cin>>number;
total = total + number;
i++;
}
cout<<“ The total is :”<<total;
T 6 25=19+6 5
T 8 33=25+8 6
F - - - 33
SUMMATION - EXERCISE
APR 2009
AVERAGE
• Calculate average:
Task:To calculate average of 5 numbers
total = 0;
average = 0;
i = 0;
while(i < 5){
cin>>number;
total = total + number;
i++;
}
average = total/i;
cout<<“ The average is :”<<average;
AVERAGE
average = total/(i-1);
cout<<“ The average is :”<<average;
APR 2009
MAXIMUM
Sample data: 25 4 6 30 10
max = -1;
Produce tracing table for the above sample data
i=1
while( i <= 5)
{
cin>>number;
if(number>max)
max = number;
i++;
}
cout<<“ The maximum value is :”<<max;
MINIMUM
OCT 2012
COUNTING
• Count
Task: user requires to input 5 numbers. Calculate and
display how many even numbers in a group of 5 numbers entered.
*counter used to count amount of even number
countE = 0;
i=1
while( i <= 5)
{
cin>>number;
if(number%2==0)
countE= countE + 1;
i++;
}
cout<<“ The amount of even numbers are :”<<countE;
COUNTING - EXERCISE
• Write a complete C++ program to allow user to input 10 numbers using while
loop. Count and display how many numbers that can be divisible by 3 and by 5.
DO … WHILE (COUNTER
CONTROL)
DO … WHILE CONTROL STRUCTURES
• The 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.
• To avoid an infinite loop, you must, once again, make sure that the loop body contains a statement that
ultimately makes the expression false and assures that it exits properly.
DO … WHILE CONTROL STRUCTURES
• Example:
DO … WHILE CONTROL STRUCTURES
Summation
TO BE CONTINUED…