SlideShare a Scribd company logo
STRUCTUR
ES
LOOP
1
WHIE DO WHILE
FOR NESTED
WHILE
FOR
DO WHILE
NESTED
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if you
specifically know start and end
position of the loop counter.
used for executing a block of
statements repeatedly until a
given condition returns false.
it executes the statements
inside the body of do-while
before checking the condition.
2
WHILE DO WHILE
FOR NESTED
WHILE
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
Here you can add some short text that is
important to explain the tittle text
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if
you specifically know start and end
position of the loop counter.
used for executing a block
of statements repeatedly
until a given condition
returns false.
it executes the statements
inside the body of do-while
before checking the
condition.
3
WHILE DO WHILE
FOR NESTED
WHILE
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
Here you can add some short text that is
important to explain the tittle text
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if
you specifically know start and end
position of the loop counter.
used for executing a block
of statements repeatedly
until a given condition
returns false.
it executes the statements
inside the body of do-while
before checking the
condition.
4
WHILE DO WHILE
FOR NESTED
WHILE
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if
you specifically know start and end
position of the loop counter.
used for executing a block
of statements repeatedly
until a given condition
returns false.
it executes the statements
inside the body of do-while
before checking the
condition.
5
WHILE DO WHILE
FOR NESTED
WHILE
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if
you specifically know start and end
position of the loop counter.
used for executing a block
of statements repeatedly
until a given condition
returns false.
it executes the statements
inside the body of do-while
before checking the
condition.
6
WHILE DO WHILE
FOR NESTED
WHILE
used for executing a block of
statements repeatedly until a
given condition returns false.
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if you
specifically know start and end
position of the loop counter.
it executes the statements
inside the body of do-while
before checking the condition.
7
WHILE LOOP
While loop is the simplest loop of c++ language. This loop executes one
or more statement while the given condition remain true. It is useful
when the number of iteration is not known in advance.
In most computer programming languages, a while loop is a control
flow statement that allows code to be executed repeatedly based on a
given Boolean condition. The while loop can be thought of as a
repeating if statement
8
SYNTAX
• The syntax of while loop is as follow:
While (condition)
Statement;
Condition:
The condition is given as a relational expression. The statement is
executed only if the given condition is true. If the condition is false, the statement is
never executed.
Statement:
statement is the instruction that is executed when the condition is true.
If two or more statement are used, these are given in braces { }. It is called the body
of the loop.
9
SYNTAX FOR COMPOUND SATATEMENT
• The syntax for compound statement is as follow:
• While (condition)
{
statement 1;
statement 2;
:
:
statement N;
10
WORKING OF ‘WHILE ‘ LOOP
• First of all, the condition is evaluated.
• If it is true, the control enters the body of the loop and executes all statement in
the body.
• After executing the statements, it again moves to the start of the loop and
evaluates the condition again.
• This process continuous as long as the condition remains true.
• when the condition becomes false, the loop is terminated.
• while loop terminates only when the condition becomes false.
• If the condition remains true, the loop never end.
• The loop that has no end point is known as an infinite loop.
11
EXAMPLE:
Write a program that displays “ Pakistan” for five times using while loop.
#include<iostream.h>
#include<conio.h>
void main ()
{
int n;
clrscr();
n=1
while(n<=5)
{
cout<<“Pakistan”<<endl;
n++;
}
getch();
} 12
What is do –while loop
• Do while loop is a variant of While loop where the
condition is not checked at the top but at the end of
the loop.
• It is known as exit controlled loop.
DO WHILE LOOP
13
Uses of do while loop
• It is used to execute a statement or set of statements
repeatedly as long as the given condition remains true.
• It is mostly used for menu selection(or to enter records).In
menu selection we have to execute the body of loop at least
once.
Syntax
do
{
body of the loop;
}while (condition);
14
Working of do while loop
• When the do while statement is executed first the body of the loop is executed
and then the condition is evaluated
• If condition is True ,execution control goes back to the beginning of the do –while
loop. This process is repeated.
• When the given condition is false during execution ,the loop is terminated.
15
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int no=2;
do
{
printf(“t%d”,no);
no=no+2;
}
while(no<=100);
getch();
}
16
Print even numbers from 1 to 100 with the help of do –while loop.
DIFFERENCE
While loop
• Test condition comes before the body
of loop.
• At the beginning of loop condition is
evaluated and then body of the loop
is executed if the condition is true.
• Semicolon(;)is not given after the
while(condition).
Do while loop
• Test condition comes after the body of
the loop.
• The condition is evaluated after
executing the body of loop. The body
of the loop must be executed at least
once even if the condition is false.
• Semicolon(;) is given after the while
(condition).
17
FOR LOOP
The “FOR” loop is used to execute a statement or set of statements repeatedly for a
specified number of times.
Syntax
for (int; condition; increment )
{
statement;
}
18
Example : Write a program that displays counting from 1 to 10 using
for-loop
#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
for(n=1;n<=10;n++)
cout<<n<<endl;
cout<<“ok”;
getch();
}
19
Example: Write a program that uses a “for” loop structure and the
following series:
1,2,4,8,16,32,64
#include<iostream.h>
#include<conio.h>
main( )
{
int,n,res;
clrscr ( );
for (n=1;n<=64;n+=n)
count<<n<<“t”;
getch ( );
}
20
NESTED WHILE LOOP
A while loop inside another while loop is called nested while loop
Syntax of Nested while loop
21
EXAMPLE OF NESTED LOOP
Example : C program to print the number pattern.
#include<iostream.h>
#include<conio.h>
Void main()
{
int i=1,j;
clrscr();
while(i<=5)
{
j=1;
while(j<=i)
{
cout<<j;
j++;
}
cout<<“n”;
i++;
}
getch();
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
22
A do-while loop inside another do-while loop is called nested do-while loop.
NESTED DO WHILE LOOP
Syntax of Nested do-while loop
23
EXAMPLE OF NESTED DO WHILE
LOOPExample : C program to print the given star pattern. *
* *
* * *
* * * *
* * * * *
#include<iostream.h>
#include<conio.h>
Void main()
{
Int i=1,j;
Clrscr();
do
{
j=1;
do
{
cout<<“*”;
j++;
}while(j<=i);
i++;
cout<<“n”;
} while(i<=5);
getch();
} 24
NESTED FOR LOOP
A for loop inside another for loop is called nested for loop.
Syntax of Nested for loop
25
EXAMPLE OF NESTED FOR LOOP
Example: C program to print the 2x2 matrices.
#include<iostrem.h>
#include<conio.h>
void main()
{
int A[2][2];
clrscr();
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
cin>>A[i][j];
cout<<“t”<<A[i][j];
}
cout<<“n”;
}
getch();
} 26
27

