You are on page 1of 18

Lesson 5

LOOPING
STATEMENTS
LOOPING STATEMENT

Most of the time when we write a program in any programming


language, we want to repeat the execution of the program, or we want to
generate something in our program, in this case, we need to use looping
or iteration statements.

in C++ there are three looping statements for,while and do-


while each of them has a specific purpose in different programming
problems that a programmer may encounter.
WHAT IS A LOOP

This refers to a set of statements in a program executed


repeatedly, either a fixed number of times or until some condition is
TRUE or FALSE . It also control structures in which a block of
instructions is repeated until a condition is fulfilled.

WHY USE LOOP?


Where need repetition of same code a number of times at that
place use Loop in place of writing more than one statements. The way of
the repetition will form a circle that’s why repetition statements are called
loops.
FLOW CHART OF A LOOPING STATEMENT IN C++
THE ADVANTAGE OF USING LOOPING STATEMENT

• Reduce the length of the code


• Take less memory space.
FOR LOOP STATEMENT

one of the most frequently used looping statement is the for loop
statement. The reason why behind that is the for loop statement is very
easy to use and implement because all the declaration for the initializing
value, conditions and increment/decrement is written in a single line of
code.
Syntax
for (initialization; conditional expression; increment/decrement expression)
{
statement1;
statement2;
statement3;
}
Initialization – This is an assignment statement that is
used to set a loop control value

Conditional Expression – This is a relational expression


that determines when the loop will exit by testing the
loo[ control variable against some value.

Increment/Decrement – It defines ho the loop control


variable will change each time the loop is repeated.
Example Program
#include<iostream>
using namespace std;
int main()
{
cout<<"\n\n";
cout<<"\tFor Loop Statement Demonstration";
cout<<"\n\n";

//for loop starts here


for (int a=1; a<=20; a+=1){
cout<<" "<<a<<" ";
}

cout<<"\n\n";
cout<<"\tEnd of the Program";
cout<<"\n\n";
}
Problem No. 1

Design a program that will ask the user to give a number and then the program
will display the list of ODD and EVEN numbers using for loop statements.

Problem No. 2

Write a program that will ask the user to give a number and then the program
will calculate the total sum of the numbers using for looping statement.

Problem No. 3

Design a program that will ask the user to give a number and then the program
will determine and check if the given number is a Prime number or NOT a Prime
Number using for loop statement.
WHILE LOOP STATEMENT

A while lopp is a control structure that allows you to repeat


a task a certain number of times. In addition, while loop statement
will continue its execution of the loop if the condition is TRUE but it
will stop if the condition is already FALSE.
Syntax
initialization;
while (condition){
statement1;
statement2;
statement3;
increment/decrement(++,--};
}

Note: if while loop condition never false then loop


become infinite loop.
Example Program
#include<iostream>
using namespace std;
int main()
{
int n=15;
cout<<"\n\n";
while(n>0){
cout<<n<<".";
n--;
}
cout<<"\n\n";
cout<<"\t\t\tLIFTOFF!!!";
cout<<"\n\n";
cout<<"\t\t\End of The Program";
cout<<"\n\n";

}
Problem No. 1

Design a program that will ask the user to give number and then the program
will compute the sum of all the digits of the given number using while loop statement.

Problem No. 2

Write a program that will accept a number from the user and then the program
will reverse the arrangement of the given number by the user using while loop statement.
DO WHILE LOOP STATEMENT

The do-while loop is similar to a while loop, except that a do-


while loop is guaranteed to execute at least one time. As you see in the
syntax that the condition appears at the end of the loop, so the statements
in the loop execute once before the condition is being tested.
if the condition expression is True, the flow of control jumps
back up to do, and the statements in the loop execute again. This process
repeats until the condition expression is false.
Syntax
initialization;
do{
statement1;
statement2;
statement3;
increment/decrement(++,--};
}while(condition);
When to use the do while loop statement

When we need to repeat the statement block at least 1 time then


we use a do while loop.
Example Program
#include <iostream>
using namespace std;
int main()
{
int num=0;
int a=1;
cout<<"\t Do While Loop Statement Demonstration";
cout<<"\n\n";

cout<<"\t Give a Number: ";


cin>>num;

do{
cout<<" "<<a<<" ";
a++;
}while (a<=num);
cout<<"\n\n";
cout<<"\t End of the Program";
}
Problem No. 1

Desing a program that will as the user to give two numbers and then the
program will compute the sum of the two numbers and display the result on the screen.
After which the program will ask the user if the user would like to continue using the
program or not using do while statement.

Problem No. 2

Design a program that will ask the user of two numbers and then the program
will compute the LCM of the two numbers using do while loop statement.

You might also like