SlideShare a Scribd company logo
Prepared By: Asst. Prof. Sejal Jadav
Unit-1
Principles of Object Oriented Programming Tokens,
expressions & Control Statements
(14 MARKS)
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
Prepared By: Asst. Prof. Sejal Jadav
Looping Control
Structure
Prepared By: Asst. Prof. Sejal Jadav
• There may be a situation, when you need to execute a
block of code several number of times.
• In general, statements are executed sequentially: The
first statement in a function is executed first, followed
by the second, and so on.
• Programming languages provide various control
structures that allow for more complicated execution
paths.
Prepared By: Asst. Prof. Sejal Jadav
• A loop statement allows us to execute a statement or
group of statements multiple times and following is
the general from of a loop statement in most of the
programming languages −
Prepared By: Asst. Prof. Sejal Jadav
• C++ programming language provides the following
type of loops to handle looping requirements.
1. for,
2. while,
3. do… while
Prepared By: Asst. Prof. Sejal Jadav
For Loop
•A for loop is a repetition control structure that
allows you to efficiently write a loop that needs
to execute a specific number of times.
Prepared By: Asst. Prof. Sejal Jadav
Syntax
The syntax of a for loop in C++ is −
for ( init; condition; increment/decrement )
{
statement(s);
}
Prepared By: Asst. Prof. Sejal Jadav
• Here is the flow of control in a for loop −
• The init step is executed first, and only once.
• This step allows you to declare and initialize any loop
control variables.
• Require semicolon (;).
Prepared By: Asst. Prof. Sejal Jadav
•Next, the condition is evaluated.
•If it is true, the body of the loop is executed.
•If it is false, the body of the loop does not execute
and flow of control jumps to the next statement
just after the for loop.
Prepared By: Asst. Prof. Sejal Jadav
•After the body of the for loop executes, the flow
of control jumps back up to
the increment statement.
Prepared By: Asst. Prof. Sejal Jadav
•The condition is now evaluated again.
•If it is true, the loop executes and the process
repeats itself (body of loop, then increment step,
and then again condition).
•After the condition becomes false, the for loop
terminates.
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Let’s see Examples: 7_For.cpp
7_forloop
Process
Prepared By: Asst. Prof. Sejal Jadav
• First initialize the variable
• In second step check condition
• In third step control goes inside loop body and
execute.
• At last increase the value of variable
• Same process is repeat until condition not false.
Prepared By: Asst. Prof. Sejal Jadav
While loop
• In while loop first check the condition if condition is
true then control goes inside the loop body
• Otherwise goes outside of the body. while loop will be
repeats in clock wise direction.
Prepared By: Asst. Prof. Sejal Jadav
Syntax
• The syntax of a while loop in C++ is -
while(condition)
{
statement(s);
}
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
C++
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Let’s look at the Example: 9_whileDemo.cpp
9_while.cpp
9_whileLoop.cpp
Prepared By: Asst. Prof. Sejal Jadav
Do...while loop
• The only difference between While loop and do-while
loop is do-while loop at least execute one time.
• No matter if the condition is true or false.
• This happen because the condition of the loop is at
the bottom of body of the loop so, it will execute the
body at least once.
Prepared By: Asst. Prof. Sejal Jadav
Syntax
do
{
statement(s);
}
while( condition );
Prepared By: Asst. Prof. Sejal Jadav
• If the condition is true, the flow of control jumps back
up to do, and the statement(s) in the loop execute
again.
• This process repeats until the given condition becomes
false.
Prepared By: Asst. Prof. Sejal Jadav
Example: do_while.cpp
Example: dowhile.cpp
Prepared By: Asst. Prof. Sejal Jadav
• A loop becomes infinite loop if a condition never
becomes false.
• The for loop is traditionally used for this purpose.
• Since none of the three expressions that form the ‘for’
loop are required, you can make an endless loop by
leaving the conditional expression empty.
Infinite
Prepared By: Asst. Prof. Sejal Jadav
Example: 6_infinite.cpp
#include <iostream>
using namespace std;
int main ()
{
for( ; ; )
{
printf("This loop will run forever.n");
}
return 0; }
Prepared By: Asst. Prof. Sejal Jadav
• When the conditional expression is absent, it is
assumed to be true.
• You may have an initialization and increment
expression, but C++ programmers more commonly use
the ‘for (;;)’ construct to signify an infinite loop.
• NOTE − You can terminate an infinite loop by pressing
Ctrl + C keys.

