0% found this document useful (0 votes)
4 views

Lecture 5

Uploaded by

fakhrialamqazi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 5

Uploaded by

fakhrialamqazi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

T S

E N
ME
AT
ST
O P
LO LECTURE #5
LOOPS
 A statement or set of statements that is executed
repeatedly called loop.

 The statement in a loop are executed for a specified


number of times or until some condition remains true.

 In C++ there are three types of looping statements used.


These are:
 The “For” Loop
 The “while” Loop

 The “do-while” Loop


“FOR” LOOP
 The “for loop” is used to executed the statement or set of
statement repeatedly for a fixed number of times.

 It is also counter/Determined (No. of repetitions is


known) loop.

 “For” loop have the following parts:


 Initialization
 Condition

 Increment or decrement

 Body of the loop


SYNTAX OF “FOR” LOOP
For (initialization ; condition ; increment/Decrement)

 In initialization part, the value to the variable(set of


variables) is assigned. New variables can be declared and
assigned in this part.

For(int a=6,b=10; a<10; a++)


 In The above example “a” and “b” are variables that are
declared and initialized.
 If the variables have already been declared, then they
cannot be declared again.
 Initialization part of the “for” loop is optional.
For ( ; a<10; a++)
For (initialization ; condition ; increment/Decrement)

 In condition part, the test condition is given. The body


of the loop is executes as long as the conditions
remains true.

For (initialization ; condition ; increment/Decrement)


 In this part the value of the variable is incremented or
decremented.
 To increment/decrement the values of more than one
variable, the variables are written, separated by comma.

 This part is executed after the executing the body of the


loop. Each time the body of the loop is executed, the
value of the variable is increased or decreased.
PROGRAM
Write a program to print first ten natural numbers using
“For” loop.

#include<iostream. h>
#include<conio. h> #include<iostream. h>
#include<conio. h>
Void main() Void main()
{
{ Int c;
c=1;
Int c; For( ; c<=10;)
For(c=1; c<=10; C++) {
Cout<< c<<“\n”;
Cout<< c “\n”; C++;
}
getch(); getch();
} }
Flow Chart of “For” LOOP

false
Condition exit

True

Body of loop

Increment
expression
Write a program to calculate odd numbers from 1 to
10.
#include<iostream. h>
#include<conio. h>

Void main()

Int n;

For(n=1; n<=10; n+=2)

Cout<<n<<endl;

Getch();
}
Write a program to calculate the sum of odd numbers from 1 to 10
and then print sum on the screen.

#include<iostream. h>
#include<conio. h>
Void main()
{

Int n , sum;
Sum=0;
For(n=1; n<=10; n+=2)
{
Sum =sum + n;
Cout<<n<<endl;
}
Cout<<“Sum =”<<sum<<endl;
getch();
}
Assignment-1
Write a program to display the even number from 1
to 100 using for loop.

Assignment-2
Write a program to display the even number from
100 to 1 using for loop.

