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
Terminology of tree
PPTX
Keywords, identifiers and data type of vb.net
PDF
Syntax Directed Definition and its applications
PPTX
Computer Science:Sql Set Operation
PPT
SWITCH CASE STATEMENT IN C
PPTX
Data structures trees - B Tree & B+Tree.pptx
PPTX
Finite Automata in compiler design
PPTX
AWT Packages , Containers and Components
Terminology of tree
Keywords, identifiers and data type of vb.net
Syntax Directed Definition and its applications
Computer Science:Sql Set Operation
SWITCH CASE STATEMENT IN C
Data structures trees - B Tree & B+Tree.pptx
Finite Automata in compiler design
AWT Packages , Containers and Components

What's hot (20)

PPTX
8. transactions
PPT
Basic concept of OOP's
PPTX
Triggers
PPTX
Android Architecture.pptx
PPT
Slides chapter 8
PPTX
Regular Expression in Compiler design
PDF
State Space Search in ai
PDF
Major and Minor Elements of Object Model
PPT
1.Role lexical Analyzer
PPT
Applet life cycle
PPTX
Back patching
PPTX
Black box software testing
PPTX
String Manipulation in Python
PPTX
Waterfall model of Software Engineering
PPTX
Search Algorithms in AI.pptx
PPTX
States, state graphs and transition testing
PDF
Production System in AI
PPTX
Phases of compiler
PPTX
python conditional statement.pptx
8. transactions
Basic concept of OOP's
Triggers
Android Architecture.pptx
Slides chapter 8
Regular Expression in Compiler design
State Space Search in ai
Major and Minor Elements of Object Model
1.Role lexical Analyzer
Applet life cycle
Back patching
Black box software testing
String Manipulation in Python
Waterfall model of Software Engineering
Search Algorithms in AI.pptx
States, state graphs and transition testing
Production System in AI
Phases of compiler
python conditional statement.pptx
Ad

Similar to Loop structures (20)

PDF
presentationonnestingofloops-110228071935-phpapp01.pdf
PPTX
DOCX
loops and iteration.docx
PPTX
Loops in c
PPTX
The Loops
PPTX
C Programming: Looping Statements in C Pgm
PPTX
Looping (Computer programming and utilization)
PPTX
Presentation on nesting of loops
PPTX
Loops In C++
PPTX
Loops c++
PPTX
Loops Basics
PPTX
Cse lecture-7-c loop
PDF
Loop and while Loop
PPTX
Loop in C Properties & Applications
PPTX
Loop (Computer programming and utilization)
PDF
Chapter 3 - Flow of Control Part II.pdf
PPT
Control structures repetition
PDF
Repetition, Basic loop structures, Loop programming techniques
PDF
loops in C ppt.pdf
presentationonnestingofloops-110228071935-phpapp01.pdf
loops and iteration.docx
Loops in c
The Loops
C Programming: Looping Statements in C Pgm
Looping (Computer programming and utilization)
Presentation on nesting of loops
Loops In C++
Loops c++
Loops Basics
Cse lecture-7-c loop
Loop and while Loop
Loop in C Properties & Applications
Loop (Computer programming and utilization)
Chapter 3 - Flow of Control Part II.pdf
Control structures repetition
Repetition, Basic loop structures, Loop programming techniques
loops in C ppt.pdf
Ad

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Institutional Correction lecture only . . .
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
master seminar digital applications in india
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Supply Chain Operations Speaking Notes -ICLT Program
O5-L3 Freight Transport Ops (International) V1.pdf
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Pharmacology of Heart Failure /Pharmacotherapy of CHF
GDM (1) (1).pptx small presentation for students
Institutional Correction lecture only . . .
human mycosis Human fungal infections are called human mycosis..pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Anesthesia in Laparoscopic Surgery in India
STATICS OF THE RIGID BODIES Hibbelers.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
master seminar digital applications in india
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
01-Introduction-to-Information-Management.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

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