More Related Content

Similar to C++ unit-1-part-14

Loop structures
Loop structuresLoop structures
Loop structures
tazeem sana
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
Abdii Rashid
 
Week04
Week04Week04
Week04hccit
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
Emroz Sardar
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
nagalakshmig4
 
Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
FarshidKhan
 
Repetations in C
Repetations in CRepetations in C
Repetations in C
yndaravind
 
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
muhammadFaheem656405
 
Conditional statements & Loops
Conditional statements & LoopsConditional statements & Loops
Conditional statements & Loops
saifullahbhatti99
 
While loop and for loop 06 (js)
While loop and for loop 06 (js)While loop and for loop 06 (js)
While loop and for loop 06 (js)
AbhishekMondal42
 
Iteration Statement in C++
Iteration Statement in C++Iteration Statement in C++
Iteration Statement in C++
Jaypee Institute of Information Technology
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
BasirKhan21
 
cpu.pdf
cpu.pdfcpu.pdf
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
Prasanna Kumar SM
 
While loop
While loopWhile loop
While loop
Feras_83
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
JavvajiVenkat
 
22H51A6755.pptx
22H51A6755.pptx22H51A6755.pptx
22H51A6755.pptx
Parameshwar Maddela
 

Similar to C++ unit-1-part-14 (20)

Loop structures
Loop structuresLoop structures
Loop structures
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Week04
Week04Week04
Week04
 
Loops in c
Loops in cLoops in c
Loops in c
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
 
Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
 
Repetations in C
Repetations in CRepetations in C
Repetations in C
 
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
 
Conditional statements & Loops
Conditional statements & LoopsConditional statements & Loops
Conditional statements & Loops
 
While loop and for loop 06 (js)
While loop and for loop 06 (js)While loop and for loop 06 (js)
While loop and for loop 06 (js)
 
Iteration Statement in C++
Iteration Statement in C++Iteration Statement in C++
Iteration Statement in C++
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
While loop
While loopWhile loop
While loop
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
22H51A6755.pptx
22H51A6755.pptx22H51A6755.pptx
22H51A6755.pptx
 

More from Jadavsejal

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone Class
Jadavsejal
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event Handling
Jadavsejal
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event Handling
Jadavsejal
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout Managers
Jadavsejal
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3
Jadavsejal
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2
Jadavsejal
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1
Jadavsejal
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13
Jadavsejal
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12
Jadavsejal
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
Jadavsejal
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
Jadavsejal
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9
Jadavsejal
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8
Jadavsejal
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7
Jadavsejal
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
Jadavsejal
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5
Jadavsejal
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4
Jadavsejal
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2
Jadavsejal
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3
Jadavsejal
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1
Jadavsejal
 

More from Jadavsejal (20)

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone Class
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event Handling
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event Handling
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout Managers
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1
 

Recently uploaded

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
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
 
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
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
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 geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
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 ...
 
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
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
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 geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 

