SlideShare a Scribd company logo
2
Most read
4
Most read
5
Most read
CONDITIONAL
STATEMENTS
NESTED IF STATEMENTS
NESTED IF
STATEMENTS
We can have two or more if statements inside each other to
check multiple conditions
 These are called nested if statements
Nested if statements can be used to test more than one
condition
Use indentation to reflect nesting and aid readability
 Typically indent 3-4 spaces or one tab
Need to take care when matching up { } brackets
 This way you can decipher the nesting of conditions
2
NESTED IF
STATEMENTS
if ( logical expression1 )
{
if ( logical expression2 )
{
// Statements to execute if expressions1 and expression2 are true
}
else
{
// Statements to execute if expression1 true and expression2 false
}
}
else
{
// Statements to execute if expression1 false
}
3
FLOWCHART FOR A
NESTED IF STATEMENT
4
NESTED IF CODE
// Determine loan qualification
if (employed == ‘y’)
{
if (recentGrad == ‘y’)
{
cout << “You qualify!”
}
}
Note: this could be done with one if and two conditions.
5
NESTED IF CODE
// Determine loan qualification using one if and
//two conditions
if (employed == ‘y’ && recentGrad == ‘y’)
{
cout << “You qualify!”
}
6
NESTED IF
STATEMENTS
// Simple nested if example
cin >> a >> b;
if (a < b)
{
cout << “A is smaller than Bn”;
if ((a > 0) && (b > 0))
cout << “A and B are both positiven”;
else
cout << “A or B or both are negativen”;
}
7
NESTED IF
STATEMENTS
// Ugly nested if example
if (a > 0) {
if (b < 0) {
a = 3 * b;
c = a + b; } }
else {
a = 2 * a;
c = b / a; }
 It is hard to see what condition the else code goes with
8
NESTED IF
STATEMENTS
// Pretty nested if example
if (a > 0)
{
if (b < 0)
{
a = 3 * b;
c = a + b;
}
}
else
{
a = 2 * a;
c = b / a;
}
9
BOOLEAN VARIABLES
 In C++ we can store true/false values in Boolean variables
 The constants “true” and “false” can be used to initialize
these variables
 bool Done = true;
 bool Quit = false;
 Boolean expressions can also be used to initialize these
variables
 bool Positive = (a >= 0);
 bool Negative = (b < 0);
10
BOOLEAN VARIABLES
 Boolean variables and true/false constants can also be
used in logical expressions
 (Done == true) is true
 (Quit != true) is true
 (Done == Quit) is false
 (true == Positive) is true
 ((a < b) == false) is false
 (Negative) is false
11
BOOLEAN VARIABLES
 Internally C++ stores Boolean variables as integers
 0 is normally used for false
 1 is normally used for true
 Any non-zero value is also considered true
 It is considered “bad programming style” to use integers
instead of true/false
 bool Good = 0;
 bool Bad = 1;
 bool Ugly = a;
12
BOOLEAN VARIABLES
 Integers are used when writing Boolean values
 cout << Good will print 0
 cout << Bad will print 1
 cout << Ugly will also print 1
 Integers are also used when reading Boolean values
 cin >> Good;
 Entering 0 sets Good variable to false
 Entering any value >= 1 sets Good variable to true
 Entering value < 0 also sets Good variable to true
13
PRIME NUMBER
EXAMPLE
 How can we test a number to see if it is prime?
 We are given numerical values between 1..100
 We need to see if it has any factors besides 1 and itself
 We need some nested if statements
 Test if input number is between 1..100
 If so, then test if 2,3,5,7 are factors of input number
 Then print out “prime” or “not prime”
