SlideShare a Scribd company logo
Amrit Kaur Looping Constructs in C++
1
Chapter 3: Loop Constructs
A loop construct allow set of statements of a program to be executed certain number of
times. The statement will continue to execute until loop condition remains true. When the
condition becomes false, loop ends and control is passes to the statement following loop.
3.1 The while Loop
While loop continues until the evaluating condition becomes false. The condition
should be a logical expression.
Syntax
initial value;
while(logical_expression)
{
statement(s);
increment;
}
Example 1: A program to print even numbers beween 0 to n where value of n is specified by user.
#include<iostream.h>
#include<conio.h>
void main()
{ int n, i;
clrscr();
cout<<"n *** Program to print EVEN series upto limit ***";
cout<<"nEnter Limit ";
cin>>n;
cout<<"nEven Number between 0 to "<< n <<endl;
for(i=0;i<=n;i=i+2) //here i is loop variable
{
cout<<i<<" ";
}
}
3.2 The do...while Loop
The do..while loop is similar to while loop as both executes until the logical expression
becomes false.
Syntax
initial value;
do
{
statement(s);
increment;
Amrit Kaur Looping Constructs in C++
2
} while(logical_expression);
Example 2 : A program to print even numbers beween 0 to n where value of n is specified by
user.
#include<iostream.h>
#include<conio.h>
void main()
{
int n, i;
clrscr();
cout<<"n *** Program to print EVEN series upto limit ***";
cout<<"nEnter Limit ";
cin>>n;
cout<<"nEven Number between 0 to "<< n <<endl;
//initalise loop variable i
i=0;
do //loop execute atleast onec
{
cout<<i<<" ";
i=i+2; // increment
}
while(i<=n); // loop execute second time if condition is true
}
3.3 Difference between while and do...while loop
1. In while loop, logical expression is evaluated first. If it evaluates to true the body
of while loop will execute otherwise not. On the other hand, in do...while loop,
the body of loop executes first and then the logical expression is evaluated. If
the expression is true, next time the
loop will execute.
2. In while loop, the body will execute
only when the logical expression is
true. On the other hand, in
do...while loop body of loop will
execute at least one time
irrespective of logical expression.
Amrit Kaur Looping Constructs in C++
3
3.4 The for Loop
The for loop is used to repeat the statement or set of statements known or specified
number of times. The for statement consists of the keyword for followed by
parenthesis containing three expressions, each
separated by semicolon. The expressions are
i. Initialisation expression: Used to initialise the
loop variable. It is executed only once.
ii. Test expression / condition: Used to evaluate the
loop condition. It is executed each time control
passes to beginning of loop. The loop will
execute only if condition is true.
iii. Increment or decrement expression: It is always
executed when control return to beginning of the
loop.
Syntax
for(initialisation; test_expression; increment)
{
statement(s);
}
Example 3: A program to print even numbers beween 0 to n where value of n is specified by
user.
#include<iostream.h>
#include<conio.h>
void main()
{
int n, i;
clrscr();
cout<<"n *** Program to print EVEN series upto limit ***";
cout<<"nEnter Limit ";
cin>>n;
cout<<"nEven Number between 0 to "<< n <<endl;
for(i=0;i<=n;i=i+2) //here i is loop variable
{
cout<<i<<" ";
Amrit Kaur Looping Constructs in C++
4
}
}
NOTE:
 You can omit the Initialization Expression, but then the looping variable is
initialized before the for loop
 You can omit the test_expression / condition, but then the condition is specified
inside the body of the for loop, generally using an if statement.
 You can omit the increment, but then the increment expression is written inside
the body of the for loop.
 You can omit all three, but then the loop is called Infinite loop