More Related Content

PPTX
File handling in c++
PPTX
C++ string
PPTX
Object oriented programming
PPTX
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
PDF
Multiple Inheritance
PPTX
this keyword in Java.pptx
PPTX
While , For , Do-While Loop
PPTX
joins in database
File handling in c++
C++ string
Object oriented programming
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
Multiple Inheritance
this keyword in Java.pptx
While , For , Do-While Loop
joins in database

What's hot (20)

PPTX
C# Framework class library
PDF
Exception handling
PPTX
Looping statement
PPTX
Namespaces in C#
PPTX
C++ language basic
PPTX
Scope rules : local and global variables
PPTX
[OOP - Lec 18] Static Data Member
PPTX
Inheritance in oops
PPTX
Polymorphism
PPTX
Macro Processor
PPT
Function overloading(c++)
PDF
Java Thread Synchronization
PPTX
OOPS Basics With Example
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPT
Data members and member functions
PPTX
Data members and member functions
PPT
PPT
friend function(c++)
PPT
01 c++ Intro.ppt
C# Framework class library
Exception handling
Looping statement
Namespaces in C#
C++ language basic
Scope rules : local and global variables
[OOP - Lec 18] Static Data Member
Inheritance in oops
Polymorphism
Macro Processor
Function overloading(c++)
Java Thread Synchronization
OOPS Basics With Example
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Data members and member functions
Data members and member functions
friend function(c++)
01 c++ Intro.ppt
Ad

Similar to Loop structures (20)

