Loops
Loops
Loops
Engr.Iftikhar Ahmed
[email protected]
Loops
A type of control structure that repeats a statement or
set of statements is known as LOOPING structures.
Its also known as iterative OR repetitive structure.
In sequential structure, all statements are executed
once.
On the other hand, conditional structure may execute
or skip a statement on the basis of some given
condition.
In some situations it is required to repeat a statement
or number of statements for a specified number of
times.
counter-controlled loops
• This type of loop depends on the value of a variable known
as counter variable.
• The value of counter variable is incremented or
decremented each time the body of the loop is executed.
• This loop terminates when the value of counter variable
reaches a particular value.
• The number of iterations of counter-controlled loop is
known in advance.
• The iterations of this loop depends on the following:
• Initial value of the counter variable
• Termination condition
TRUE
condition Loop body
NO
Write a program that displays “Chemical Engineering” five times using while
loop.
#include<iostream>
using namespace std;
int main( )
{
int n=1;
While (n<=5)
{
cout<<“ Chemical Engineering”<<endl;
n++;
}
Return 0;
}
Write a program that displays counting from 1 to 10 using while loop.
#include<iostream>
using namespace std;
int main( )
{
int n;
n=1;
While (n<=10)
{
cout<<n<<endl;
n++;
}
Return 0;
}
Write a program that displays first five numbers and their sum using while
loop.
#include<iostream>
using namespace std;
int main( )
{
int c, sum;
n=1;
Sum=0;
While (n<=5)
{
cout<<c<<endl;
Sum=sum+c;
C=c+1;
}
cout<<“SUM is ”<<sum;
Return 0;
}
Write a program that displays first five numbers with their squares using
while loop.
#include<iostream>
using namespace std;
int main( )
{
int n;
n=1;
While (c<=5)
{
cout<<n<<n*n<<endl;
n++;
}
Return 0;
}
Write a program that inputs a number from user and displays a table of that
number using while loop.
#include<iostream>
using namespace std;
int main( )
{
int n,c;
c=1;
Cout<<“Enter a number :”;
Cin>>n;
While (c<=10)
{
cout<<n<<“*”<<c<<n*c<<endl;
C=c+1;
}
Return 0;
}
Write a program that inputs a number from user and displays the factorial of
that number using while loop.
#include<iostream>
using namespace std;
int main( )
{
int n,c,f;
c=f=1;
Cout<<“Enter a number :”;
Cin>>n;
While (c<=n)
{
f=f*c;
c=c+1;
}
cout<<“Factorial of “<<n<<“ is ”<<f;
return 0;
}
Write a Program that displays the degree-to-radian table using while
loop.
Write a Program that displays the sum of following series using while
loop. (1+1/2+1/4+1/6+……..1/100)
Write a Program that inputs a positive number. It then displays the sum
of all odd numbers and the sum of all even numbers from 1 to number
entered by user using while loop.
Write a Program that inputs a sentence from user and counts the numbers
of words in that sentence.
Write a Program that inputs starting and ending number from the user
and displays all even & odd numbers in the given range while loop.
Write a program using while loop to enter number from the user and then
display it. The loop is terminated when the user enters -1.
do-While loop
• Do-While loop is an iterative control in C++.
• This loop executes one or more statements while the given
condition is true. In this loop, the condition comes after
the body of the loop. It is an important loop in a situation
when the loop body must be executed at least once.
Syntax:
The syntax of while is as under: Beginning of the Loop
Do
{
statement 1 ;
statement 2 ;
.
Body of Loop
.
statement N ;
}
while (condition)
Flow chart of do-While loop
Loop body
TRUE
condition
NO
Difference between while & do-while loop
In while loop condition comes before In do-while loop condition comes after
the body of the loop. the body of the loop.
Loop body
}
Pretest and Posttest loops
A posttest is a condition that is used to test for the
completion of loop at the bottom of the loop. It
means that the statements in the loop will always be
executed at least once.
do;
{
Loop body
}
While (n==0)
FOR loop
For loop executes one or more statements for a specified
number of times. This loop is also called counter-controlled
loop. It is the most flexible loop. That is why, it is the most
frequently-used loop by the programmers.
Syntax:
for (initialization; condition; increment / decrement)
{
Statement 1;
Statement 2;
.
.
Statement n;
}
Flowchart of FOR loop
initialization
Increment / Decrement
TRUE
condition Loop Body
FALSE
Write a program that displays counting from 1 to 10 using for loop.
#include<iostream>
using namespace std;
int main( )
Initialization
{
int n;
For (n=1; n<=10; n++)
cout<<n<<endl;
Increment / decrement
Return 0;
}
Condition
Write a program that displays numbers from 1 to 10 using for loop.
#include<iostream>
using namespace std;
int main( )
{
int n;
For (n=1; n<=10; n++)
cout<<n<<endl;
Return 0;
}
Write a program that displays product of all odd numbers from 1 to 10
using for loop.
#include<iostream>
using namespace std;
int main( )
{
Long int product=1;
Int c;
For (c=1; c<=10; c=c+2)
product *=c;
cout<<“Result is : ”<<product<<endl;
Return 0;
}
Write a program that inputs table number and length of table and
then displays the table using for loop.
#include<iostream>
using namespace std;
int main( )
{
int tab, len,c;
Cout<<“Enter number for table : ”;
Cin>>tab;
Cout<<“Enter the Length of table :”;
Cin>>len;
For (c=1; c<=len; c++)
Cout<<tab<<“ * ”<<c<<“ = ”<<tab*c<<endl;
Return 0;
}
Write a program that to print 1 4 7 10 . . . . .40 using for loop.
#include<iostream>
using namespace std;
int main( )
{
int a=1, i, incre=3;
Cout<<“\n The Series is as follow :”;
For (i=1; a<=40; i++)
{
Cout<<a<<“ ”;
a=a+incre;
}
Return 0;
}
Continue statement
The continue statement is used in the body of the loop.
It is used to move the control to the start of loop body.
When this statement is executed in the loop body, the
remaining statements of current iteration are not
executed. The control directly moves to next iteration.
int x;
for(x=1;x<=5;x++)
{
cout<<“Hello World !\n”;
continue;
cout<<“knowledge is Power. ”;
}
Write a program that inputs a number from user using for loop.it
displays the number if it is greater than ZERO otherwise it inputs
next number using continue statement.
#include<iostream>
using namespace std;
int main( )
{
int x, num; return 0;
For (x=1; x<=5; x++) }
{
Cout<<“Enter a Number : ”;
Cin>>num;
If (num<=0)
continue
Cout<<“You Entered ”<<num<<endl;
}
break statement
The break statement is used in the body of the loop to
exit from the loop. when this statement is executed in
the loop body, the remaining iterations of the loop are
skipped. The control directly moves outside the body
and statement that comes after the body is executed.
for(x=1;x<=5;x++)
{
cout<<“Questioning\n”;
break;
cout<<“Gateway to Knowledge. ”;
}
cout<<“Good Luck”;