3.5 The break Statements
The break statement is used to exit the loop before the loop condition is re evaluated.
Example 4: A program to print even numbers between 0 to n where value of n is specified by
user and program terminates when i is greater than 10.
#include<iostream.h>
#include<conio.h>
void main()
{ int n, i;
clrscr();
cout<<"n *** Program to print EVEN series upto limit ***";
cout<<"nEnter Limit ";
cin>>n;
cout<<"nEven Number between 0 to "<< n <<endl;
//initalise loop variable i
i=0;
do //loop execute atleast onec
{
cout<<i<<" ";
i=i+2; // increment
if (i==10) { break; }
}
while(i<=n); // loop execute second time if condition is true
}
3.6 The continue statement
Amrit Kaur Looping Constructs in C++
5
The continue statements is used to skip all the subsequent / remaining instructions
and take the control back to the loop (next increment/decrement value).
Example 4: A program to print even numbers between 0 to n where value of n is specified by
user and increments value of n by 10.
#include<iostream.h>
#include<conio.h>
void main()
{ int n, i;
clrscr();
cout<<"n *** Program to print EVEN series upto limit ***";
cout<<"nEnter Limit ";
cin>>n;
cout<<"nEven Number between 0 to "<< n <<endl;
//initalise loop variable i
i=0;
do //loop execute atleast onec
{
cout<<i<<"n";
i=i+2; // increment
if(i>20)
{ n=n+10;
cout<<"nProgram continue to print even nos upto limit n="<<n<<endl;
continue;
}
}
while(i<=n); // loop execute second time if condition is true
}

More Related Content

What's hot

Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loopsbsdeol28
 
C# Loops
C# LoopsC# Loops
C# Loops
Hock Leng PUAH
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
Loop c++
Loop c++Loop c++
Loop c++
Mood Mood
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
Debre Tabor University
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
Huda Alameen
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
Huda Alameen
 
Loops
LoopsLoops
Looping statement
Looping statementLooping statement
Looping statementilakkiya
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
deekshagopaliya
 

What's hot (20)

Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Looping
LoopingLooping
Looping
 
C# Loops
C# LoopsC# Loops
C# Loops
 
Loops c++
Loops c++Loops c++
Loops c++
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Loop c++
Loop c++Loop c++
Loop c++
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Loops in c
Loops in cLoops in c
Loops in c
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Loops
LoopsLoops
Loops
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Looping statement
Looping statementLooping statement
Looping statement
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 

Viewers also liked

Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOPAmrit Kaur
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 InheritanceAmrit Kaur
 
Chapter 6 OOPS Concept
Chapter 6 OOPS ConceptChapter 6 OOPS Concept
Chapter 6 OOPS ConceptAmrit Kaur
 
Security and Viruses
Security and VirusesSecurity and Viruses
Security and VirusesAmrit Kaur
 
ComputerBasics
ComputerBasicsComputerBasics
ComputerBasicsAmrit Kaur
 
7. exceptions handling in pl
7. exceptions handling in pl7. exceptions handling in pl
7. exceptions handling in pl
Amrit Kaur
 
10. timestamp
10. timestamp10. timestamp
10. timestamp
Amrit Kaur
 
8. transactions
8. transactions8. transactions
8. transactions
Amrit Kaur
 
12. oracle database architecture
12. oracle database architecture12. oracle database architecture
12. oracle database architecture
Amrit Kaur
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle database
Amrit Kaur
 
9. index and index organized table
9. index and index organized table9. index and index organized table
9. index and index organized table
Amrit Kaur
 

Viewers also liked (12)

Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOP
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
 
Chapter 6 OOPS Concept
Chapter 6 OOPS ConceptChapter 6 OOPS Concept
Chapter 6 OOPS Concept
 
Security and Viruses
Security and VirusesSecurity and Viruses
Security and Viruses
 
ComputerBasics
ComputerBasicsComputerBasics
ComputerBasics
 
7. exceptions handling in pl
7. exceptions handling in pl7. exceptions handling in pl
7. exceptions handling in pl
 
10. timestamp
10. timestamp10. timestamp
10. timestamp
 
8. transactions
8. transactions8. transactions
8. transactions
 
12. oracle database architecture
12. oracle database architecture12. oracle database architecture
12. oracle database architecture
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle database
 
9. index and index organized table
9. index and index organized table9. index and index organized table
9. index and index organized table
 

Similar to Chapter 3

Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
C++ control structure
C++ control structureC++ control structure
C++ control structurebluejayjunior
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
Mohammed Saleh
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Khan
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
 
Jumping statements
Jumping statementsJumping statements
Jumping statementsSuneel Dogra
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
JavvajiVenkat
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
Abou Bakr Ashraf
 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