PPTX
C language 2
PPTX
PPTX
Cse lecture-7-c loop
PDF
Cpp loop types
PPTX
Loop in C Properties & Applications
PDF
whileloop-161225171903.pdf
PPT
While loop
PPT
Looping in c++
PPT
Looping in c++
PPTX
Lecture on Loop while loop for loop + program
DOCX
loops and iteration.docx
PPTX
Loops c++
PPTX
Loops in c
PPTX
The Loops
PPTX
Loops Basics
PPTX
2nd year computer science chapter 12 notes
PPT
Deeksha gopaliya
PPTX
Loops In C++
PDF
Java Repetiotion Statements
C language 2
Cse lecture-7-c loop
Cpp loop types
Loop in C Properties & Applications
whileloop-161225171903.pdf
While loop
Looping in c++
Looping in c++
Lecture on Loop while loop for loop + program
loops and iteration.docx
Loops c++
Loops in c
The Loops
Loops Basics
2nd year computer science chapter 12 notes
Deeksha gopaliya
Loops In C++
Java Repetiotion Statements
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Lesson notes of climatology university.
PPTX
Pharma ospi slides which help in ospi learning
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
master seminar digital applications in india
PPTX
Cell Types and Its function , kingdom of life
PDF
Computing-Curriculum for Schools in Ghana
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Insiders guide to clinical Medicine.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Structure & Organelles in detailed.
PPTX
Institutional Correction lecture only . . .
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
2.FourierTransform-ShortQuestionswithAnswers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O7-L3 Supply Chain Operations - ICLT Program
Lesson notes of climatology university.
Pharma ospi slides which help in ospi learning
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
master seminar digital applications in india
Cell Types and Its function , kingdom of life
Computing-Curriculum for Schools in Ghana
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial disease of the cardiovascular and lymphatic systems
Insiders guide to clinical Medicine.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Structure & Organelles in detailed.
Institutional Correction lecture only . . .
Pharmacology of Heart Failure /Pharmacotherapy of CHF

