SlideShare a Scribd company logo
1 of 34
C++ Language
By : Shrirang Pinjarkar
Email : shrirang.pinjarkar@gmail.com
UNIT -4
CONTROL
STRUCTURE
Control Structures
Programs are written using three basic structures
 Sequence
 a sequence is a series of statements that execute
one after another
 Repetition(loop or iteration)
 repetition (looping) is used to repeat statements
while certain conditions are met.
 Selection(branching)
 selection (branch) is used to execute different
statements depending on certain conditions
Called control structures or logic structures
Sequence
The sequence
structure directs the
computer to process
the program
instructions, one after
another, in the order
listed in the program
Selection
(Branch)
Selection
structure: makes
a decision and
then takes an
appropriate action
based on that
decision
 Also called the
decision
structure
Repetition (Loop)
Repetition
structure: directs
computer to repeat
one or more
instructions until
some condition is
met
 Also called a loop or
iteration
Normal flow
Statement 1;
Statement 2;
Statement 3;
Statement 4;
Conditional Statements
if
if else
nested if (if – else if – else if – else)
statement blocks ({…})
(goto)
switch (case, default, break)
if
if(condition)statement
if(x==100)
cout<<“x is 100”;
 If the condition is true, statement is
executed.
 If the condition is false, statement is not
executed.
if flow
Condititional Statementif(…)
true
false
if else
 if (condition)statement1
else statement2
if(x==100)
cout<<“x is 100”;
else
cout<<“x is not 100”;
 If the statement1 is true, then print out on the
screen x is 100
 If the statement2 is true, then print out on the
screen x is not 100
if else flow
Condititional Statementif(…)
else Statement
true
false
Statement Blocks
 If we want more than a single instruction is
executed, we must group them in a in a block
of statements by using curly brackets ({…})
if (x>0)
{
cout<<“x is positive”;
}
else
{
cout<<“x is negative”;
}
Nested if statements
 When if statement occurs with in another if statement,
then such type of if statement is called nested if
statement.
 if (condition1)
{
if (condition2)
statement-1;
else
statement-2;
}
else
statement-3;
Nested if statements
if(a>=b && a>=c)
cout<<“a is biggest”;
else if(b>=a && b>=c)
cout<<“b is biggest”;
else
cout<<“c is biggest”;
Nested if flow
Condititional Statement 2else if
else if Condititional Statement 3
Else Statement
Condititional Statement 1if true
true
true
false
false
false
Switch Statement
more structured
 Structure is good - less confusing
 Its objective is to check several possible
constant values for an expression and
Similar to if-elseif-elseif-else but a little
simpler.
Switch flow
Condititional Statement 2case 2
case 3 Condititional Statement 3
Condititional Statement 1case 1
switch
Switch statement
switch(variable)
{
case constant1:
block of instructions 1
break;
case constant2:
block of instructions 2
break;
default:
default block of instructions
}
Switch statement
 Switch evaluates expression and checks if it is
equivalent to constant1, if it is, it executes block of
instructions 1 until it finds the break keyword, then the
program will jump to the end of the switch selective
structure.
 If expression was not equivalent to constant1, it will
check if expression is equivalent to constant2. if it is, it
executes block of instructions 2 until it finds the break
keyword.
 Finally if the value of expression has not matched any
of the specified constants, the program will execute the
instructions included in the default: section, if this one
exists, since it is optional.
Switch statement
switch(x) {
case 1:
cout<<“x is 1”;
break;
case 2:
cout<<“x is 2”;
break;
default:
cout<<“value of x is unknown”;
}
For example ,
Loops
 Control of loop is divided into two parts:
 Entry control loop- in this first of all condition is
checked if it is true then body of the loop is executed.
Otherwise we can exit from the loop when the
condition becomes false. Entry control loop is also
called base loop. Example- While loop and for loop
 Exit control loop- in this first body is executed and