nagalakshmig4
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
NaumanRasheed11
 
etlplooping-170320213203.pptx
etlplooping-170320213203.pptxetlplooping-170320213203.pptx
etlplooping-170320213203.pptx
ffyuyufyfufufufu
 
Introduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptxIntroduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptx
ofeliacanaria1
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
ShifatiRabbi
 

Similar to Chapter 3 (20)

Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Ch3 repetition
Ch3 repetitionCh3 repetition
Ch3 repetition
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
 
Control structures
Control structuresControl structures
Control structures
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
etlplooping-170320213203.pptx
etlplooping-170320213203.pptxetlplooping-170320213203.pptx
etlplooping-170320213203.pptx
 
Introduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptxIntroduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptx
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 

More from Amrit Kaur

File Organization
File OrganizationFile Organization
File Organization
Amrit Kaur
 
Introduction to transaction processing
Introduction to transaction processingIntroduction to transaction processing
Introduction to transaction processing
Amrit Kaur
 
ER diagram
ER diagramER diagram
ER diagram
Amrit Kaur
 
Transaction Processing
Transaction ProcessingTransaction Processing
Transaction Processing
Amrit Kaur
 
Normalization
NormalizationNormalization
Normalization
Amrit Kaur
 
Sample Interview Question
Sample Interview QuestionSample Interview Question
Sample Interview Question
Amrit Kaur
 
6. triggers
6. triggers6. triggers
6. triggers
Amrit Kaur
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
Amrit Kaur
 
4. plsql
4. plsql4. plsql
4. plsql
Amrit Kaur
 
3. ddl create
3. ddl create3. ddl create
3. ddl create
Amrit Kaur
 
2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATEAmrit Kaur
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
Amrit Kaur
 
Chapter 2: Conditional Construct in C++
Chapter 2: Conditional Construct in C++Chapter 2: Conditional Construct in C++
Chapter 2: Conditional Construct in C++
Amrit Kaur
 
C++ Tokens
C++ TokensC++ Tokens
C++ Tokens
Amrit Kaur
 
Lesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMSLesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMS
Amrit Kaur
 

More from Amrit Kaur (16)

File Organization
File OrganizationFile Organization
File Organization
 
Introduction to transaction processing
Introduction to transaction processingIntroduction to transaction processing
Introduction to transaction processing
 
ER diagram
ER diagramER diagram
ER diagram
 
Transaction Processing
Transaction ProcessingTransaction Processing
Transaction Processing
 
Normalization
NormalizationNormalization
Normalization
 
Sample Interview Question
Sample Interview QuestionSample Interview Question
Sample Interview Question
 
6. triggers
6. triggers6. triggers
6. triggers
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
4. plsql
4. plsql4. plsql
4. plsql
 
3. ddl create
3. ddl create3. ddl create
3. ddl create
 
2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE2. DML_INSERT_DELETE_UPDATE
2. DML_INSERT_DELETE_UPDATE
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Chapter 2: Conditional Construct in C++
Chapter 2: Conditional Construct in C++Chapter 2: Conditional Construct in C++
Chapter 2: Conditional Construct in C++
 
C++ Tokens
C++ TokensC++ Tokens
C++ Tokens
 
Lesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMSLesson 1: Introduction to DBMS
Lesson 1: Introduction to DBMS
 

Recently uploaded

Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 

Recently uploaded (20)

Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 

