SlideShare a Scribd company logo
1 of 10
Decision Making
What you’ll learn:
1. If else
2. Ternary Operators
3. Switch case
Flow of a Program
Start
set of instructions
…
set of instructions
Stop
 Program executes
instructions sequentially.
 Normal flow of a program
shows a single path.
Control Statements
Types of control statements:
 Decision Making Statements
 Iterative Statements
 Jump Statements
What if you want to have multiple paths?
Start Path - 1
Path - 3
Stop
Path – 1
Path - 3
Path – 2
And need to choose one among them based on some condition?
Path Some Condition
The Solution: Decision Making
 Deciding the order or flow of execution of statements is called decision making
 Decision making statements in C are:
 If – else
 Ternary Operator
 Switch
If – else
Syntax:
if (expression) {
…block of statement(s)…
}
else {
…block of statement(s)…
}
expression
instructions instructions
True False
Nested if - else
Syntax:
if (expression1) {
…block of statement(s)…
}
else {
if (expression2) {
…block of statement(s)…
}
else {
if (expression3) {
…block of statement(s)…
}
else {
…block of statement(s)…
}
}
}
exp-1
exp-2
instructions instructions instructions
True
TrueFalse
False
else-if ladder
Syntax:
if (expression1) {
…block of statement(s)…
}
else if (expression2) {
…block of statement(s)…
}
else if (expression3) {
…block of statement(s)…
}
….
….
else {
…block of statement(s)…
}
exp-1
…
exp-n
True
True
True
False
False
False
(else)
Set of instructions
Set of instructions
Set of instructions
Set of instructions
Set of instructions
Switch-case
Syntax:
switch (case_expression)
{
case constant_expression1 :
…block of code…
break;
case constant_expression2 :
…block of code…
break;
case constant_expression3 :
…block of code…
break;
…
default : … block of code…
}
exp-1
…
Default
True
True
True
False
False
False
Set of instructions
Set of instructions
Set of instructions
Set of instructions
Ternary Operator
Syntax:
test_condition ? statement1 :
statement2 ;
Equivalent form in if-else
construct:
if (test_consition)
statement1;
else
statement2;
expression
instruction instruction
True False

More Related Content

Similar to 2.decision making

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
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
Introduction to flowchart
Introduction to flowchartIntroduction to flowchart
Introduction to flowchartJordan Delacruz
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
Chapter 2 - Flow of Control Part I.pdf
Chapter 2 -  Flow of Control Part I.pdfChapter 2 -  Flow of Control Part I.pdf
Chapter 2 - Flow of Control Part I.pdfKirubelWondwoson1
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlENGWAU TONNY
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c languagechintupro9
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Selection statements
Selection statementsSelection statements
Selection statementsHarsh Dabas
 
Introductiontoflowchart 110630082600-phpapp01
Introductiontoflowchart 110630082600-phpapp01Introductiontoflowchart 110630082600-phpapp01
Introductiontoflowchart 110630082600-phpapp01VincentAcapen1
 
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
 
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
 
Decision makingandbranching in c
Decision makingandbranching in cDecision makingandbranching in c
Decision makingandbranching in cNMahendiran
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptxssuserfb3c3e
 
Control structures i
Control structures i Control structures i
Control structures i Ahmad Idrees
 

Similar to 2.decision making (20)

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
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Introduction to flowchart
Introduction to flowchartIntroduction to flowchart
Introduction to flowchart
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Chapter 2 - Flow of Control Part I.pdf
Chapter 2 -  Flow of Control Part I.pdfChapter 2 -  Flow of Control Part I.pdf
Chapter 2 - Flow of Control Part I.pdf
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Selection statements
Selection statementsSelection statements
Selection statements
 
Introductiontoflowchart 110630082600-phpapp01
Introductiontoflowchart 110630082600-phpapp01Introductiontoflowchart 110630082600-phpapp01
Introductiontoflowchart 110630082600-phpapp01
 
Ch04
Ch04Ch04
Ch04
 
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
 
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
 
Decision makingandbranching in c
Decision makingandbranching in cDecision makingandbranching in c
Decision makingandbranching in c
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Control structures i
Control structures i Control structures i
Control structures i
 

2.decision making

  • 1. Decision Making What you’ll learn: 1. If else 2. Ternary Operators 3. Switch case
  • 2. Flow of a Program Start set of instructions … set of instructions Stop  Program executes instructions sequentially.  Normal flow of a program shows a single path.
  • 3. Control Statements Types of control statements:  Decision Making Statements  Iterative Statements  Jump Statements
  • 4. What if you want to have multiple paths? Start Path - 1 Path - 3 Stop Path – 1 Path - 3 Path – 2 And need to choose one among them based on some condition? Path Some Condition
  • 5. The Solution: Decision Making  Deciding the order or flow of execution of statements is called decision making  Decision making statements in C are:  If – else  Ternary Operator  Switch
  • 6. If – else Syntax: if (expression) { …block of statement(s)… } else { …block of statement(s)… } expression instructions instructions True False
  • 7. Nested if - else Syntax: if (expression1) { …block of statement(s)… } else { if (expression2) { …block of statement(s)… } else { if (expression3) { …block of statement(s)… } else { …block of statement(s)… } } } exp-1 exp-2 instructions instructions instructions True TrueFalse False
  • 8. else-if ladder Syntax: if (expression1) { …block of statement(s)… } else if (expression2) { …block of statement(s)… } else if (expression3) { …block of statement(s)… } …. …. else { …block of statement(s)… } exp-1 … exp-n True True True False False False (else) Set of instructions Set of instructions Set of instructions Set of instructions Set of instructions
  • 9. Switch-case Syntax: switch (case_expression) { case constant_expression1 : …block of code… break; case constant_expression2 : …block of code… break; case constant_expression3 : …block of code… break; … default : … block of code… } exp-1 … Default True True True False False False Set of instructions Set of instructions Set of instructions Set of instructions
  • 10. Ternary Operator Syntax: test_condition ? statement1 : statement2 ; Equivalent form in if-else construct: if (test_consition) statement1; else statement2; expression instruction instruction True False