then condition is checked. If condition is true, then
again body of loop is executed. If condition is false,
then control will move out from the loop. Exit control
loop is also called Derived loop. Example- Do-while
Loops
The loops in statements in C++
language are-
While loop
Do loop/Do-while loop
For loop
Nested for loop
While Loop
 while (expression) statement
 While loop is an Entry control loop
 And its function is simply to repeat statement while
expression is true.
Condititional Statementswhile(…)
true
false
While loop
#include<iostream.h>
int main()
{
int n;
cout<<“Enter the starting number :”;
cin>>n;
while(n>0){
cout<<n<<“,”;
n--;
}
cout << “nFIRE!“;
return 0;
}
OUTPUT
Enter the starting number :5
5,4,3,2,1
FIRE!
For loop
For loop is an Entry control loop when action is to be
repeated for a predetermined number of times.
Most used – most complicated
Normally used for counting
Four parts
 Initialise expression
 Test expression
 Body
 Increment expression
for(initialization ;condition ;increment)
{body of loop}
for loop
body
statements
continuation
test
initialisation
increment
true
false
For loop
int main()
{
int count;
for (count=1; count<=5; count++)
{
cout <<count<<“,”;
}
cout<<“FIRE”;
return 0;
}
OUTPUT
1,2,3,4,5FIRE
Do-While Loop
do statement while (expression)
Do-while is an exit control loop. Based on a
condition, the control is transferred back to a
particular point in the program.
 Similar to the while loop except that condition
in the do while is check is at end of loop not
the start
do
{
statements;
}
while (condition is true);
Do Flow
condititional statements
while(…)
true
false
do
Nesting of Loops
A loop can be inside another loop. C++
can have at least 256 levels of nesting.
for(int;condition;increment)
{
for(int;condition;increment)
{
statement(s);
}
statement(s);
}
Break Statement
 Goes straight to the end of a do, while or for
loop or a switch statement block,
https://www.facebook.com/AniLK0
221
#include<iostream.h>
int main(){
int n;
for(n=10;n>0;n--)
{ cout<<n<<“,”;
if(n==5)
{cout<<“count down aborted!”;
break;}
}
return 0;}
O/P: 10,9,8,7,6,5,count down
aborted!
Continue Statement
 Goes straight back to the start of a do, while
or for loop,
#include<iostream.h>
int main()
{
int n;
for(n=10;n>0;n--){
if(n==5)continue;
cout<<n<<“,”;}
cout << “FIRE!“;
return 0;}
O/P: 10,9,8,7,6,4,3,2,1,FIRE!
THANK
YOU

More Related Content

What's hot (20)

Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Exception handling in python
Exception handling in pythonException handling in python
Exception handling in python
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Data types
Data typesData types
Data types
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in Python
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Architecture of .net framework
Architecture of .net frameworkArchitecture of .net framework
Architecture of .net framework
 

Viewers also liked

Chapter 4 decision making
Chapter   4 decision makingChapter   4 decision making
Chapter 4 decision makingAjay Ardeshana
 
CPA Certification Program Review
CPA Certification Program ReviewCPA Certification Program Review
CPA Certification Program ReviewEric Hill
 
ACCA Geography Bee (Grades 4-6)
ACCA Geography Bee (Grades 4-6)ACCA Geography Bee (Grades 4-6)
ACCA Geography Bee (Grades 4-6)accaknights
 
Does Your Website Stack Up? CPA Website Findings Revealed
Does Your Website Stack Up? CPA Website Findings RevealedDoes Your Website Stack Up? CPA Website Findings Revealed
Does Your Website Stack Up? CPA Website Findings RevealedSkoda Minotti
 
Ethiopian GTP- Macroeconomic policy and strategy review
Ethiopian GTP- Macroeconomic policy and strategy reviewEthiopian GTP- Macroeconomic policy and strategy review
Ethiopian GTP- Macroeconomic policy and strategy reviewAsmamaw Amare
 
Transitioning from reaching every district to reaching every community
Transitioning from reaching every district to reaching every communityTransitioning from reaching every district to reaching every community
Transitioning from reaching every district to reaching every communityJSI
 
