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

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

PDF
control statement
Kathmandu University
 
PPT
03a control structures
Manzoor ALam
 
PDF
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
PPTX
Introduction to Selection control structures in C++
Neeru Mittal
 
PPT
Chap 4 c++
Venkateswarlu Vuggam
 
PPTX
Lec16.pptx problem specifications computer
samiullahamjad06
 
PPTX
Control Statements and their use in C.pptx
jackyjokyoof
 
PDF
Chap 4 c++
Venkateswarlu Vuggam
 
PPTX
Decision Controls in C++ Programming Lecture
TishaFayeMendoza
 
PPT
C++basics
aamirsahito
 
PPT
c++basics.ppt
TatyaTope4
 
PPT
c++basiccs.ppt
RaghavendraMR5
 
PPT
c++basics.ppt
EPORI
 
PPT
c++basics.ppt
Infotech27
 
PPT
C++basics
JosephAlex21
 
PPTX
Decision Control Structure If & Else
Abdullah Bhojani
 
PDF
Fundamentals of Computer Programming - Flow of Control I
ChereLemma2
 
PPT
Control structures in C++ Programming Language
Ahmad Idrees
 
PPTX
CPP04 - Selection
Michael Heron
 
PDF
Selection
Jason J Pulikkottil
 
control statement
Kathmandu University
 
03a control structures
Manzoor ALam
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
Introduction to Selection control structures in C++
Neeru Mittal
 
Lec16.pptx problem specifications computer
samiullahamjad06
 
Control Statements and their use in C.pptx
jackyjokyoof
 
Decision Controls in C++ Programming Lecture
TishaFayeMendoza
 
C++basics
aamirsahito
 
c++basics.ppt
TatyaTope4
 
c++basiccs.ppt
RaghavendraMR5
 
c++basics.ppt
EPORI
 
c++basics.ppt
Infotech27
 
C++basics
JosephAlex21
 
Decision Control Structure If & Else
Abdullah Bhojani
 
Fundamentals of Computer Programming - Flow of Control I
ChereLemma2
 
Control structures in C++ Programming Language
Ahmad Idrees
 
CPP04 - Selection
Michael Heron
 

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
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Ad

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