Chapter 4 (2) - Repetition Control Structure
Chapter 4 (2) - Repetition Control Structure
Prepared for:
CSC 402 – Programming 1
INTRODUCTION
CONTROL STRUCTURES
REPETITION STRUCTURE
WHY LOOPING?
WHY LOOPING?
REPETITION STRUCTURE
COUNTER-CONTROLLED LOOP
©Rose Hafsah Ab Rauf – UiTM Shah Alam
while LOOP
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
QUICK EXERCISE
What will be the output displayed on the screen for the program below?
What will be the output displayed on the screen for the program below?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
QUICK EXERCISE
QUICK EXERCISE
Correct the following code so that it reads and finds the sum of 30 numbers
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
QUICK EXERCISE
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
for LOOP
© Najwa Abd Ghafar – UiTM Johor
QUICK EXERCISE
Transform the program above using a for loop instead of while loop
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
QUICK TIP!
©Rose Hafsah Ab Rauf – UiTM Shah Alam
© Najwa Abd Ghafar – UiTM Johor
QUICK TIP!
©Rose Hafsah Ab Rauf – UiTM Shah Alam
© Najwa Abd Ghafar – UiTM Johor
SENTINEL-CONTROLLED LOOP
©Rose Hafsah Ab Rauf – UiTM Shah Alam
EXAMPLE
#include <iostream>
using namespace std;
int main(){
int bil = 0, num;
char ans='n';
while(ans!='Y')
{
bil++;
cout<<"Enter an integer number"<<endl;
cin>>num;
cout<<"To exit enter 'Y'"<<endl;
cin>>ans;
}
cout<< "Number of integers entered is "<<bil<<endl;
return 0;
}
}
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
EXAMPLE
##include <iostream>
#include <cmath>
using namespace std;
int main(){
double power;
cout<<"Enter a floating point number to compute the power of 2."<<endl;
cout<<"Enter 0 to stop the program"<<endl;
cin>>power;
while(power)
{
cout<<"The "<<power<<"th power of 2 is :"<<pow(2,power)<<endl;
cout<<"Enter a floating point number to compute the power of 2."<<endl;
cout<<"Enter 0 to stop the program"<<endl;
cin>>power;
}
return 0;
}
© Najwa Abd Ghafar – UiTM Johor
- 1;
break STATEMENT
break Statement:
Causes an immediate exit from the enclosing loop
QUICK EXERCISE
What will be the output displayed on the screen for the program above?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
continue STATEMENT
continue Statement:
Causes the remaining statements in the current loop to be skipped
Go to next loop
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
What will be the output displayed on the screen for the program above?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
What is wrong with this source code? How can you fix this problem.
© Najwa Abd Ghafar – UiTM Johor
FLAG-CONTROLLED LOOP
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
NESTED LOOP
Nested Loop:
Is used when you want to execute a loop within another loop
An inner loop will be contained inside an outer loop
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
What will be the output displayed on the screen for the program above?
© Najwa Abd Ghafar – UiTM Johor
QUICK EXERCISE
©Rose Hafsah Ab Rauf – UiTM Shah Alam
What will be the output displayed on the screen for the program above?
© Najwa Abd Ghafar – UiTM Johor
QUICK EXERCISE
©Rose Hafsah Ab Rauf – UiTM Shah Alam
What will be the output displayed on the screen for the program above?