The Economy under President Obama
The Economy under President ObamaThe Economy under President Obama
The Economy under President ObamaDavid Doney
 
Online PMES - Concept of POSSESSION and OWNERSHIP
Online PMES - Concept of POSSESSION and OWNERSHIPOnline PMES - Concept of POSSESSION and OWNERSHIP
Online PMES - Concept of POSSESSION and OWNERSHIPBLP Cooperative
 
Federal ministry of land, housing & urban development
Federal ministry of land, housing & urban developmentFederal ministry of land, housing & urban development
Federal ministry of land, housing & urban developmentOtoide Ayemere
 
Mulu Gebreeyesus: Industrial Policy and Development in Ethiopia: Evolution an...
Mulu Gebreeyesus: Industrial Policy and Development in Ethiopia: Evolution an...Mulu Gebreeyesus: Industrial Policy and Development in Ethiopia: Evolution an...
Mulu Gebreeyesus: Industrial Policy and Development in Ethiopia: Evolution an...UNU-WIDER
 
ACCA F9 - exam preparation for Dec 2014
ACCA F9 - exam preparation for Dec 2014ACCA F9 - exam preparation for Dec 2014
ACCA F9 - exam preparation for Dec 2014cert-easy
 
Paul Lydon Notes Acca F9
Paul Lydon Notes Acca F9Paul Lydon Notes Acca F9
Paul Lydon Notes Acca F9IBAT College
 
Rural-Urban Transformation in Ethiopia - Implications for Development Strategies
Rural-Urban Transformation in Ethiopia - Implications for Development StrategiesRural-Urban Transformation in Ethiopia - Implications for Development Strategies
Rural-Urban Transformation in Ethiopia - Implications for Development Strategiesessp2
 

Viewers also liked (20)

C++ chapter 2
C++ chapter 2C++ chapter 2
C++ chapter 2
 
C++ Chapter 3
C++ Chapter 3C++ Chapter 3
C++ Chapter 3
 
C++ Chapter 1
C++ Chapter 1C++ Chapter 1
C++ Chapter 1
 
Chapter 4 decision making
Chapter   4 decision makingChapter   4 decision making
Chapter 4 decision making
 