Assignment-3
Write a program to display the following sequence
0,5,10,20,25,30,35,40
PROGRAM
Write a program to display the following sequence.
0.5, 1, 1.5, 2, 2.5, 3
#include<iostream. h>
#include<conio. h>
Void main()
{
float n;
For(n=0.5; n<=3; n+=0.5)
{
Cout<<n<<endl;
}
getch();
Write a program to calculate the factorial of any positive
number.
(n*(n-1)*(n-2)*……..-1)

#include<iostream. h>
#include<conio. h>
Void main()
{
Int n, F=1;
Cout<<“Enter any positive number”;
Cin>>n;
For(int c=1;c<=n; c++)
F*=c;
Cout<<n<<“factorial is ”<<f;
getch();
}
“WHILE” LOOP
 It is a conditional loop statement used to execute a
statement or a set of statement as long as he given
condition remains true.

Syntax of “While” loop is:

While(Condition)
Statements;

Condition: it consist of relational expression. If it is true,


the statement or set of statement given in the while loop
is executed.
 Statement: it represents the body of the loop. The
compound statement or a set of statement are written
in braces {}.

 While loop for more than one statements can be


written as:

While(Condition)
{
Statement 1;
Statement 2;
Statement n;
}
 When the while loop statement is executed, the
condition is evaluated first.

 If the condition is true then the statement or set of


statement in the body of the body of the while loop is
executed.

 After executing the statement the control will shift


back to “While” and the condition is tested. if the
condition become false at any time then control will
go the statements that comes immediately after the
body of the loop.
PROGRAM
#include<iostream. h>
#include<conio. h>
Void main()
{
int n;
n=1;
While(n<=5)
{
Cout <<“computer science”<<endl ;
n=n+1;
}
getch();
}
Flow Chart of “While” LOOP

false
Condition

True
Body of loop

Statement after the


loop body
Write a program to calculate the sum of even numbers from 1 to 10 and
then print sum on the screen.

#include<iostream. h>
#include<conio. h>
Void main()
{

Int n, sum;
Sum=0;
n=0
while( n<=10)
{
sum =sum + n;
Cout<<n<<endl;
n=n+2;
}
Cout<<“Sum =”<<sum<<endl;
getch();
}
 Write a program to calculate factorial using while loop.
#include<iostream. h>
#include<conio. h>
Void main()
{
Int fact,n,c;
Cout<<“Enter a positive number”;
Cin>>n;
Fact=1;
While(n>=1)
{
Fact=fact*n;
n=n-1;
}
Cout<<factorial=“<<fact;
getch();
}
Write a program to find the sum of the following series:-
1+1/2+1/3+1/4………..1/45
#include<iostream. h>
#include<conio. h>
Void main()
{
Float s,c;
S=0.0;
C=1;
While(c<=45)
{
S=s+1.0/c;
c++;
}
Cout<<“sum of series=”<<s;
getch();
}
Write a program to print the number from 1 to 10 in
descending order.

#include<iostream. h>
#include<conio. h>
Void main()
{
Int n=10;
While(n!=0)
Cout<<n--<<endl;
Getch();
}
THE “DO-WHILE” LOOP
The “do-while” is condition loop statement.
It is same like “While” loop ,but in this case the condition
is tested after the execution of the statement.

Syntax of “do-while” Loop

do
{
Statements;
}
While(condition);
do it is a keyword that indicates the starting of
do-while loop.

Statements are enclosed in braces represents the


body of the loop.

Condition the condition must be true for the


execution of the loop.
 In do-while loop the body of the loop is executed
at least one before the condition is tested.

 The statements are repeatedly executed as long as


the test condition remains true.

 If the given condition becomes false at any stage


during the program execution the loop terminates
and the control shifts to the statement that comes
immediately after the keyword “While”.
Flow Chart of “do-While” LOOP

Body of loop

True
Condition

false

Statement after the


while loop
DIFFERENCE BETWEEN WHILE & DO-WHILE
 The “while” loop condition is test comes before the
body of the loop. first condition is tested and then the
body of the loop is executed.

 In “do-while” loop the condition is tested after the


execution of the statement. Atleast one statement is
executed if the condition is false.

 There is ; after condition in do-while.


PROGRAM
#include<iostream. h>
#include<conio. h>
Void main()
{
int n;
n=1;
{
Cout <<“computer science”<<endl ;
n=n+1;
}
While(n<=5);
getch();
}
ASSIGNMENT

 Repeat all the examples of “For loop” and “While


loop” Using “Do-While” Loop
NESTED LOOP
 A loop structure completely inside the body of another loop structure called nested
loop.

 The concept of nested loops is defined as following:


#include<iostream. h>
Void main()
{
Int x, i; Output
X=1;
While(x<=2) //outer loop
{ 1
i=1; computer science
Cout<<x<<endl;
computer science
While(i<=3)//inner Loop
{ computer science
Cout<<“computer science” ;
cout<<endl;
2
i++;
} computer science
x++; computer science
}
computer science
getch();
}
Write a program to print the output as
shown below:
1
1 2 #include<iostream. h>
1 2 3
1 2 3 4 #include<conio.h>
1 2 3 4
5
Void main()
{
Int x, i;
For (x=1;x<=5; x++)
{
for (i=1;i<=x; i++)
Cout<<i<<“\t”;
Cout<<endl;
}
getch();
}
Write a program to print the output
as shown below:
#include<iostream. h>
1 1 1 #include<conio.h>
1 1 2
1 2 1 Void main()
1 2 2 {
2 1 1
2 1 2 Int i, j ,k;
2 2 1
2 2 2
For(i=1;i<=2;i++)

For(j=1;j<=2;j++)

For(k=1;k<=2;k++)
Cout<<i<<“\t”<<j<<“\t”<<k;
cout<<endl;
getch();
}
#include<iostream. h>
Write a program to print the output
as shown below: #include<conio.h>
CS Void main()
CS CS
CS CS CS {
CS CS CS CS
CS CS CS CS
Int a, b;
CS For(a=1;a<=5; a++)
{
For (b=1;b<=a; b++)
Cout<<“CS”;
Cout<<endl;
}
getch();
}
CALCULATE LUCAS SEQUENCE
1 3 4 7 11 18 29
#include<iostream. h> a=b;
#include<conio.h> b=c;
Void main() C=a + b;
{ }
Int a, b, c; getch();
a=1; }
b=3;
Cout<<a<<“\t”<<b;
C=a + b;
While(c<=29)
{
FABONACI SERIES
#include<iostream. h> Cout<<third<<“\t”;
#include<conio.h> First = second;
Void main() Second=third;
{ }
Int first =0, second = 1,third; getch();
Int n , c; }
Clrscr();
Cout<<“order the fabonacci series”;
Cin>>n;
Cout<<“\n fabonacci series
is”<<endl;
Cout<<first <<“\t”<<second<<“\t”;
For(c=0;c<n-2;c++)
{
#include<iostream. h>
Write a program to print the output as
shown below: #include<conio.h>
5 4 3 2 1 Void main()
4 3 2 1 {
3 2 1
2 1 Int a, b, c, space =40;
1 C =5;
do
{
For(a=1;a<=space; a++)
Cout<<“ ”;
For(b=c; b>=1; b--)
Cout<<b<<“ ”;
Cout<<endl;
Space+=2;
N--;
}
While(c>=1);
getch();
WRITE A PROGRAM TO
CALCULATE PRIME NUMBER
#include<iostream. h> else
#include<conio.h> {
Void main()
c++;
{
Int n, c=2, r;
Continue;
Cout<<“Enter any number”; }
Cin>>n; }
While(c<n) Cout<<n<< “is a prime
{ number”;
r=n % c; getch();
If(r= =0)
}
{
Cout<<n<< “is a composite number”;
exit(0);
}
CONTINUE AND EXIT() STATEMENT
• The break statement immediately ends the looping process.
It transfer the control the closing braces of the most current
loop.

• If the break statement is used in the inner loop of a nested


loop, the break exit only the inner loop.

• Continue statement perform the opposite function of break


statement.

• Transfer the control at the top of the loop for next iteration.

• While exit() statement, declare in stdlib.h header file . It


enables you to exit the program i.e. it terminate the program
and return the user to the operating syatem.
Void main() Void main() Void main()
{ { {
While(c<=1 While(c<=10) While(c<=10)
0)
{ {
{
Continue; Exit(0);
Break;
} }
}
. .
.
.
. .
. . .
} } }

You might also like