C++ Repetition
Statement
COMPUTER PROGRAMMING 1
LEARNING OBJECTIVES
Students should be able to:
Understand the different types of loops such as for,
while, do-while and nested for loop.
Examine break and continue statements
Discover how to form and use nested control
structures
Contents
1. Loop Statements
2. Parts of a loop
3. Types of Loops
While Loop
For Loop
Do-while Loop
4. Nested Loops
5. Jump Statements
Why Is Repetition Needed?
Repetition allows you to efficiently use variables
Can input, add, and average multiple numbers using
a limited number of variables.
It can reduce the duplication of code.
LOOP STATEMENT
The loop statements allow a
set of instructions to be
performed repeatedly until a
certain condition is fulfilled.
Following is the general form
of a loop statement in most
of the programming
languages.
PARTS OF A LOOP
Initialization Expression(s) initialize(s) the loop
variables in the beginning of the loop.
TestExpression decides whether the loop will be
executed (if test expression is true) or not (if test
expression is false).
Update Expression(s) update(s) the values of
loop variables after every iteration of the loop.
TheBody-of-the-Loop contains statements to be
executed repeatedly.
TYPES OF LOOPS
C++ programming language provides following types of
loop to handle looping requirements:
Loop Type Description
WHILE LOOP Repeats a statement or group of
statements until a given condition is true. It
tests the condition before executing the
loop body.
FOR LOOP Execute a sequence of statements multiple
times and abbreviates the code that
manages the loop variable.
DO WHILE LOOP Like a while statement, except that it tests
the
condition at the end of the loop body
NESTED LOOP You can use one or more loop inside any
another while, for or do..while loop.
WHILE LOOP
Repeats a statement or group of
statements until a given condition is
true. It tests the condition before
executing the loop body.
WHILE LOOP
The syntax of while statement :
while (loop repetition condition)
statement
Loop repetition condition is the condition which
controls the loop.
The statement is repeated as long as the loop
repetition condition is true.
A loop is called an infinite loop if the loop repetition
condition is always true.
Example – While loop
Output
Program Code:
#include <iostream>
Write a program using namespace std;
that display int main() {
int i = 0;
“Hello! Granby” while (i < 10) {
cout << i <<" \t
10 times. Hello! Granby"<< "\n";
i++;
}
return 0;
}
LOGIC for WHILE LOOP
Test Yourself– While loop
Int i = 0;
while ( i < 10 ) {
cout << i;
i += 2;
}
Test Yourself– While loop
int i = 1;
while ( i < 10 ) {
cout << i
<<endl;
i += 2;
}
Test Yourself – While loop
Write a program that Program Code:
display this output
using While loop. #include <iostream>
using namespace std;
int main(){
int i=1;
while(i<=6){
cout<<"Value of
variable i is: "<<i<<endl;
i++;
}
return 0;
}
DO-WHILE LOOP
Like a while statement, except that
it tests the condition at the end of
the loop body
DO-WHILE LOOP
• The syntax of do-while statement in C++:
do
statement
while (loop repetition condition);
• The statement is first executed.
• If the loop repetition condition is true, the
statement is repeated.
• Otherwise, the loop is exited.
DO-WHILE LOOP
To avoid an infinite loop, the loop body must
contain a statement that makes the expression
false
The statement can be simple or compound
If compound, it must be in braces
do...while loop has an exit condition and always
iterates at least once (unlike for and while)
Example DO-WHILE LOOP
Program Code:
Output
int i = 0; #include <iostream>
using namespace std;
do {
int main() {
cout << i << "\ int i = 0;
do {
n"; cout << i << "\n";
i++; }
i++;
} while (i < 5);
return 0;
while (i < 5); }
Example DO-WHILE LOOP
Program Code:
Write a program that
display this output #include <iostream>
using Do-While loop. using namespace std;
int main(){
int num=1;
do{
cout<<"Value of num: "<<num<<endl;
num++;
}while(num<=6);
return 0;
}
LOGIC for DO WHILE LOOP
Test Yourself – DO WHILE
Output
Write a program that Program Code:
#include <iostream>
display counting from using namespace std;
1 to 10 using do-
while loop. int main() {
int i = 1;
do {
cout << i << "\n";
i++;
}
while (i <= 10);
return 0;
}
Test Yourself – DO WHILE
Write the complete code of the given output using DO WHILE LOOP.
int i = 0;
do {
cout<<i;
i++;
}
while (i<10);
Test Yourself – DO WHILE
Write the program that display the even numbers.
int i = 0;
do
{
cout << i;
i += 2;
}
while ( i < 10 );
FOR LOOP
Execute a sequence of statements
multiple times and abbreviates the
code that manages the loop
variable.
FOR LOOP
A for loop has the following syntax:
Example – For Loop
Output
Program Code:
for (int i = 0; i < 5; i++) { #include <iostream>
cout << i << "\n"; using namespace std;
} int main() {
for (int i = 0; i < 5;
i++) {
cout << i << "\n";
}
return 0;
}
Example 2 – For Loop
Outp
Program Code:
Write a program ut
that display the #include <iostream>
using namespace std;
even values
between 0 to 10. int main() {
for (int i = 0; i <= 10; i =
i + 2) {
cout << i << "\n";
}
return 0;
}
Logic for FOR LOOP
Test Yourself
Program Code:
Write a program that #include <iostream>
display the below using namespace std;
int main(){
output using FOR loop. for(int i=1; i<=6; i++){
cout<<"Value of variable i is:
"<<i<<endl;
}
return 0;
}
Multiple Choice Questions
½ crosswise yellow paper
1. The statement i++; is equivalent to
a.i = i + i;
b.i = i + 1;
c.i = i - 1;
d.i --;
[Link]'s wrong? for (int k = 2, k <=12, k++)
a. the increment should always be ++k
[Link] variable must always be the letter i
when using a for loop
c. there should be a semicolon at the end of the
statement
[Link] commas should be semicolons
3. Which looping process checks the test
condition at the end of the loop?
a. for
[Link]
c. do-while
[Link] looping process checks the test
condition at the end
4. Which looping process is best used when the
number of iterations is known?
a. for
[Link]
c. do-while
[Link] looping processes require that the
iterations be known
Writea complete code of the given
output.
Write a complete code of the given
output.
ANSWER
B
D
C
A
NESTED LOOP
Nested loops consist of an outer loop with one or more inner loops.
The above loop will run for 100*50 iterations.
Program Code:
Example #include <iostream>
using namespace std;
int main() {
int row;
Output
Write a program that cout<<"Enter number
display a pattern of a of rows:";
given numbers using cin>>row;
nested loop.
for (int x=1; x<=row;
x++)
{
for (int z=1;
z<=x; z++)
{
cout<<z<<" ";
}
cout<<"\n";
}
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
JUMP STATEMENTS
The break statement
• The break statement enables a program to skip over part of
the code.
• A break statement terminates the smallest enclosing while, do-
while and for statements.
• A break statement skips the rest of the loop and jumps over to
the statement following the loop.
EXAMPLE – BREAK LOOP
for (int i = 0; i < 10; i++) { Program Code:
if (i == 4) {
break; #include <iostream>
} using namespace std;
Output
cout << i << "\n";
} int main() {
for (int i = 0; i < 10;
i++) {
if (i == 4) {
break;
}
cout << i << "\n";
}
return 0;
}
BREAK STATEMENT in FOR LOOP
BREAK STATEMENT in WHILE LOOP
BREAK STATEMENT in DO-WHILE LOOP
CONTINUE
Outpu
Program Code:
The continue statement breaks one using Continue and For loop
t
iteration (in the loop), if a specified #include <iostream>
condition occurs, and continues with using namespace std;
the next iteration in the loop. int main() {
for (int i = 0; i < 10; i++) {
if (i == 4) {
for (int i = 0; i < 10; i+ continue;
+) { }
if (i == 4) { cout << i << "\n";
continue; }
} return 0;
cout << i << "\n"; }
}
EXAMPLE – CONTINUE using WHILE loop
Write a program that display Program Code:
the output below using
Continue and while loop. #include <iostream>
using namespace std; Output
int main(){
int j=6;
while (j >=0) {
if (j==4) {
j--;
continue;
}
cout<<"Value of j: "<<j<<endl;
j--;
}
return 0;
}
EXAMPLE – CONTINUE using DO-WHILE loop
Program Code
Write a program that display the #include <iostream>
output below using Continue and do- using namespace std;
while loop. int main(){
int j=4;
do {
if (j==7) {
j++;
continue;
}
cout<<"j is: "<<j<<endl;
j++;
}while(j<10);
return 0;
}
TEST YOURSELF
Write a c++ program to print the numbers from 0 to 10 using for loop
#include <iostream>
using namespace std;
int main(){
for (int i = 0; i <= 10; i++){
cout << "i=" << i<<endl;
}
return 0;
}
TEST YOURSELF
Write a c++ program to print the even numbers from 10 to 0 using for
loop.
#include <iostream>
using namespace std;
int main(){
for (int x = 10; x >= 1; x -= 2){
cout << "num (x) =" << x << endl;
}
return 0;
}
TEST YOURSELF
Write a c++ program asks the user to enter 5 characters using for loop,
if the user enters 'n' exit the loop.
#include <iostream>
using namespace std;
int main(){
char ch;
for (int i = 0; i <= 5; i++){
cout << "Enter charactar " << i <<" :";
cin >> ch;
if (ch == 'n')
break;
}
return 0;
}
TEST YOURSELF
Write a c++ program to print numbers from 10 to 0 using for loop
except the number 5.
#include <iostream>
using namespace std;
int main(){
for (int n = 10; n>0; n--) {
if (n == 5)
n--;
continue;
cout << n << ", ";
}
return 0;
}
Review the next topic about C+
+ Arrays, Arrays and Loops,
Omit Array Size, Get Array Size
and Multi-dimensional Arrays.