SlideShare a Scribd company logo
1 of 24
Slide Set –
13 CP
Conditional Control
Flow
• Conditional control flow is also referred to as conditional logicor
selection logic.
• It is one of the order in which the program instructions are executed.
• Its execution order differs from the sequential logic.
• It executes the instructions on the basis of one or more conditions.
• If the conditions are satisfied it will execute the instructions else
the instructions will not be executed (skipped).
Conditional Control
Flow
In conditional logic:
• Statements are executed on the basisof conditions.
• If conditions are satisfied the statements are executed.
• If conditions are not satisfied the statements are
skipped.
Conditional Control Structures in
C++
• Conditional control structures are used to execute set of statements on
the basis of one or more conditions.
• The statements that are needed to be executed conditionally are placed
with in the conditional structures and one or more conditions are
specified.
• The conditional control structures implement the
conditional/selection logic in C++.
if Statement in
C++
• if statement implements one-way selection logic.
• It executes the statement(s) on the basis of one
condition.
• If the condition is true, it executes if block.
• If the condition is false, it does not execute if block.
if Statement –
Syntax
if ( condition )
{
statement set
;
}
if ( condition
)
statement
;
if Statement –
Syntax
if Statement –
Syntax
• There are two syntaxes of if statement.
• In first syntax we have multiple statements inside the if statement.
• In this case it is compulsory to enclose all the statements in the braces
{ }
.
• In second syntax we have just one statement inside the if statement.
• In this case it is optional to enclose the statement in thebraces { } .
• All the statements enclosed in {} is called as the block.
if Statement – Flow
Chart
if Statement -
Example
Problem Statement 1: If the number is positive thendisplay
it.
if Statement -
Example
Problem Statement 2: If the number is multiple of 5 then add 1 to it
and display the resultant number.
If-else Statement in
C++
• If-else statementimplements two-way selection logic.
• It executes the statement(s) on the basis of one
condition.
• If the condition is true, it executes if block.
• If the condition is false, itexecutes else block.
If-else Statement –
Syntax
if ( condition )
{
statement set 1
;
}
else
{
statement set 2
;
if ( condition )
statement set 1
;
else
statement set 2
;
If-else Statement –
Syntax
If-else Statement –
Syntax
If-else Statement –
Syntax
• There are two syntaxes of if-else statement.
• In first syntax we have multiple statements inside the if-else statement.
• In this case it is compulsory to enclose all the statements in the braces
{ }
.
• In second syntax we have just one statement inside the if-else
statement.
• In this case it is optional to enclose the statement in thebraces { } .
If-else Statement – Flow
Chart
if-else Statement -
Example
Problem Statement 1: If the number is even, display “Number is even”
else display “Number is odd”.
switch Statement in
C++
• switch implements choice-way selection logic.
• It executes the statement(s) on the basis of expression and available choices.
• It first evaluates the expression and matches the value of expression to
available choices (cases).
• If the value matches with first case, then case 1 body is executed.
• If the value matches with second case, then case 2 body is executed.
• And the process continues up to nth case.
• If the value does not matches with any of the case, then default case
body is executed.
switch Statement –
Syntax
switch ( expression )
{
case 1 :
statement set 1
;
break ;
case 2 :
statement set 2
;
break ;
case 3 :
statement set 3
;
break ;
default :
switch Statement –
Syntax
switch Statement –
Syntax
• In switch statement, first the expression is evaluated to a single value.
• In expression we can only use the value of data types: char, short,int
and
long. No any other data types is accepted.
• We cannot use relational operation in switch statement.
• switch statement always contain one expression and multiple cases.
Where each case is ended by break statement.
• Last case is the default case (it is optional), it is executed when the
evaluated value does not match with any of thecase.
switch Statement – Flow
Chart
switch Statement -
Example
Problem Statement: Give remarks to the student according to the
marks he/she gets out of 5 in the sessionaltest.

More Related Content

Similar to IF & SWITCH (conditional control) in computer science

Similar to IF & SWITCH (conditional control) in computer science (20)

basics of python - control_structures_and_debugging.pptx
basics of python - control_structures_and_debugging.pptxbasics of python - control_structures_and_debugging.pptx
basics of python - control_structures_and_debugging.pptx
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7
 
Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
conditional.pptx
conditional.pptxconditional.pptx
conditional.pptx
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Conditional Statement-pptx-lesson3asdfsa
Conditional Statement-pptx-lesson3asdfsaConditional Statement-pptx-lesson3asdfsa
Conditional Statement-pptx-lesson3asdfsa
 
If statements in C
If statements in CIf statements in C
If statements in C
 
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
Selection
SelectionSelection
Selection
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Ch04
Ch04Ch04
Ch04
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
SWITCH CASE STATEMENT IN C
SWITCH CASE STATEMENT IN CSWITCH CASE STATEMENT IN C
SWITCH CASE STATEMENT IN C
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 

Recently uploaded

Film Coated Tablet and Film Coating raw materials.pdf
Film Coated Tablet and Film Coating raw materials.pdfFilm Coated Tablet and Film Coating raw materials.pdf
Film Coated Tablet and Film Coating raw materials.pdfPharmatech-rx
 
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...Sérgio Sacani
 
ERTHROPOIESIS: Dr. E. Muralinath & R. Gnana Lahari
ERTHROPOIESIS: Dr. E. Muralinath & R. Gnana LahariERTHROPOIESIS: Dr. E. Muralinath & R. Gnana Lahari
ERTHROPOIESIS: Dr. E. Muralinath & R. Gnana Laharimuralinath2
 
Isolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptxIsolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptxGOWTHAMIM22
 
Plasmapheresis - Dr. E. Muralinath - Kalyan . C.pptx
Plasmapheresis - Dr. E. Muralinath - Kalyan . C.pptxPlasmapheresis - Dr. E. Muralinath - Kalyan . C.pptx
Plasmapheresis - Dr. E. Muralinath - Kalyan . C.pptxmuralinath2
 
GBSN - Microbiology (Unit 6) Human and Microbial interaction
GBSN - Microbiology (Unit 6) Human and Microbial interactionGBSN - Microbiology (Unit 6) Human and Microbial interaction
GBSN - Microbiology (Unit 6) Human and Microbial interactionAreesha Ahmad
 
Erythropoiesis- Dr.E. Muralinath-C Kalyan
Erythropoiesis- Dr.E. Muralinath-C KalyanErythropoiesis- Dr.E. Muralinath-C Kalyan
Erythropoiesis- Dr.E. Muralinath-C Kalyanmuralinath2
 
Cell Immobilization Methods and Applications.pptx
Cell Immobilization Methods and Applications.pptxCell Immobilization Methods and Applications.pptx
Cell Immobilization Methods and Applications.pptxCherry
 
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...frank0071
 
Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Sérgio Sacani
 
The Scientific names of some important families of Industrial plants .pdf
The Scientific names of some important families of Industrial plants .pdfThe Scientific names of some important families of Industrial plants .pdf
The Scientific names of some important families of Industrial plants .pdfMohamed Said
 
MODERN PHYSICS_REPORTING_QUANTA_.....pdf
MODERN PHYSICS_REPORTING_QUANTA_.....pdfMODERN PHYSICS_REPORTING_QUANTA_.....pdf
MODERN PHYSICS_REPORTING_QUANTA_.....pdfRevenJadePalma
 
National Biodiversity protection initiatives and Convention on Biological Di...
National Biodiversity protection initiatives and  Convention on Biological Di...National Biodiversity protection initiatives and  Convention on Biological Di...
National Biodiversity protection initiatives and Convention on Biological Di...PABOLU TEJASREE
 
Land use land cover change analysis and detection of its drivers using geospa...
Land use land cover change analysis and detection of its drivers using geospa...Land use land cover change analysis and detection of its drivers using geospa...
Land use land cover change analysis and detection of its drivers using geospa...MohammedAhmed246550
 
INSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversityINSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversitySteffi Friedrichs
 
Continuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsContinuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsSérgio Sacani
 
Detectability of Solar Panels as a Technosignature
Detectability of Solar Panels as a TechnosignatureDetectability of Solar Panels as a Technosignature
Detectability of Solar Panels as a TechnosignatureSérgio Sacani
 
GBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday LifeGBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday LifeAreesha Ahmad
 
GBSN - Microbiology Lab 2 (Compound Microscope)
GBSN - Microbiology Lab 2 (Compound Microscope)GBSN - Microbiology Lab 2 (Compound Microscope)
GBSN - Microbiology Lab 2 (Compound Microscope)Areesha Ahmad
 
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdfPests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdfPirithiRaju
 

Recently uploaded (20)

Film Coated Tablet and Film Coating raw materials.pdf
Film Coated Tablet and Film Coating raw materials.pdfFilm Coated Tablet and Film Coating raw materials.pdf
Film Coated Tablet and Film Coating raw materials.pdf
 
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
Extensive Pollution of Uranus and Neptune’s Atmospheres by Upsweep of Icy Mat...
 
ERTHROPOIESIS: Dr. E. Muralinath & R. Gnana Lahari
ERTHROPOIESIS: Dr. E. Muralinath & R. Gnana LahariERTHROPOIESIS: Dr. E. Muralinath & R. Gnana Lahari
ERTHROPOIESIS: Dr. E. Muralinath & R. Gnana Lahari
 
Isolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptxIsolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptx
 
Plasmapheresis - Dr. E. Muralinath - Kalyan . C.pptx
Plasmapheresis - Dr. E. Muralinath - Kalyan . C.pptxPlasmapheresis - Dr. E. Muralinath - Kalyan . C.pptx
Plasmapheresis - Dr. E. Muralinath - Kalyan . C.pptx
 
GBSN - Microbiology (Unit 6) Human and Microbial interaction
GBSN - Microbiology (Unit 6) Human and Microbial interactionGBSN - Microbiology (Unit 6) Human and Microbial interaction
GBSN - Microbiology (Unit 6) Human and Microbial interaction
 
Erythropoiesis- Dr.E. Muralinath-C Kalyan
Erythropoiesis- Dr.E. Muralinath-C KalyanErythropoiesis- Dr.E. Muralinath-C Kalyan
Erythropoiesis- Dr.E. Muralinath-C Kalyan
 
Cell Immobilization Methods and Applications.pptx
Cell Immobilization Methods and Applications.pptxCell Immobilization Methods and Applications.pptx
Cell Immobilization Methods and Applications.pptx
 
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
Ostiguy & Panizza & Moffitt (eds.) - Populism in Global Perspective. A Perfor...
 
Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...
 
The Scientific names of some important families of Industrial plants .pdf
The Scientific names of some important families of Industrial plants .pdfThe Scientific names of some important families of Industrial plants .pdf
The Scientific names of some important families of Industrial plants .pdf
 
MODERN PHYSICS_REPORTING_QUANTA_.....pdf
MODERN PHYSICS_REPORTING_QUANTA_.....pdfMODERN PHYSICS_REPORTING_QUANTA_.....pdf
MODERN PHYSICS_REPORTING_QUANTA_.....pdf
 
National Biodiversity protection initiatives and Convention on Biological Di...
National Biodiversity protection initiatives and  Convention on Biological Di...National Biodiversity protection initiatives and  Convention on Biological Di...
National Biodiversity protection initiatives and Convention on Biological Di...
 
Land use land cover change analysis and detection of its drivers using geospa...
Land use land cover change analysis and detection of its drivers using geospa...Land use land cover change analysis and detection of its drivers using geospa...
Land use land cover change analysis and detection of its drivers using geospa...
 
INSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversityINSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere University
 
Continuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsContinuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discs
 
Detectability of Solar Panels as a Technosignature
Detectability of Solar Panels as a TechnosignatureDetectability of Solar Panels as a Technosignature
Detectability of Solar Panels as a Technosignature
 
GBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday LifeGBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday Life
 
GBSN - Microbiology Lab 2 (Compound Microscope)
GBSN - Microbiology Lab 2 (Compound Microscope)GBSN - Microbiology Lab 2 (Compound Microscope)
GBSN - Microbiology Lab 2 (Compound Microscope)
 
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdfPests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
Pests of Green Manures_Bionomics_IPM_Dr.UPR.pdf
 

IF & SWITCH (conditional control) in computer science

  • 2. Conditional Control Flow • Conditional control flow is also referred to as conditional logicor selection logic. • It is one of the order in which the program instructions are executed. • Its execution order differs from the sequential logic. • It executes the instructions on the basis of one or more conditions. • If the conditions are satisfied it will execute the instructions else the instructions will not be executed (skipped).
  • 3. Conditional Control Flow In conditional logic: • Statements are executed on the basisof conditions. • If conditions are satisfied the statements are executed. • If conditions are not satisfied the statements are skipped.
  • 4. Conditional Control Structures in C++ • Conditional control structures are used to execute set of statements on the basis of one or more conditions. • The statements that are needed to be executed conditionally are placed with in the conditional structures and one or more conditions are specified. • The conditional control structures implement the conditional/selection logic in C++.
  • 5. if Statement in C++ • if statement implements one-way selection logic. • It executes the statement(s) on the basis of one condition. • If the condition is true, it executes if block. • If the condition is false, it does not execute if block.
  • 6. if Statement – Syntax if ( condition ) { statement set ; } if ( condition ) statement ;
  • 8. if Statement – Syntax • There are two syntaxes of if statement. • In first syntax we have multiple statements inside the if statement. • In this case it is compulsory to enclose all the statements in the braces { } . • In second syntax we have just one statement inside the if statement. • In this case it is optional to enclose the statement in thebraces { } . • All the statements enclosed in {} is called as the block.
  • 9. if Statement – Flow Chart
  • 10. if Statement - Example Problem Statement 1: If the number is positive thendisplay it.
  • 11. if Statement - Example Problem Statement 2: If the number is multiple of 5 then add 1 to it and display the resultant number.
  • 12. If-else Statement in C++ • If-else statementimplements two-way selection logic. • It executes the statement(s) on the basis of one condition. • If the condition is true, it executes if block. • If the condition is false, itexecutes else block.
  • 13. If-else Statement – Syntax if ( condition ) { statement set 1 ; } else { statement set 2 ; if ( condition ) statement set 1 ; else statement set 2 ;
  • 16. If-else Statement – Syntax • There are two syntaxes of if-else statement. • In first syntax we have multiple statements inside the if-else statement. • In this case it is compulsory to enclose all the statements in the braces { } . • In second syntax we have just one statement inside the if-else statement. • In this case it is optional to enclose the statement in thebraces { } .
  • 17. If-else Statement – Flow Chart
  • 18. if-else Statement - Example Problem Statement 1: If the number is even, display “Number is even” else display “Number is odd”.
  • 19. switch Statement in C++ • switch implements choice-way selection logic. • It executes the statement(s) on the basis of expression and available choices. • It first evaluates the expression and matches the value of expression to available choices (cases). • If the value matches with first case, then case 1 body is executed. • If the value matches with second case, then case 2 body is executed. • And the process continues up to nth case. • If the value does not matches with any of the case, then default case body is executed.
  • 20. switch Statement – Syntax switch ( expression ) { case 1 : statement set 1 ; break ; case 2 : statement set 2 ; break ; case 3 : statement set 3 ; break ; default :
  • 22. switch Statement – Syntax • In switch statement, first the expression is evaluated to a single value. • In expression we can only use the value of data types: char, short,int and long. No any other data types is accepted. • We cannot use relational operation in switch statement. • switch statement always contain one expression and multiple cases. Where each case is ended by break statement. • Last case is the default case (it is optional), it is executed when the evaluated value does not match with any of thecase.
  • 23. switch Statement – Flow Chart
  • 24. switch Statement - Example Problem Statement: Give remarks to the student according to the marks he/she gets out of 5 in the sessionaltest.