Achieving acca approved learning partner (gold status
Achieving acca approved learning partner (gold statusAchieving acca approved learning partner (gold status
Achieving acca approved learning partner (gold status
 
CPA Certification Program Review
CPA Certification Program ReviewCPA Certification Program Review
CPA Certification Program Review
 
ACCA Geography Bee (Grades 4-6)
ACCA Geography Bee (Grades 4-6)ACCA Geography Bee (Grades 4-6)
ACCA Geography Bee (Grades 4-6)
 
Does Your Website Stack Up? CPA Website Findings Revealed
Does Your Website Stack Up? CPA Website Findings RevealedDoes Your Website Stack Up? CPA Website Findings Revealed
Does Your Website Stack Up? CPA Website Findings Revealed
 
Ethiopian GTP- Macroeconomic policy and strategy review
Ethiopian GTP- Macroeconomic policy and strategy reviewEthiopian GTP- Macroeconomic policy and strategy review
Ethiopian GTP- Macroeconomic policy and strategy review
 
Transitioning from reaching every district to reaching every community
Transitioning from reaching every district to reaching every communityTransitioning from reaching every district to reaching every community
Transitioning from reaching every district to reaching every community
 
ACCA Presentation
ACCA PresentationACCA Presentation
ACCA Presentation
 
The Economy under President Obama
The Economy under President ObamaThe Economy under President Obama
The Economy under President Obama
 
Online PMES - Concept of POSSESSION and OWNERSHIP
Online PMES - Concept of POSSESSION and OWNERSHIPOnline PMES - Concept of POSSESSION and OWNERSHIP
Online PMES - Concept of POSSESSION and OWNERSHIP
 
Federal ministry of land, housing & urban development
Federal ministry of land, housing & urban developmentFederal ministry of land, housing & urban development
Federal ministry of land, housing & urban development
 
Mulu Gebreeyesus: Industrial Policy and Development in Ethiopia: Evolution an...
Mulu Gebreeyesus: Industrial Policy and Development in Ethiopia: Evolution an...Mulu Gebreeyesus: Industrial Policy and Development in Ethiopia: Evolution an...
Mulu Gebreeyesus: Industrial Policy and Development in Ethiopia: Evolution an...
 
ACCA F9 - exam preparation for Dec 2014
ACCA F9 - exam preparation for Dec 2014ACCA F9 - exam preparation for Dec 2014
ACCA F9 - exam preparation for Dec 2014
 
Urban And Regional Planning And Development Law
Urban And Regional Planning And Development LawUrban And Regional Planning And Development Law
Urban And Regional Planning And Development Law
 
Paul Lydon Notes Acca F9
Paul Lydon Notes Acca F9Paul Lydon Notes Acca F9
Paul Lydon Notes Acca F9
 
Rural-Urban Transformation in Ethiopia - Implications for Development Strategies
Rural-Urban Transformation in Ethiopia - Implications for Development StrategiesRural-Urban Transformation in Ethiopia - Implications for Development Strategies
Rural-Urban Transformation in Ethiopia - Implications for Development Strategies
 
Turbo c++
Turbo c++Turbo c++
Turbo c++
 

Similar to C++ chapter 4

Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.pptManojKhadilkar1
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Control structures ii
Control structures ii Control structures ii
Control structures ii Ahmad Idrees
 
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 notesmuhammadFaheem656405
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsRai University
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsRai University
 
NRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptxNRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptxKRIPABHARDWAJ1
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statementsRai University
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxSKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxLECO9
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statementsRai University
 

Similar to C++ chapter 4 (20)

Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Control statements
Control statementsControl statements
Control statements
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
Control statements
Control statementsControl statements
Control statements
 
07 flow control
07   flow control07   flow control
07 flow control
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
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
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Flow of control
Flow of controlFlow of control
Flow of control
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
 
NRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptxNRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptx
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
 

Recently uploaded

Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 

Recently uploaded (20)

Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 

C++ chapter 4

  • 1. C++ Language By : Shrirang Pinjarkar Email : shrirang.pinjarkar@gmail.com
  • 3. Control Structures Programs are written using three basic structures  Sequence  a sequence is a series of statements that execute one after another  Repetition(loop or iteration)  repetition (looping) is used to repeat statements while certain conditions are met.  Selection(branching)  selection (branch) is used to execute different statements depending on certain conditions Called control structures or logic structures
  • 4. Sequence The sequence structure directs the computer to process the program instructions, one after another, in the order listed in the program
  • 5. Selection (Branch) Selection structure: makes a decision and then takes an appropriate action based on that decision  Also called the decision structure
  • 6. Repetition (Loop) Repetition structure: directs computer to repeat one or more instructions until some condition is met  Also called a loop or iteration
  • 7. Normal flow Statement 1; Statement 2; Statement 3; Statement 4;
  • 8. Conditional Statements if if else nested if (if – else if – else if – else) statement blocks ({…}) (goto) switch (case, default, break)
  • 9. if if(condition)statement if(x==100) cout<<“x is 100”;  If the condition is true, statement is executed.  If the condition is false, statement is not executed.
  • 11. if else  if (condition)statement1 else statement2 if(x==100) cout<<“x is 100”; else cout<<“x is not 100”;  If the statement1 is true, then print out on the screen x is 100  If the statement2 is true, then print out on the screen x is not 100
  • 12. if else flow Condititional Statementif(…) else Statement true false
  • 13. Statement Blocks  If we want more than a single instruction is executed, we must group them in a in a block of statements by using curly brackets ({…}) if (x>0) { cout<<“x is positive”; } else { cout<<“x is negative”; }
  • 14. Nested if statements  When if statement occurs with in another if statement, then such type of if statement is called nested if statement.  if (condition1) { if (condition2) statement-1; else statement-2; } else statement-3;
  • 15. Nested if statements if(a>=b && a>=c) cout<<“a is biggest”; else if(b>=a && b>=c) cout<<“b is biggest”; else cout<<“c is biggest”;
  • 16. Nested if flow Condititional Statement 2else if else if Condititional Statement 3 Else Statement Condititional Statement 1if true true true false false false
  • 17. Switch Statement more structured  Structure is good - less confusing  Its objective is to check several possible constant values for an expression and Similar to if-elseif-elseif-else but a little simpler.
  • 18. Switch flow Condititional Statement 2case 2 case 3 Condititional Statement 3 Condititional Statement 1case 1 switch
  • 19. Switch statement switch(variable) { case constant1: block of instructions 1 break; case constant2: block of instructions 2 break; default: default block of instructions }
  • 20. Switch statement  Switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes block of instructions 1 until it finds the break keyword, then the program will jump to the end of the switch selective structure.  If expression was not equivalent to constant1, it will check if expression is equivalent to constant2. if it is, it executes block of instructions 2 until it finds the break keyword.  Finally if the value of expression has not matched any of the specified constants, the program will execute the instructions included in the default: section, if this one exists, since it is optional.
  • 21. Switch statement switch(x) { case 1: cout<<“x is 1”; break; case 2: cout<<“x is 2”; break; default: cout<<“value of x is unknown”; } For example ,
  • 22. Loops  Control of loop is divided into two parts:  Entry control loop- in this first of all condition is checked if it is true then body of the loop is executed. Otherwise we can exit from the loop when the condition becomes false. Entry control loop is also called base loop. Example- While loop and for loop  Exit control loop- in this first body is executed and then condition is checked. If condition is true, then again body of loop is executed. If condition is false, then control will move out from the loop. Exit control loop is also called Derived loop. Example- Do-while
  • 23. Loops The loops in statements in C++ language are- While loop Do loop/Do-while loop For loop Nested for loop
  • 24. While Loop  while (expression) statement  While loop is an Entry control loop  And its function is simply to repeat statement while expression is true. Condititional Statementswhile(…) true false
  • 25. While loop #include<iostream.h> int main() { int n; cout<<“Enter the starting number :”; cin>>n; while(n>0){ cout<<n<<“,”; n--; } cout << “nFIRE!“; return 0; } OUTPUT Enter the starting number :5 5,4,3,2,1 FIRE!
  • 26. For loop For loop is an Entry control loop when action is to be repeated for a predetermined number of times. Most used – most complicated Normally used for counting Four parts  Initialise expression  Test expression  Body  Increment expression for(initialization ;condition ;increment) {body of loop}
  • 28. For loop int main() { int count; for (count=1; count<=5; count++) { cout <<count<<“,”; } cout<<“FIRE”; return 0; } OUTPUT 1,2,3,4,5FIRE
  • 29. Do-While Loop do statement while (expression) Do-while is an exit control loop. Based on a condition, the control is transferred back to a particular point in the program.  Similar to the while loop except that condition in the do while is check is at end of loop not the start do { statements; } while (condition is true);
  • 31. Nesting of Loops A loop can be inside another loop. C++ can have at least 256 levels of nesting. for(int;condition;increment) { for(int;condition;increment) { statement(s); } statement(s); }
  • 32. Break Statement  Goes straight to the end of a do, while or for loop or a switch statement block, https://www.facebook.com/AniLK0 221 #include<iostream.h> int main(){ int n; for(n=10;n>0;n--) { cout<<n<<“,”; if(n==5) {cout<<“count down aborted!”; break;} } return 0;} O/P: 10,9,8,7,6,5,count down aborted!
  • 33. Continue Statement  Goes straight back to the start of a do, while or for loop, #include<iostream.h> int main() { int n; for(n=10;n>0;n--){ if(n==5)continue; cout<<n<<“,”;} cout << “FIRE!“; return 0;} O/P: 10,9,8,7,6,4,3,2,1,FIRE!