Introduction To Programming 05
Introduction To Programming 05
Science
Subject: Introduction to Programming-C++
Lecturer: Mr. Fakhruddin Noori
C++ Programming Language
Chapter 05
C++ Programming Chapter 05
Language
Principles of
Marketing
Loops Statements
C++ Programming Chapter 05
Language
LOOPS STATEMENT
Principles of
A statement or set of statements that is executed repeatedly is called loop.
Marketing
The statements in a loop are executed for a specified number of times or
until some condition remain true.
in C++ there are three kinds of loop statements .
The while loop
The do while loop
The for loop
C++ Programming Chapter 05
Language
THE WHILE LOOP
It is a conditional loop statement.
Principles ofto execute a statement or set of
It is used
statements as long as the given condition remains true.
Marketing
The syntax of the while loop is
while (condition)
statement;
Condition: it consists of a relational expression. If it is true statements given
in while loop is executed.
C++ Programming Chapter 05
Language
THE WHILE LOOP
Principles
statement: it represents the body ofcompound statements are
of loop. The
written in braces { }. Marketing
syntax for compound statements.
while(condition)
{ statement-1;
statement -2;
}
C++ Programming Chapter 05
Language
Flow chart for while loop
Principles of
Marketing
C++ Programming Chapter 05
Language
Program to print statement using while loop
int main()
{
int a;
a=1;
while(a<=5)
{
cout<<“I am a student”<<endl;
a=a+1;
}
cout<<“program Ends” ;
return 0;
}
C++ Programming Chapter 05
Language
when “while” loop is executed , the computer first evaluates the given
condition . If the given condition is true
Principles of the statement or set of statements
under while is executed.
Marketing
After executing the statements under while the control shifts back to while
and the condition is again tested. If the given condition becomes false at
any stage the execution of the body of loop is terminated.
And control shifts to the statement that comes immediately after the body
of the loop. The body of the loop must contain a statement so that the
condition on the loop becomes false during execution of the loop.
If the condition of the loop never becomes false , the loop never ends. It
becomes an infinite.
C++ Programming Chapter 05
Language
Program to calculate the sum of first ten odd numbers. also print the odd number.
int main()
{ Principles of
int s, n; Marketing
s=0;
n=1;
while (n<=10)
{
s=s + n;
cout<<n<<endl;
n=n+2;
}
cout<<“sum =”<<s<<endl;
return 0;
}
C++ Programming Chapter 05
Language
The “do-while” loop
The “do-while ” loop is alsoPrinciples of statement. It is like a while
conditional loop
Marketing
loop but in this loop the condition is tested after executing the statement of
the loop.
the syntax of the “do while ”loop.
do
{
statement;
}
while(condition);
C++ Programming Chapter 05
Language
Do while Loop Flow Chart
Principles of
Marketing
C++ Programming Chapter 05
Language
Program for do while loop
int main() Principles of
{ Marketing
int n;
n=1;
do
{
cout<<n<<endl;
n++;
} while(n<=10);
getch();
}
C++ Programming Chapter 05
Language
The for loop
The for loop statement is used to execute a of
Principles set of statements repeatedly for
a fixed number of times. It isMarketing
also known as counter loop. The structure of
this loop is different from both the while and do-while loop. It has
following parts:
o initialization
o condition
o increment
o body of loop
C++ Programming Chapter 05
Language
The For Loop
Principles
for ( initialization; condition; of
increment/decrement)
for(int a=5;a<10;a++) Marketing
In initialization part the value in the variable is assigned when control
enters into the loop first time. The initialization part of “for loop” is
optional. If this part is committed then a semicolon is used in its place.
the syntax of the for loop without initialization part is
for(; a<=10;a++)
C++ Programming Chapter 05
Language
The For Loop
Principles
in condition part the test condition ofThe body of the loop executes
is given.
Marketing
as long as this condition remains true.
for example a<=10.
in increment part the value in the variable is increased.
the statement under the for loop are the body of loop. If more than one
statement are to be executed these are enclosed in braces
C++ programming Chapter 05
Language
Principles of
Marketing
C++ Programming Chapter 05
Language
Write program to print first ten natural number
main() Principles of
{ Marketing
int c;
for (c=1;c<=10;c++)
cout<<c<<endl;
}
C++ Programming Chapter 05
Language
Principles of
Marketing
e n d
# 5
te r
h a p
C