SlideShare a Scribd company logo
Programming
Lecture 5
Mr. Danial Haider
Agenda of Lecture
 The if Selection
 The if/Else Selection
 Nested if/else Selection
Decision Making in C++
 Decision making is about deciding the order of execution of statements based on certain conditions or
repeat a group of statements until certain specified conditions are met.
 C++ handles decision-making by supporting the following statements,
 If statement
 switch statement
 conditional operator statement
 goto statement
Decision Making with If statement
 The if statement may be implemented in different forms depending on the complexity of conditions to
be tested. The different forms are:
 Simple if statement
 If…else statement
 Nested if…else statement
 else if statement
Simple If statement
 The general form of a simple if statement is,
 If (expression)
 {
 Statement-inside;
 }
 Statement-outside;
 If expression is true, then statement-inside is executed, other wise only statement-outside will
be executed.
The if Selection Structure
 Selection structure:
 Used to choose among alternative courses of action
 Pseudocode:
If student’s grade is greater than or equal to 60
Print “Passed”
 If condition true
 Print statement executed and program goes on to next statement
 If false, print statement is ignored and the program goes onto the next statement
 Indenting makes programs easier to read
 C++ ignores whitespace characters
The if Selection Structure
 Pseudocode statement in C++:
If grade >= 60
Print “Passed”
if ( grade >= 60 )
cout<<"Passedn";
 C++ code corresponds closely to the pseudocode
 Diamond symbol (decision symbol)
 Indicates decision is to be made
 Contains an expression that can be true or false
 Test the condition, follow appropriate path
The if Selection Structure
 if structure is a single-entry/single-exit structure
true
false
grade >= 60 print “Passed”
A decision can be made
on any expression.
zero - false
nonzero – true
Example:
3 - 4 is true
Simple If statement - Example
 #include<iostream.h>
 int main()
 {
 int x,y;
 x=15;
 y=13;
 If(x > y)
 {
 cout<<“x is greater than y”;
 }
 return 0;
 }
The if/else Selection Structure
 Test for multiple cases by placing if/else selection structures inside if/else selection
structures
The if/else Selection Structure
 Pseudocode:
If student’s grade is greater than or equal to 60
Print “Passed”
Else
Print “Failed”
 C++ code:
if ( grade >= 60 )
cout <<"Passedn";
else
cout << "Failedn";
 Ternary conditional operator (?:)
 cout << ( grade >= 60 ? "Passed" : "Failed" );
 grade >= 60 ? cout << "Passed" : cout << "Failed";
The if/else Selection Structure
 The general form of a simple if/else statement is,
 If (expression)
 {
 Statement-block1;
 }
 else
 {
 Statement-block2;
 }
 If expression is true, then statement-block1 is executed, other wise only statement-
block2 will be executed.
The if/else Selection -Example
 #include<iostream.h>
 int main()
 {
 int x,y;
 x=15;
 y=13;
 if(x > y){
 cout<<“x is greater than y”;
 }
 else{
 cout<<“y is greater than x”;
 }
 return 0;}
Nested if…else Selection - Statement(general-form)
 if (expression)
 {
 if (expression1)
 {
 Statement-block1;
 }
 else
 {
 Statement-block2;
 }
 }
 else
 {
 Statement-block3;
 }
 If ‘expression’ is false the ‘statement-
block3’ will be executed, otherwise it
continues to perform the test for
‘expression 1’ . If the ‘expression 1’ is
true the ‘statement block1’ is
executed otherwise ‘statement-block2’
is executed
Else…if Selection
The expression is tested from the top (of the ladder) downwards. As soon as the true condition is found, the
statement associated with it is executed.
Else…if Selection
Dangling - else Problem
 The C++ compiler always associates an else with the immediately preceding if unless told to do
otherwise by the placement of braces ({ and }). This behavior can lead to what’s referred to as the
dangling-else problem.
 (int x = 35; int y = 3; )
Dangling - else Problem
Blocks
 The if selection statement expects only one statement in its body.
 Similarly, the if and else parts of an if…else statement each expect only one body statement.
 To include several statements in the body of an if or in either part of an if…else, enclose the
statements in braces ({ and }). A set of statements contained within a pair of braces is called a
compound statement or a block
Questions??

More Related Content

PPTX
Control Statement programming
University of Potsdam
 
PPT
Decision making and branching
Hossain Md Shakhawat
 
