SlideShare uma empresa Scribd logo
1 de 27
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados (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
 

Semelhante a Loop structures

Semelhante a 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
 

Último

CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxSaurabhParmar42
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphNetziValdelomar1
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17Celine George
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICESayali Powar
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationMJDuyan
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17Celine George
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.raviapr7
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice documentXsasf Sfdfasd
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxAditiChauhan701637
 

Último (20)

CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptx
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a Paragraph
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICE
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive Education
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice document
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptx
 

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