SlideShare a Scribd company logo
1 of 27
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

What's hot (20)

Loops c++
Loops c++Loops c++
Loops c++
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Loops
LoopsLoops
Loops
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Loops
LoopsLoops
Loops
 
Java loops
Java loopsJava loops
Java loops
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
While loops
While loopsWhile loops
While loops
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Iteration Statement in C++
Iteration Statement in C++Iteration Statement in C++
Iteration Statement in C++
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Looping
LoopingLooping
Looping
 
C++ loop
C++ loop C++ loop
C++ loop
 
3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)
 
Loops in c
Loops in cLoops in c
Loops in c
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Programming
ProgrammingProgramming
Programming
 

Similar to Loop structures

Similar to Loop structures (20)

C language 2
C language 2C language 2
C language 2
 
Loops
LoopsLoops
Loops
 
Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Loops in c
Loops in cLoops in c
Loops in c
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 
Deeksha gopaliya
Deeksha gopaliyaDeeksha gopaliya
Deeksha gopaliya
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Loop
LoopLoop
Loop
 
DECISION MAKING.pptx
DECISION MAKING.pptxDECISION MAKING.pptx
DECISION MAKING.pptx
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
loops in C ppt.pdf
loops in C ppt.pdfloops in C ppt.pdf
loops in C ppt.pdf
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 

Recently uploaded

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Recently uploaded (20)

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

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