PDF
conditional_statement.pdf
MdShahriarHossainSak
 
PPT
Chapter 1 nested control structures
Khirulnizam Abd Rahman
 
PPT
03a control structures
Manzoor ALam
 
PPT
Visula C# Programming Lecture 3
Abou Bakr Ashraf
 
PPT
Lecture 3
Soran University
 
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
Control Statement programming
University of Potsdam
 
Decision making and branching
Hossain Md Shakhawat
 
conditional_statement.pdf
MdShahriarHossainSak
 
Chapter 1 nested control structures
Khirulnizam Abd Rahman
 
03a control structures
Manzoor ALam
 
Visula C# Programming Lecture 3
Abou Bakr Ashraf
 
Lecture 3
Soran University
 
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 

Similar to week 3 Programming lecture 05 (1) j.pptx (20)

PPTX
C statements
Ahsann111
 
PPT
Selection Control Structures
PRN USM
 
PPTX
If and select statement
Rahul Sharma
 
PPTX
Control Structures, If..else, switch..case.pptx
doncreiz1
 
PPTX
If statements in c programming
Archana Gopinath
 
PDF
ICP - Lecture 7 and 8
Hassaan Rahman
 
PPTX
programming c language.
Abdul Rehman
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PPTX
Decision statements
Jaya Kumari
 
PPTX
Java Decision Control
Jayfee Ramos
 
PPTX
Decision statements in vb.net
ilakkiya
 
PDF
csj-161127083146power point presentation
deepayaganti1
 
PPTX
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
PPT
C statements.ppt presentation in c language
chintupro9
 
PPTX
Selection statements
Harsh Dabas
 
PDF
Chapter 8 - Conditional Statement
Deepak Singh
 
PPTX
Ch05-converted.pptx
ShivamChaturvedi67
 
PPT
Chapter 1 Nested Control Structures
Khirulnizam Abd Rahman
 
PPTX
CH-4 (1).pptx
Mehul Desai
 
C statements
Ahsann111
 
Selection Control Structures
PRN USM
 
If and select statement
Rahul Sharma
 
Control Structures, If..else, switch..case.pptx
doncreiz1
 
If statements in c programming
Archana Gopinath
 
ICP - Lecture 7 and 8
Hassaan Rahman
 
programming c language.
Abdul Rehman
 
Control Statements in Java
Niloy Saha
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
Decision statements
Jaya Kumari
 
Java Decision Control
Jayfee Ramos
 
Decision statements in vb.net
ilakkiya
 
csj-161127083146power point presentation
deepayaganti1
 
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
C statements.ppt presentation in c language
chintupro9
 
Selection statements
Harsh Dabas
 
Chapter 8 - Conditional Statement
Deepak Singh
 
Ch05-converted.pptx
ShivamChaturvedi67
 
Chapter 1 Nested Control Structures
Khirulnizam Abd Rahman
 
CH-4 (1).pptx
Mehul Desai
 
Ad

More from ZainabNoor83 (11)

PPTX
week 3 Programming lecture 06 (1) .pptx
ZainabNoor83
 
PPTX
SlideEgg_200403-MetaASFSADFDDDDDverse.pptx
ZainabNoor83
 
PPTX
week 4 Programming lecture 08asfsfsd.pptx
ZainabNoor83
 
PPTX
Applied Programming and Design PrinciplesLecture 2.pptx
ZainabNoor83
 
PPTX
Applied Programming and Design Principles Lecture 1.pptx
ZainabNoor83
 
PPTX
ET 123456789098765432234567654323432333334
ZainabNoor83
 
PPTX
Programming Lecture 01 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
ZainabNoor83
 
PPT
LLS Year 1-2.ppt
ZainabNoor83
 
PPTX
Frequency.pptx
ZainabNoor83
 
PPTX
IR Lec 3 (1) (1).pptx
ZainabNoor83
 
PPTX
Foreign Policy of Pakistan-II.pptx
ZainabNoor83
 
week 3 Programming lecture 06 (1) .pptx
ZainabNoor83
 
SlideEgg_200403-MetaASFSADFDDDDDverse.pptx
ZainabNoor83
 
week 4 Programming lecture 08asfsfsd.pptx
ZainabNoor83
 
Applied Programming and Design PrinciplesLecture 2.pptx
ZainabNoor83
 