Chapter 3

  • 1. Amrit Kaur Looping Constructs in C++ 1 Chapter 3: Loop Constructs A loop construct allow set of statements of a program to be executed certain number of times. The statement will continue to execute until loop condition remains true. When the condition becomes false, loop ends and control is passes to the statement following loop. 3.1 The while Loop While loop continues until the evaluating condition becomes false. The condition should be a logical expression. Syntax initial value; while(logical_expression) { statement(s); increment; } Example 1: A program to print even numbers beween 0 to n where value of n is specified by user. #include<iostream.h> #include<conio.h> void main() { int n, i; clrscr(); cout<<"n *** Program to print EVEN series upto limit ***"; cout<<"nEnter Limit "; cin>>n; cout<<"nEven Number between 0 to "<< n <<endl; for(i=0;i<=n;i=i+2) //here i is loop variable { cout<<i<<" "; } } 3.2 The do...while Loop The do..while loop is similar to while loop as both executes until the logical expression becomes false. Syntax initial value; do { statement(s); increment;
  • 2. Amrit Kaur Looping Constructs in C++ 2 } while(logical_expression); Example 2 : A program to print even numbers beween 0 to n where value of n is specified by user. #include<iostream.h> #include<conio.h> void main() { int n, i; clrscr(); cout<<"n *** Program to print EVEN series upto limit ***"; cout<<"nEnter Limit "; cin>>n; cout<<"nEven Number between 0 to "<< n <<endl; //initalise loop variable i i=0; do //loop execute atleast onec { cout<<i<<" "; i=i+2; // increment } while(i<=n); // loop execute second time if condition is true } 3.3 Difference between while and do...while loop 1. In while loop, logical expression is evaluated first. If it evaluates to true the body of while loop will execute otherwise not. On the other hand, in do...while loop, the body of loop executes first and then the logical expression is evaluated. If the expression is true, next time the loop will execute. 2. In while loop, the body will execute only when the logical expression is true. On the other hand, in do...while loop body of loop will execute at least one time irrespective of logical expression.
  • 3. Amrit Kaur Looping Constructs in C++ 3 3.4 The for Loop The for loop is used to repeat the statement or set of statements known or specified number of times. The for statement consists of the keyword for followed by parenthesis containing three expressions, each separated by semicolon. The expressions are i. Initialisation expression: Used to initialise the loop variable. It is executed only once. ii. Test expression / condition: Used to evaluate the loop condition. It is executed each time control passes to beginning of loop. The loop will execute only if condition is true. iii. Increment or decrement expression: It is always executed when control return to beginning of the loop. Syntax for(initialisation; test_expression; increment) { statement(s); } Example 3: A program to print even numbers beween 0 to n where value of n is specified by user. #include<iostream.h> #include<conio.h> void main() { int n, i; clrscr(); cout<<"n *** Program to print EVEN series upto limit ***"; cout<<"nEnter Limit "; cin>>n; cout<<"nEven Number between 0 to "<< n <<endl; for(i=0;i<=n;i=i+2) //here i is loop variable { cout<<i<<" ";
  • 4. Amrit Kaur Looping Constructs in C++ 4 } } NOTE:  You can omit the Initialization Expression, but then the looping variable is initialized before the for loop  You can omit the test_expression / condition, but then the condition is specified inside the body of the for loop, generally using an if statement.  You can omit the increment, but then the increment expression is written inside the body of the for loop.  You can omit all three, but then the loop is called Infinite loop 3.5 The break Statements The break statement is used to exit the loop before the loop condition is re evaluated. Example 4: A program to print even numbers between 0 to n where value of n is specified by user and program terminates when i is greater than 10. #include<iostream.h> #include<conio.h> void main() { int n, i; clrscr(); cout<<"n *** Program to print EVEN series upto limit ***"; cout<<"nEnter Limit "; cin>>n; cout<<"nEven Number between 0 to "<< n <<endl; //initalise loop variable i i=0; do //loop execute atleast onec { cout<<i<<" "; i=i+2; // increment if (i==10) { break; } } while(i<=n); // loop execute second time if condition is true } 3.6 The continue statement
  • 5. Amrit Kaur Looping Constructs in C++ 5 The continue statements is used to skip all the subsequent / remaining instructions and take the control back to the loop (next increment/decrement value). Example 4: A program to print even numbers between 0 to n where value of n is specified by user and increments value of n by 10. #include<iostream.h> #include<conio.h> void main() { int n, i; clrscr(); cout<<"n *** Program to print EVEN series upto limit ***"; cout<<"nEnter Limit "; cin>>n; cout<<"nEven Number between 0 to "<< n <<endl; //initalise loop variable i i=0; do //loop execute atleast onec { cout<<i<<"n"; i=i+2; // increment if(i>20) { n=n+10; cout<<"nProgram continue to print even nos upto limit n="<<n<<endl; continue; } } while(i<=n); // loop execute second time if condition is true }