Chapter 3 Selections Liang Introduction to Programming with

  • Slides: 23
Download presentation
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson

Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 10136097200

Motivations If you assigned a negative value for radius in Listing 2. 1, Compute.

Motivations If you assigned a negative value for radius in Listing 2. 1, Compute. Area. cpp, the program would print an invalid result. If the radius is negative, you don't want the program to compute the area. How can you deal with this situation? Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 2

Objectives FTo declare bool type and write Boolean expressions using comparison operators (§ 3.

Objectives FTo declare bool type and write Boolean expressions using comparison operators (§ 3. 2). FTo implement selection control using one-way if statements (§ 3. 3) FTo program the Guess. Birth. Date game using one-way if statements (§ 3. 4). FTo implement selection control using two-way if statements (§ 3. 5). FTo implement selection control using nested if statements (§ 3. 6). FTo avoid common errors in if statements (§ 3. 7). FTo program using selection statements for a variety of examples (BMI, Compute. Tax, Subtraction. Quiz) (§§ 3. 8 -3. 10). FTo generate random numbers using the rand function and set a seed using the srand function (§ 3. 10). FTo combine conditions using logical operators (&&, ||, and !) (§ 3. 11). FTo program using selection statements with combined conditions (Leap. Year, Lottery) (§§ 3. 12 -3. 13). FTo implement selection control using switch statements (§ 3. 14). FTo write expressions using the conditional operator (§ 3. 15). FTo format output using the stream manipulators (§ 3. 16). FTo examine the rules governing operator precedence and operator associativity (§ 3. 17). Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 30136097200

The bool Type and Operators Often in a program you need to compare two

The bool Type and Operators Often in a program you need to compare two values, such as whether i is greater than j. C++ provides six relational operators (also known as comparison operators) in Table 3. 1 that can be used to compare two values. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 4

Comparison Operators Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education,

Comparison Operators Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 5

One-way if Statements if (boolean. Expression) { statement(s); } if (radius >= 0) {

One-way if Statements if (boolean. Expression) { statement(s); } if (radius >= 0) { area = radius * PI; cout << "The area for the circle of " << " radius " << radius << " is " << area; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 6

Note The braces can be omitted if they enclose a single statement. Liang, Introduction

Note The braces can be omitted if they enclose a single statement. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 7

Examples Write a program that checks whether a number is even or odd. The

Examples Write a program that checks whether a number is even or odd. The program prompts the user to enter an integer and displays “number is even” if it is even and “number is odd” if it is odd. #include <iostream> using namespace std; int main() { // Prompt the user to enter an integer int number; cout << "Enter an integer: "; cin >> number; Test. Boolean. cpp if (number % 2 == 0) cout << number << " is even. "; if (number % 2 != 0) cout << number << " is odd. "; system("PAUSE"); return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 8

Caution Adding a semicolon at the end of an if clause is a common

Caution Adding a semicolon at the end of an if clause is a common mistake. This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 9

Examples Guess. Birth. Date This section uses the if statements to write an interesting

Examples Guess. Birth. Date This section uses the if statements to write an interesting game program. The program can find your birth date. The program prompts you to answer whether your birth date is in the following five sets of numbers: Your birth date is the sum of the first numbers in the sets where your birth date appears. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 10

Guess. Birth. Date Examples #include <iostream> using namespace std; int main() { int date

Guess. Birth. Date Examples #include <iostream> using namespace std; int main() { int date = 0; // Date to be determined char answer; // Prompt the user for Set 1 cout << "Is your birth date in Set 1? " << endl; cout << "16 17 18 19n" << "20 21 22 23n" << "24 25 26 27n" << "28 29 30 31" << endl; cout << "Enter N for No and Y for Yes: "; cin >> answer; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 11

Guess. Birth. Date Examples if (answer == 'Y') date += 16; // Prompt the

Guess. Birth. Date Examples if (answer == 'Y') date += 16; // Prompt the user for Set 2 cout << "n. Is your birth date in Set 2? " << endl; cout << " 8 9 10 11n" << "12 13 14 15n" << "24 25 26 27n" << "28 29 30 31" << endl; cout << "Enter N for No and Y for Yes: "; cin >> answer; if (answer == 'Y') date += 8; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 12

Guess. Birth. Date Examples // Prompt the user for Set 3 cout << "n.

Guess. Birth. Date Examples // Prompt the user for Set 3 cout << "n. Is your birth date in Set 3? " << endl; cout << " 1 3 5 7n" << " 9 11 13 15n" << "17 19 21 23n" << "25 27 29 31" << endl; cout << "Enter N for No and Y for Yes: "; cin >> answer; if (answer == 'Y') date += 1; // Prompt the user for Set 4 cout << "n. Is your birth date in Set 4? " << endl; cout << " 2 3 6 7n" << "10 11 14 15n" << "18 19 22 23n" << "26 27 30 31" << endl; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 13

Guess. Birth. Date Examples cout << "Enter N for No and Y for Yes:

Guess. Birth. Date Examples cout << "Enter N for No and Y for Yes: "; cin >> answer; if (answer == 'Y') date += 2; // Prompt the user for Set 5 cout << "n. Is your birth date in Set 5? " << endl; cout << " 4 5 6 7n" << "12 13 14 15n" << "20 21 22 23n" << "28 29 30 31" << endl; cout << "Enter N for No and Y for Yes: "; cin >> answer; if (answer == 'Y') date += 4; cout << "Your birth date is " << date << endl; return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 14

Logical Operators Sometimes, the execution path is determined by a combination of several conditions.

Logical Operators Sometimes, the execution path is determined by a combination of several conditions. You can use logical operators to combine these conditions. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 15

Truth Table for Operator ! Liang, Introduction to Programming with C++, Second Edition, (c)

Truth Table for Operator ! Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 16

Truth Table for Operator && Liang, Introduction to Programming with C++, Second Edition, (c)

Truth Table for Operator && Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 17

Truth Table for Operator || Liang, Introduction to Programming with C++, Second Edition, (c)

Truth Table for Operator || Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 18

Example: Write a program that checks whether a number is divisible by 2 and

Example: Write a program that checks whether a number is divisible by 2 and 3, whether a number is divisible by 2 or 3, and whether a number is divisible by 2 or 3 but not both: #include <iostream> using namespace std; int main(){ int number; cout << "Enter an integer: "; cin >> number; Test. Boolean. Operators. cpp if (number % 2 == 0 && number % 3 == 0) cout << number << " is divisible by 2 and 3. " << endl; if (number % 2 == 0 || number % 3 == 0) cout << number << " is divisible by 2 or 3. " << endl; if ((number % 2 == 0 || number % 3 == 0) && !(number % 2 == 0 && number % 3 == 0)) cout << number << " divisible by 2 or 3, but not both. " << endl; system("PAUSE"); with C++, Second Edition, (c) 2010 Pearson Education, Inc. return(0); }Liang, Introduction to Programming All rights reserved. 0136097200 19

The if. . . else Statement if (boolean. Expression) { statement(s)-for-the-true-case; } else {

The if. . . else Statement if (boolean. Expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 20

The if. . . else Statement if (radius>=0) { area=radius * PI; cout<<“The area

The if. . . else Statement if (radius>=0) { area=radius * PI; cout<<“The area of the circle is: ”<<area; } else { cout<<“Negative radius”; } if (number%2==0) cout<<number<<“is even”; else cout<<number<<“is odd”; Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 21

Examples Write a program that lets the user enter a year and checks whether

Examples Write a program that lets the user enter a year and checks whether it is a leap year. A year is a leap year if it is divisible by 4 but not by 100 or if it is divisible by 400. So you can use the following Boolean expression to check whether a year is a leap year: (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 22

Leap. Year. cpp #include <iostream> using namespace std; int main(){ cout << "Enter a

Leap. Year. cpp #include <iostream> using namespace std; int main(){ cout << "Enter a year: "; int year; cin >> year; // Check if the year is a leap year bool is. Leap. Year = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); // Display the result in a message dialog box if (is. Leap. Year) cout << year << " is a leap year. "; else cout << year << " is a not leap year. "; system("PAUSE"); with C++, Second Edition, (c) 2010 Pearson Education, Inc. return 0; } Liang, Introduction to Programming All rights reserved. 0136097200 23