Loop structures

  • 2. WHIE DO WHILE FOR NESTED WHILE FOR DO WHILE NESTED LOOP Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met. Writing a loop inside another loop is known as nested loop. We can write while , do...while , for loop in a nested loop. for loop is easy to implement if you specifically know start and end position of the loop counter. used for executing a block of statements repeatedly until a given condition returns false. it executes the statements inside the body of do-while before checking the condition. 2
  • 3. WHILE DO WHILE FOR NESTED WHILE FOR Here you can add some short text that is important to explain the tittle text DO WHILE NESTED Here you can add some short text that is important to explain the tittle text LOOP Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met. Writing a loop inside another loop is known as nested loop. We can write while , do...while , for loop in a nested loop. for loop is easy to implement if you specifically know start and end position of the loop counter. used for executing a block of statements repeatedly until a given condition returns false. it executes the statements inside the body of do-while before checking the condition. 3
  • 4. WHILE DO WHILE FOR NESTED WHILE FOR Here you can add some short text that is important to explain the tittle text DO WHILE NESTED Here you can add some short text that is important to explain the tittle text LOOP Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met. Writing a loop inside another loop is known as nested loop. We can write while , do...while , for loop in a nested loop. for loop is easy to implement if you specifically know start and end position of the loop counter. used for executing a block of statements repeatedly until a given condition returns false. it executes the statements inside the body of do-while before checking the condition. 4
  • 5. WHILE DO WHILE FOR NESTED WHILE FOR Here you can add some short text that is important to explain the tittle text DO WHILE NESTED LOOP Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met. Writing a loop inside another loop is known as nested loop. We can write while , do...while , for loop in a nested loop. for loop is easy to implement if you specifically know start and end position of the loop counter. used for executing a block of statements repeatedly until a given condition returns false. it executes the statements inside the body of do-while before checking the condition. 5
  • 6. WHILE DO WHILE FOR NESTED WHILE FOR Here you can add some short text that is important to explain the tittle text DO WHILE NESTED LOOP Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met. Writing a loop inside another loop is known as nested loop. We can write while , do...while , for loop in a nested loop. for loop is easy to implement if you specifically know start and end position of the loop counter. used for executing a block of statements repeatedly until a given condition returns false. it executes the statements inside the body of do-while before checking the condition. 6
  • 7. WHILE DO WHILE FOR NESTED WHILE used for executing a block of statements repeatedly until a given condition returns false. FOR Here you can add some short text that is important to explain the tittle text DO WHILE NESTED LOOP Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met. Writing a loop inside another loop is known as nested loop. We can write while , do...while , for loop in a nested loop. for loop is easy to implement if you specifically know start and end position of the loop counter. it executes the statements inside the body of do-while before checking the condition. 7
  • 8. WHILE LOOP While loop is the simplest loop of c++ language. This loop executes one or more statement while the given condition remain true. It is useful when the number of iteration is not known in advance. In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement 8
  • 9. SYNTAX • The syntax of while loop is as follow: While (condition) Statement; Condition: The condition is given as a relational expression. The statement is executed only if the given condition is true. If the condition is false, the statement is never executed. Statement: statement is the instruction that is executed when the condition is true. If two or more statement are used, these are given in braces { }. It is called the body of the loop. 9
  • 10. SYNTAX FOR COMPOUND SATATEMENT • The syntax for compound statement is as follow: • While (condition) { statement 1; statement 2; : : statement N; 10
  • 11. WORKING OF ‘WHILE ‘ LOOP • First of all, the condition is evaluated. • If it is true, the control enters the body of the loop and executes all statement in the body. • After executing the statements, it again moves to the start of the loop and evaluates the condition again. • This process continuous as long as the condition remains true. • when the condition becomes false, the loop is terminated. • while loop terminates only when the condition becomes false. • If the condition remains true, the loop never end. • The loop that has no end point is known as an infinite loop. 11
  • 12. EXAMPLE: Write a program that displays “ Pakistan” for five times using while loop. #include<iostream.h> #include<conio.h> void main () { int n; clrscr(); n=1 while(n<=5) { cout<<“Pakistan”<<endl; n++; } getch(); } 12
  • 13. What is do –while loop • Do while loop is a variant of While loop where the condition is not checked at the top but at the end of the loop. • It is known as exit controlled loop. DO WHILE LOOP 13
  • 14. Uses of do while loop • It is used to execute a statement or set of statements repeatedly as long as the given condition remains true. • It is mostly used for menu selection(or to enter records).In menu selection we have to execute the body of loop at least once. Syntax do { body of the loop; }while (condition); 14
  • 15. Working of do while loop • When the do while statement is executed first the body of the loop is executed and then the condition is evaluated • If condition is True ,execution control goes back to the beginning of the do –while loop. This process is repeated. • When the given condition is false during execution ,the loop is terminated. 15
  • 17. DIFFERENCE While loop • Test condition comes before the body of loop. • At the beginning of loop condition is evaluated and then body of the loop is executed if the condition is true. • Semicolon(;)is not given after the while(condition). Do while loop • Test condition comes after the body of the loop. • The condition is evaluated after executing the body of loop. The body of the loop must be executed at least once even if the condition is false. • Semicolon(;) is given after the while (condition). 17
  • 18. FOR LOOP The “FOR” loop is used to execute a statement or set of statements repeatedly for a specified number of times. Syntax for (int; condition; increment ) { statement; } 18
  • 19. Example : Write a program that displays counting from 1 to 10 using for-loop #include<iostream.h> #include<conio.h> main() { int n; clrscr(); for(n=1;n<=10;n++) cout<<n<<endl; cout<<“ok”; getch(); } 19
  • 20. Example: Write a program that uses a “for” loop structure and the following series: 1,2,4,8,16,32,64 #include<iostream.h> #include<conio.h> main( ) { int,n,res; clrscr ( ); for (n=1;n<=64;n+=n) count<<n<<“t”; getch ( ); } 20
  • 21. NESTED WHILE LOOP A while loop inside another while loop is called nested while loop Syntax of Nested while loop 21
  • 22. EXAMPLE OF NESTED LOOP Example : C program to print the number pattern. #include<iostream.h> #include<conio.h> Void main() { int i=1,j; clrscr(); while(i<=5) { j=1; while(j<=i) { cout<<j; j++; } cout<<“n”; i++; } getch(); } 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 22
  • 23. A do-while loop inside another do-while loop is called nested do-while loop. NESTED DO WHILE LOOP Syntax of Nested do-while loop 23
  • 24. EXAMPLE OF NESTED DO WHILE LOOPExample : C program to print the given star pattern. * * * * * * * * * * * * * * * #include<iostream.h> #include<conio.h> Void main() { Int i=1,j; Clrscr(); do { j=1; do { cout<<“*”; j++; }while(j<=i); i++; cout<<“n”; } while(i<=5); getch(); } 24
  • 25. NESTED FOR LOOP A for loop inside another for loop is called nested for loop. Syntax of Nested for loop 25
  • 26. EXAMPLE OF NESTED FOR LOOP Example: C program to print the 2x2 matrices. #include<iostrem.h> #include<conio.h> void main() { int A[2][2]; clrscr(); for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { cin>>A[i][j]; cout<<“t”<<A[i][j]; } cout<<“n”; } getch(); } 26
  • 27. 27