14
PRIME NUMBER
EXAMPLE
// Check for prime numbers using a factoring approach
#include <iostream>
using namespace std;
int main()
{
// Local variable declarations
// Read input parameters
// Check input is valid
// Check if number is prime
// Print output
return 0;
}
15
For the first version
of program we just
write comments in
the main program to
explain our approach
PRIME NUMBER
EXAMPLE
// Check for prime numbers using a factoring approach
#include <iostream>
using namespace std;
int main()
{
// Local variable declarations
int Number = 0;
bool Prime = true;
// Read input parameters
cout << “Enter a number [1..100]:”;
cin >> Number;
16
For the second
version of program
we initialize variables
and read the input
value from user
PRIME NUMBER
EXAMPLE
…
cout << “Enter a number [1..100]:”;
cin >> Number;
// Check input is valid
if ((Number < 1) || (Number > 100))
cout << “Error: Number is out of rangen”;
else
{
// Check if number is prime
// Print output
}
17
For the next version
of the program we
add code to verify the
range of input value
PRIME NUMBER
EXAMPLE
…
// Check if number is prime
if (Number == 1) Prime = false;
if ((Number > 2) && (Number % 2 == 0)) Prime = false;
if ((Number > 3) && (Number % 3 == 0)) Prime = false;
if ((Number > 5) && (Number % 5 == 0)) Prime = false;
if ((Number > 7) && (Number % 7 == 0)) Prime = false;
// Print output
if (Prime)
cout << “Number “ << Number << “ IS primen”;
else
cout << “Number “ << Number << “ is NOT primen”;
… 18
In the final
version we finish
the prime number
calculation and
print the output
PRIME NUMBER
EXAMPLE
 How should we test the prime number program?
 Test the range checking code by entering values “on the
border” of the input range (e.g. 0,1,2 and 99,100,101)
 Test program with several values we know are prime
 Test program with several values we know are not prime
 To be really compulsive we could test all values between
1..100 and compare to known prime numbers
 What is wrong with this program?
 It only works for inputs between 1..100
 It will not “scale up” easily if we extend this input range
19
SUMMARY
 In this section we showed how if statements and if-else
statements can be nested inside each other to create more
complex paths through a program
 We also showed how proper indenting is important to read
and understand programs with nested if statements
 We have seen how Boolean variables can be used to store
true/false values in a program
 Finally, we used an incremental approach to create a
program for checking the factors of input numbers to see
if they are prime or not
20
EXAMPLE: WAGES.CPP
Write a C++ program that calculates weekly wages for hourly
employees.
 Regular hours 0 - 40 are paid at $10/hours.
 Overtime (> 40 hours per week) is paid at 150%

More Related Content

PPT
Control All
PPT
How to Program
PPTX
Basics of Control Statement in C Languages
PPTX
Pseudocode
PPT
PDF
Programming fundamental 02
PDF
C Programming Unit II Sharad Institute college
PDF
Control All
How to Program
Basics of Control Statement in C Languages
Pseudocode
Programming fundamental 02
C Programming Unit II Sharad Institute college

Similar to 10-Lec - Nested IF Statement.pptx (20)

PDF
Lesson 2 C STATEMENT IN PROGRAMMING (M).pdf
PDF
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
PPT
C language UPTU Unit3 Slides
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
PPTX
Introduction& Overview-to-C++_programming.pptx
PPTX
Introduction to C++ programming language
PPTX
computer programming Control Statements.pptx
PPTX
C Programming: Control Structure
PPTX
Control structure of c
PPTX
Lecture 2
PDF
4. programing 101
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
PDF
Control statements-Computer programming
DOCX
C Control Statements.docx
PDF
Loops and conditional statements
PPT
Chapter 4 flow control structures and arrays
PPT
PDF
ICP - Lecture 7 and 8
PPTX
Control Statement programming
Lesson 2 C STATEMENT IN PROGRAMMING (M).pdf
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
C language UPTU Unit3 Slides
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
Introduction& Overview-to-C++_programming.pptx
Introduction to C++ programming language
computer programming Control Statements.pptx
C Programming: Control Structure
Control structure of c
Lecture 2
4. programing 101
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Control statements-Computer programming
C Control Statements.docx
Loops and conditional statements
Chapter 4 flow control structures and arrays
ICP - Lecture 7 and 8
Control Statement programming
Ad

More from AqeelAbbas94 (20)

PPT
trees_introduction.ppt ug yg i gg yg gj
PPT
ch6_2_v1.ppt yh jkkj jhjh h jhyj hg hghh
PPT
lecture_5 (2).ppt hjhrrgjbgrmgrhbgrgghjd
PPT
1-Lec - Introduction vhvv,vbvv,v (2).ppt
PPT
2-Lec - History of OOP and Java (1) .ppt
PPTX
Lecture-32-33.pptx
PPTX
blue.pptx
PPTX
use_case+use_case description.pptx
PPT
555e81217b39f1c1262b33d0.ppt
PPTX
12-Lec - Repetition For Loop.pptx
PPTX
hexagon.pptx
PPT
Lecture1.ppt
PDF
wepik-exploring-the-robust-features-of-samsung-health-app-enhancing-well-bein...
PPT
lecture56.ppt
PPT
04.ppt
PPT
19-Lec - Multidimensional Arrays.ppt
PPT
SelectionSort.ppt
PPT
Lecture2 (1).ppt
PPT
ch06.ppt
PPTX
Your Title goes Here.pptx
trees_introduction.ppt ug yg i gg yg gj
ch6_2_v1.ppt yh jkkj jhjh h jhyj hg hghh
lecture_5 (2).ppt hjhrrgjbgrmgrhbgrgghjd
1-Lec - Introduction vhvv,vbvv,v (2).ppt
2-Lec - History of OOP and Java (1) .ppt
Lecture-32-33.pptx
blue.pptx
use_case+use_case description.pptx
555e81217b39f1c1262b33d0.ppt
12-Lec - Repetition For Loop.pptx
hexagon.pptx
Lecture1.ppt
wepik-exploring-the-robust-features-of-samsung-health-app-enhancing-well-bein...
lecture56.ppt
04.ppt
19-Lec - Multidimensional Arrays.ppt
SelectionSort.ppt
Lecture2 (1).ppt
ch06.ppt
Your Title goes Here.pptx
Ad

Recently uploaded (20)

PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
HVAC Specification 2024 according to central public works department
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
IGGE1 Understanding the Self1234567891011
PDF
Computing-Curriculum for Schools in Ghana
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
20th Century Theater, Methods, History.pptx
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Trump Administration's workforce development strategy
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
My India Quiz Book_20210205121199924.pdf
Unit 4 Computer Architecture Multicore Processor.pptx
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
HVAC Specification 2024 according to central public works department
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
IGGE1 Understanding the Self1234567891011
Computing-Curriculum for Schools in Ghana
Paper A Mock Exam 9_ Attempt review.pdf.
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
20th Century Theater, Methods, History.pptx
AI-driven educational solutions for real-life interventions in the Philippine...
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Computer Architecture Input Output Memory.pptx
Trump Administration's workforce development strategy
What if we spent less time fighting change, and more time building what’s rig...
TNA_Presentation-1-Final(SAVE)) (1).pptx