Applied Programming and Design Principles Lecture 1.pptx
ZainabNoor83
 
ET 123456789098765432234567654323432333334
ZainabNoor83
 
Programming Lecture 01 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
ZainabNoor83
 
LLS Year 1-2.ppt
ZainabNoor83
 
Frequency.pptx
ZainabNoor83
 
IR Lec 3 (1) (1).pptx
ZainabNoor83
 
Foreign Policy of Pakistan-II.pptx
ZainabNoor83
 
Ad

Recently uploaded (20)

PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
Study Material and notes for Women Empowerment
ComputerScienceSACWC
 
PPTX
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
CDH. pptx
AneetaSharma15
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Study Material and notes for Women Empowerment
ComputerScienceSACWC
 
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
CDH. pptx
AneetaSharma15
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 

week 3 Programming lecture 05 (1) j.pptx

  • 2. Agenda of Lecture  The if Selection  The if/Else Selection  Nested if/else Selection
  • 3. Decision Making in C++  Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met.  C++ handles decision-making by supporting the following statements,  If statement  switch statement  conditional operator statement  goto statement
  • 4. Decision Making with If statement  The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are:  Simple if statement  If…else statement  Nested if…else statement  else if statement
  • 5. Simple If statement  The general form of a simple if statement is,  If (expression)  {  Statement-inside;  }  Statement-outside;  If expression is true, then statement-inside is executed, other wise only statement-outside will be executed.
  • 6. The if Selection Structure  Selection structure:  Used to choose among alternative courses of action  Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed”  If condition true  Print statement executed and program goes on to next statement  If false, print statement is ignored and the program goes onto the next statement  Indenting makes programs easier to read  C++ ignores whitespace characters
  • 7. The if Selection Structure  Pseudocode statement in C++: If grade >= 60 Print “Passed” if ( grade >= 60 ) cout<<"Passedn";  C++ code corresponds closely to the pseudocode  Diamond symbol (decision symbol)  Indicates decision is to be made  Contains an expression that can be true or false  Test the condition, follow appropriate path
  • 8. The if Selection Structure  if structure is a single-entry/single-exit structure true false grade >= 60 print “Passed” A decision can be made on any expression. zero - false nonzero – true Example: 3 - 4 is true
  • 9. Simple If statement - Example  #include<iostream.h>  int main()  {  int x,y;  x=15;  y=13;  If(x > y)  {  cout<<“x is greater than y”;  }  return 0;  }
  • 10. The if/else Selection Structure  Test for multiple cases by placing if/else selection structures inside if/else selection structures
  • 11. The if/else Selection Structure  Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed” Else Print “Failed”  C++ code: if ( grade >= 60 ) cout <<"Passedn"; else cout << "Failedn";  Ternary conditional operator (?:)  cout << ( grade >= 60 ? "Passed" : "Failed" );  grade >= 60 ? cout << "Passed" : cout << "Failed";
  • 12. The if/else Selection Structure  The general form of a simple if/else statement is,  If (expression)  {  Statement-block1;  }  else  {  Statement-block2;  }  If expression is true, then statement-block1 is executed, other wise only statement- block2 will be executed.
  • 13. The if/else Selection -Example  #include<iostream.h>  int main()  {  int x,y;  x=15;  y=13;  if(x > y){  cout<<“x is greater than y”;  }  else{  cout<<“y is greater than x”;  }  return 0;}
  • 14. Nested if…else Selection - Statement(general-form)  if (expression)  {  if (expression1)  {  Statement-block1;  }  else  {  Statement-block2;  }  }  else  {  Statement-block3;  }  If ‘expression’ is false the ‘statement- block3’ will be executed, otherwise it continues to perform the test for ‘expression 1’ . If the ‘expression 1’ is true the ‘statement block1’ is executed otherwise ‘statement-block2’ is executed
  • 15. Else…if Selection The expression is tested from the top (of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.
  • 17. Dangling - else Problem  The C++ compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what’s referred to as the dangling-else problem.  (int x = 35; int y = 3; )
  • 18. Dangling - else Problem
  • 19. Blocks  The if selection statement expects only one statement in its body.  Similarly, the if and else parts of an if…else statement each expect only one body statement.  To include several statements in the body of an if or in either part of an if…else, enclose the statements in braces ({ and }). A set of statements contained within a pair of braces is called a compound statement or a block