C++ unit-1-part-14

  • 1. Prepared By: Asst. Prof. Sejal Jadav Unit-1 Principles of Object Oriented Programming Tokens, expressions & Control Statements (14 MARKS) B.C.A & B.Sc.(IT) – 3 CS-13 C++ and Object Oriented Programming
  • 2. Prepared By: Asst. Prof. Sejal Jadav Looping Control Structure
  • 3. Prepared By: Asst. Prof. Sejal Jadav • There may be a situation, when you need to execute a block of code several number of times. • In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. • Programming languages provide various control structures that allow for more complicated execution paths.
  • 4. Prepared By: Asst. Prof. Sejal Jadav • A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages −
  • 5. Prepared By: Asst. Prof. Sejal Jadav • C++ programming language provides the following type of loops to handle looping requirements. 1. for, 2. while, 3. do… while
  • 6. Prepared By: Asst. Prof. Sejal Jadav For Loop •A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
  • 7. Prepared By: Asst. Prof. Sejal Jadav Syntax The syntax of a for loop in C++ is − for ( init; condition; increment/decrement ) { statement(s); }
  • 8. Prepared By: Asst. Prof. Sejal Jadav • Here is the flow of control in a for loop − • The init step is executed first, and only once. • This step allows you to declare and initialize any loop control variables. • Require semicolon (;).
  • 9. Prepared By: Asst. Prof. Sejal Jadav •Next, the condition is evaluated. •If it is true, the body of the loop is executed. •If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
  • 10. Prepared By: Asst. Prof. Sejal Jadav •After the body of the for loop executes, the flow of control jumps back up to the increment statement.
  • 11. Prepared By: Asst. Prof. Sejal Jadav •The condition is now evaluated again. •If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). •After the condition becomes false, the for loop terminates.
  • 12. Prepared By: Asst. Prof. Sejal Jadav
  • 13. Prepared By: Asst. Prof. Sejal Jadav Let’s see Examples: 7_For.cpp 7_forloop Process
  • 14. Prepared By: Asst. Prof. Sejal Jadav • First initialize the variable • In second step check condition • In third step control goes inside loop body and execute. • At last increase the value of variable • Same process is repeat until condition not false.
  • 15. Prepared By: Asst. Prof. Sejal Jadav While loop • In while loop first check the condition if condition is true then control goes inside the loop body • Otherwise goes outside of the body. while loop will be repeats in clock wise direction.
  • 16. Prepared By: Asst. Prof. Sejal Jadav Syntax • The syntax of a while loop in C++ is - while(condition) { statement(s); }
  • 17. Prepared By: Asst. Prof. Sejal Jadav
  • 18. Prepared By: Asst. Prof. Sejal Jadav
  • 19. Prepared By: Asst. Prof. Sejal Jadav
  • 20. Prepared By: Asst. Prof. Sejal Jadav C++
  • 21. Prepared By: Asst. Prof. Sejal Jadav
  • 22. Prepared By: Asst. Prof. Sejal Jadav
  • 23. Prepared By: Asst. Prof. Sejal Jadav
  • 24. Prepared By: Asst. Prof. Sejal Jadav
  • 25. Prepared By: Asst. Prof. Sejal Jadav
  • 26. Prepared By: Asst. Prof. Sejal Jadav
  • 27. Prepared By: Asst. Prof. Sejal Jadav Let’s look at the Example: 9_whileDemo.cpp 9_while.cpp 9_whileLoop.cpp
  • 28. Prepared By: Asst. Prof. Sejal Jadav Do...while loop • The only difference between While loop and do-while loop is do-while loop at least execute one time. • No matter if the condition is true or false. • This happen because the condition of the loop is at the bottom of body of the loop so, it will execute the body at least once.
  • 29. Prepared By: Asst. Prof. Sejal Jadav Syntax do { statement(s); } while( condition );
  • 30. Prepared By: Asst. Prof. Sejal Jadav • If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. • This process repeats until the given condition becomes false.
  • 31. Prepared By: Asst. Prof. Sejal Jadav Example: do_while.cpp Example: dowhile.cpp
  • 32. Prepared By: Asst. Prof. Sejal Jadav • A loop becomes infinite loop if a condition never becomes false. • The for loop is traditionally used for this purpose. • Since none of the three expressions that form the ‘for’ loop are required, you can make an endless loop by leaving the conditional expression empty. Infinite
  • 33. Prepared By: Asst. Prof. Sejal Jadav Example: 6_infinite.cpp #include <iostream> using namespace std; int main () { for( ; ; ) { printf("This loop will run forever.n"); } return 0; }
  • 34. Prepared By: Asst. Prof. Sejal Jadav • When the conditional expression is absent, it is assumed to be true. • You may have an initialization and increment expression, but C++ programmers more commonly use the ‘for (;;)’ construct to signify an infinite loop. • NOTE − You can terminate an infinite loop by pressing Ctrl + C keys.