10-Lec - Nested IF Statement.pptx

  • 2. NESTED IF STATEMENTS We can have two or more if statements inside each other to check multiple conditions  These are called nested if statements Nested if statements can be used to test more than one condition Use indentation to reflect nesting and aid readability  Typically indent 3-4 spaces or one tab Need to take care when matching up { } brackets  This way you can decipher the nesting of conditions 2
  • 3. NESTED IF STATEMENTS if ( logical expression1 ) { if ( logical expression2 ) { // Statements to execute if expressions1 and expression2 are true } else { // Statements to execute if expression1 true and expression2 false } } else { // Statements to execute if expression1 false } 3
  • 4. FLOWCHART FOR A NESTED IF STATEMENT 4
  • 5. NESTED IF CODE // Determine loan qualification if (employed == ‘y’) { if (recentGrad == ‘y’) { cout << “You qualify!” } } Note: this could be done with one if and two conditions. 5
  • 6. NESTED IF CODE // Determine loan qualification using one if and //two conditions if (employed == ‘y’ && recentGrad == ‘y’) { cout << “You qualify!” } 6
  • 7. NESTED IF STATEMENTS // Simple nested if example cin >> a >> b; if (a < b) { cout << “A is smaller than Bn”; if ((a > 0) && (b > 0)) cout << “A and B are both positiven”; else cout << “A or B or both are negativen”; } 7
  • 8. NESTED IF STATEMENTS // Ugly nested if example if (a > 0) { if (b < 0) { a = 3 * b; c = a + b; } } else { a = 2 * a; c = b / a; }  It is hard to see what condition the else code goes with 8
  • 9. NESTED IF STATEMENTS // Pretty nested if example if (a > 0) { if (b < 0) { a = 3 * b; c = a + b; } } else { a = 2 * a; c = b / a; } 9
  • 10. BOOLEAN VARIABLES  In C++ we can store true/false values in Boolean variables  The constants “true” and “false” can be used to initialize these variables  bool Done = true;  bool Quit = false;  Boolean expressions can also be used to initialize these variables  bool Positive = (a >= 0);  bool Negative = (b < 0); 10
  • 11. BOOLEAN VARIABLES  Boolean variables and true/false constants can also be used in logical expressions  (Done == true) is true  (Quit != true) is true  (Done == Quit) is false  (true == Positive) is true  ((a < b) == false) is false  (Negative) is false 11
  • 12. BOOLEAN VARIABLES  Internally C++ stores Boolean variables as integers  0 is normally used for false  1 is normally used for true  Any non-zero value is also considered true  It is considered “bad programming style” to use integers instead of true/false  bool Good = 0;  bool Bad = 1;  bool Ugly = a; 12
  • 13. BOOLEAN VARIABLES  Integers are used when writing Boolean values  cout << Good will print 0  cout << Bad will print 1  cout << Ugly will also print 1  Integers are also used when reading Boolean values  cin >> Good;  Entering 0 sets Good variable to false  Entering any value >= 1 sets Good variable to true  Entering value < 0 also sets Good variable to true 13
  • 14. PRIME NUMBER EXAMPLE  How can we test a number to see if it is prime?  We are given numerical values between 1..100  We need to see if it has any factors besides 1 and itself  We need some nested if statements  Test if input number is between 1..100  If so, then test if 2,3,5,7 are factors of input number  Then print out “prime” or “not prime” 14
  • 15. PRIME NUMBER EXAMPLE // Check for prime numbers using a factoring approach #include <iostream> using namespace std; int main() { // Local variable declarations // Read input parameters // Check input is valid // Check if number is prime // Print output return 0; } 15 For the first version of program we just write comments in the main program to explain our approach
  • 16. PRIME NUMBER EXAMPLE // Check for prime numbers using a factoring approach #include <iostream> using namespace std; int main() { // Local variable declarations int Number = 0; bool Prime = true; // Read input parameters cout << “Enter a number [1..100]:”; cin >> Number; 16 For the second version of program we initialize variables and read the input value from user
  • 17. PRIME NUMBER EXAMPLE … cout << “Enter a number [1..100]:”; cin >> Number; // Check input is valid if ((Number < 1) || (Number > 100)) cout << “Error: Number is out of rangen”; else { // Check if number is prime // Print output } 17 For the next version of the program we add code to verify the range of input value
  • 18. PRIME NUMBER EXAMPLE … // Check if number is prime if (Number == 1) Prime = false; if ((Number > 2) && (Number % 2 == 0)) Prime = false; if ((Number > 3) && (Number % 3 == 0)) Prime = false; if ((Number > 5) && (Number % 5 == 0)) Prime = false; if ((Number > 7) && (Number % 7 == 0)) Prime = false; // Print output if (Prime) cout << “Number “ << Number << “ IS primen”; else cout << “Number “ << Number << “ is NOT primen”; … 18 In the final version we finish the prime number calculation and print the output
  • 19. PRIME NUMBER EXAMPLE  How should we test the prime number program?  Test the range checking code by entering values “on the border” of the input range (e.g. 0,1,2 and 99,100,101)  Test program with several values we know are prime  Test program with several values we know are not prime  To be really compulsive we could test all values between 1..100 and compare to known prime numbers  What is wrong with this program?  It only works for inputs between 1..100  It will not “scale up” easily if we extend this input range 19
  • 20. SUMMARY  In this section we showed how if statements and if-else statements can be nested inside each other to create more complex paths through a program  We also showed how proper indenting is important to read and understand programs with nested if statements  We have seen how Boolean variables can be used to store true/false values in a program  Finally, we used an incremental approach to create a program for checking the factors of input numbers to see if they are prime or not 20
  • 21. EXAMPLE: WAGES.CPP Write a C++ program that calculates weekly wages for hourly employees.  Regular hours 0 - 40 are paid at $10/hours.  Overtime (> 40 hours per week) is paid at 150%