c++ programming, namiq sultan1 chapter 4 making decisions namiq sultan university of duhok...

73
C++ Programming, Namiq Sultan 1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference: Starting Out with C++, Tony Gaddis, 2 nd Ed.

Upload: jessica-paul

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 1

Chapter 4 Making Decisions

Namiq Sultan

University of Duhok

Department of Electrical and Computer Engineerin

Reference: Starting Out with C++, Tony Gaddis, 2nd Ed.

Page 2: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 2

4.1 Relational Operators

• Relational operators allow you to compare numeric values and determine if one is greater than, less than, equal to, or not equal to another.

Page 3: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 3

Table 4-1

Relational Operators (in Order of Precedence)

Meaning

>

<

> =

< =

= =

!=

Greater than

Less than

Greater than or equal to

Less than or equal to

Equal to

Not equal to

Page 4: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 4

Table 4-2

Expression What the Expression Means X > Y

X < Y

X > = Y

X < = Y

X = = Y

X != Y

Is X greater than Y ?

Is X less than Y ?

Is X greater than or equal to Y ?

Is X less than or equal to Y ?

Is X equal to Y ?

Is X not equal to Y ?

Page 5: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 5

The Value of a Relationship

• Relational expressions are also know as a Boolean expression

• Warning! The equality operator is two equal signs together

==

Page 6: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 6

Table 4-3 Assume x is 10 and y is 7

Expression Value

x < y False, because x is not less than y .

x > y True, because x is greater than y .

x > = y True, because x is greater than or equal to y .

x < = y False, because x is not less than or equal to y .

y != x True, because y is not equal to x .

Page 7: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 7

Program 4-1

// This program displays the values of true and false states.

#include <iostream>

using namespace std;

int main()

{int trueValue, falseValue, x = 5, y = 10;

trueValue = x < y;falseValue = y== x;cout << "True is " << trueValue << endl;cout << "False is " << falseValue << endl;

}

Program OutputTrue is 1False is 0

Page 8: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 8

4.2 The if Statement

• The if statement can cause other statements to execute only under certain conditions.

Page 9: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 9

Program 4-2

// This program averages 3 test scores#include <iostream> using namespace std;

int main(void){

int score1, score2, score3;float average;

cout << "Enter 3 test scores and I will average them: ";cin >> score1 >> score2 >> score3;average = (score1 + score2 + score3) / 3.0;cout << "Your average is " << average << endl;if (average > 95)

cout << "Congratulations! That's a high score!\n";

}

Page 10: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 10

Program Output with Example Input

Enter 3 test scores and I will average them: 80 90 70 [Enter]

Your average is 80

Program Output with Other Example Input

Enter 3 test scores and I will average them: 100 100 100 [Enter]

Your average is 100

Congratulations! That's a high score!

Page 11: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 11

Table 4-5

Statements Outcome

if (H o u rs > 4 0 ) O v e rT im e = 1 ;

Assigns 1 to O v erT im e only when H o u rs is greater than 40

if (V a lu e > 3 2 ) co u t < < " In v a lid n u m b er \n " ;

Displays the message “Invalid number” only when V alu e is greater than 32

if (O v erT im e = = 1 ) P a y R a te * = 2 ;

Multiplies P ay R a te by 2 only when O v erT im e is equal to 1

Page 12: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 12

Be Careful With Semicolonsif (expression)

statement;

• Notice that the semicolon comes after the statement that gets executed if the expression is true; the semicolon does NOT follow the expression

Expression

Statement(s)

TrueFalse

Page 13: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 13

Program 4-3

// This program demonstrates how a misplaced semicolon// prematurely terminates an if statement.#include <iostream> using namespace std;

int main(){

int x = 0, y = 10;cout << “x is " << x << " and y is " << y << endl;if (x > y); // misplaced semicolon!

cout << “x is greater than y\n"; // Always executed

}

Program OutputX is 0 and Y is 10

X is greater than Y

Page 14: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 14

Programming Style and the if Statement

• The conditionally executed statement should appear on the line after the if statement.

• The conditionally executed statement should be indented one “level” from the if statement.

• Note: Each time you press the tab key, you are indenting one level.

Page 15: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 15

And Now Back to Truth

• When a relational expression is true, it has the value 1.• When a relational expression is false it has the value 0.• An expression that has the value 0 is considered false by

the if statement.• An expression that has any value other than 0 is considered

true by the if statement.

Page 16: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 16

• Consider the following statement:

if (x = 2) // caution here!!!!

cout << “It is True!”;• This statement does not determine if x is equal to 2, it

assigns x the value 2, therefore, this expression will always be true because the value of the expression is 2, a non-zero value

Page 17: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 17

Program 4-5

// This program averages 3 test scores. The if statement uses// the = operator, but the == operator was intended.#include <iostream> using namespace std;

int main(){

int score1, score2, score3;float average;

cout << "Enter 3 scores and I will average them:";cin >> score1 >> score2 >> score3;average = (score1 + score2 + score3) / 3.0;cout << "Your average is " << average << endl;if (average = 100) // Wrong

cout << "Congratulations! That's a high score!\n";

}

Page 18: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 18

Program Output with Example Input

Enter your 3 test scores and I will average them: 80 90 70[Enter]

Your average is 80

Congratulations! That’s a perfect score!

Page 19: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 19

4.3 Flags

• A flag is a variable, usually a boolean or an integer, that signals when a condition exists.

• If your compiler does not support the bool data type, use int instead.

Page 20: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 20

Program 4-6

// This program averages 3 test scores. It uses the variable highScore as a flag.#include <iostream> using namespace std;

int main( ){

int score1, score2, score3;float average;bool highScore = false;

  cout << "Enter your 3 test scores and I will average them: ";cin >> score1 >> score2 >> score3;average = (score1 + score2 + score3) / 3.0;if (average > 95)

highScore = true; // Set the flag variablecout << "Your average is " << average << endl;if (highScore)

cout << "Congratulations! That's a high score!\n";\}

Page 21: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 21

Program 4-6

Program Output with Example Input

Enter your 3 test scores and I will average them: 100 100 100 [Enter]

Your average is 100.0

Congratulations! That's a high score!

Page 22: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 22

4.4 Expanding the if Statement

• The if statement can conditionally execute a block of statement enclosed in braces.

if (expression){ statement; statement; // Place as many statements here as necessary.}

Page 23: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 23

Program 4-7

// This program averages 3 test scores.

// It uses the variable highScore as a flag.

#include <iostream>

using namespace std;

int main(void)

{

int score1, score2, score3;

float average;

bool highScore = false;

cout << "Enter 3 test scores and I will average them: ";

cin >> score1 >> score2 >> score3;

average = (score1 + score2 + score3) / 3.0;

if (average > 95)

highScore = true; // Set the flag variable

Page 24: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 24

cout << "Your average is " << average << endl;

if (highScore)

{

cout << "Congratulations!\n";

cout << "That's a high score.\n";

cout << "You deserve a pat on the back!\n";

}

}

Page 25: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 25

Program Output with Example Input

Enter your 3 test scores and I will average them: 100 100 100 [Enter]

Your average is 100.0

Congratulations!

That's a high score.

You deserve a pat on the back!

Program Output with Different Example Input

Enter your 3 test scores and I will average them: 80 90 70 [Enter]

Your average is 80.0

Page 26: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 26

Don’t Forget the Braces!

• If you intend to execute a block of statements with an if statement, don’t forget the braces.

• Without the braces, the if statement only executes the very next statement.

Page 27: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 27

4.5 The if/else Statement• The if/else statement will execute one group of statements if the

expression is true, or another group of statements if the expression is false.if (expression)

statement or block of statements;

else

statement or block of statements;

expression

Statement(s) Statement(s)

TrueFalse

Page 28: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 28

Program 4-9

// This program uses the modulus operator to determine// if a number is odd or even. If the number is evenly divided// by 2, it is an even number. A remainder indicates it is odd.#include <iostream> using namespace std;

int main(){

int number;

cout << "Enter an integer and I will tell you if it\n";cout << "is odd or even. ";cin >> number;if (number % 2 == 0)

cout << number << " is even.\n";else

cout << number << " is odd.\n";

}

Page 29: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 29

Program Output with Example Input

Enter an integer and I will tell you if itis odd or even. 17 [Enter]

17 is odd.

Page 30: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 30

Program 4-10

// This program asks the user for two numbers, num1 and num2.// num1 is divided by num2 and the result is displayed.// Before the division operation, however, num2 is tested// for the value 0. If it contains 0, the division does not take place.#include <iostream> using namespace std;

int main(){

float num1, num2, quotient;

cout << "Enter a number: ";cin >> num1;cout << "Enter another number: ";cin >> num2;

Page 31: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 31

if (num2 == 0){

cout << "Division by zero is not possible.\n";cout << "Please run the program again and enter\n";cout << "a number besides zero.\n";

}else{

quotient = num1 / num2;cout << "The quotient of " << num1 << " divided by ";cout << num2 << " is " << quotient << ".\n";

}

}

Page 32: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 32

Program Output

(When the user enters 0 for num2)

Enter a number: 10 [Enter]

Enter another number: 0 [Enter]

Division by zero is not possible.

Please run the program again and enter

a number besides zero.

Page 33: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 33

4.6 The if/else if Construct

• The if/else if statement is a chain of if statements. They perform their tests, one after the other, until one of them is found to be true.

if (expression)

statement or block of statements;

else if (expression)

statement or block of statements;

//

// put as many else it’s as needed here

//

else if (expression)

statement or block of statements;

Page 34: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 34

Program 4-11

// This program uses an if/else if statement to assign a// letter grade (A,B,C,D,or F) to a numeric test score.#include <iostream> using namespace std;

int main(){

int testScore;char grade;

cout << "Enter your numeric test score and I will\n";cout << "tell you the letter grade you earned: ";cin >> testScore;

Page 35: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 35

if (testScore < 60)grade = 'F';

else if (testScore < 70)grade = 'D';

else if (testScore < 80)grade = 'C';

else if (testScore < 90)grade = 'B';

else if (testScore <= 100)grade = 'A';

cout << "Your grade is " << grade << ".\n";

}

exprn Statement(s)

exprn Statement(s)

exprn Statement(s)

exprn Statement(s)

Statement(s)

T

T

T

T

F

F

F

F

Page 36: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 36

Program Output with Example Input

Enter your test score and I willtell you the letter grade you earned: 88 [Enter]

Your grade is B.

Page 37: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 37

Program 4-12

// This program uses independent if/else statements to assign a// letter grade (A, B, C, D, or F) to a numeric test score.// Do you think it will work?#include <iostream> using namespace std;

int main( ){

int testScore;char grade;

cout << "Enter your test score and I will tell you\n";cout << "the letter grade you earned: ";cin >> testScore;

Page 38: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 38

if (testScore < 60)grade = 'F';

if (testScore < 70)grade = 'D';

if (testScore < 80)grade = 'C';

if (testScore < 90)grade = 'B';

if (testScore <= 100)grade = 'A';

cout << "Your grade is " << grade << ".\n";

}

exprn Statement(s)

exprn Statement(s)

exprn Statement(s)

exprn Statement(s)

Statement(s)

T

T

T

T

F

F

F

F

Page 39: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 39

Program Output with Example Input

Enter your test score and I will tell youthe letter grade you earned: 40 [Enter]

Your grade is A.

Page 40: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 40

Program 4-13//This program uses an if/else if statement to//assign a letter grade ( A, B, C, D, or F )//to a numeric test score.#include <iostream> using namespace std;

int main(){

int testScore;

cout << "Enter your test score and I will tell you\n";cout << "the letter grade you earned: ";cin >> testScore;if (testScore < 60){

cout << "Your grade is F.\n";cout << "This is a failing grade. Better see your ";cout << "instructor.\n";

}

Page 41: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 41

else if (testScore < 70) {

cout << "Your grade is D.\n"; cout << "This is below average. You should get "; cout << "tutoring.\n";

}else if (testScore < 80)

{ cout << "Your grade is C.\n"; cout << "This is average.\n";

} else if (testScore < 90) {

cout << "Your grade is B.\n"; cout << "This is an above average grade.\n";

}

Page 42: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 42

Program Output with Example Input

Enter your test score and I will tell youthe letter grade you earned: 94 [Enter]

Your grade is A.

This is a superior grade. Good work!

else if (testScore <= 100) {

cout << "Your grade is A.\n";cout << "This is a superior grade. Good work!\n";

}}

Page 43: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 43

4.7 Using a Trailing else

• A trailing else, placed at the end of an if/else if statement, provides default action when none of the if’s have true expressions

Page 44: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 44

Program 4-14

// This program uses an if/else if statement to assign a// letter grade (A, B, C, D, or F) to a numeric test score.// A trailing else has been added to catch test scores > 100.

#include <iostream> using namespace std;

int main(){

int testScore;

cout << "Enter your test score and I will tell you\n";cout << "the letter grade you earned: ";cin >> testScore;

Page 45: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 45

if (testScore < 60){

cout << "Your grade is F.\n";cout << "This is a failing grade. Better see your ";cout << "instructor.\n";

}else if (testScore < 70){

cout << "Your grade is D.\n";cout << "This is below average. You should get ";cout << "tutoring.\n";

}

Page 46: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 46

else if (testScore < 80){

cout << "Your grade is C.\n";cout << "This is average.\n";

}else if (testScore < 90){

cout << "Your grade is B.\n";cout << "This is an above average grade.\n";

}

Page 47: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 47

else if (testScore <= 100){

cout << "Your grade is A.\n";cout << "This is a superior grade. Good work!\n";

}else // Default action{

cout << testScore << " is an invalid score.\n";cout << "Please enter scores no greater than 100.\n";

}

}

Page 48: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 48

Program Output with Example Input

Enter your test score and I will tell you

the letter grade you earned: 104 [Enter]

104 is an invalid score.

Please enter scores no greater than 100.

Page 49: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 49

4.9 Nested if Statements

• A nested if statement is an if statement in the conditionally-executed code of another if statement.

Page 50: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 50

Program 4-16

// This program demonstrates the nested if statement.#include <iostream> using namespace std;

int main(){

char employed, recentGrad;

cout << "Answer the following questions\n";cout << "with either Y for Yes or ";cout << "N for No.\n";cout << "Are you employed? ";cin >> employed;cout << "Have you graduated from college ";cout << "in the past two years? ";cin >> recentGrad;

Page 51: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 51

if (employed == 'Y'){

if (recentGrad == 'Y') // Nested if{

cout << "You qualify for the special ";cout << "interest rate.\n";

}}

}

Page 52: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 52

Program Output with Example Input

Answer the following questions

with either Y for Yes or N for No.

Are you employed? Y[Enter]

Have you graduated from college in the past two years? Y[Enter]

You qualify for the special interest rate.

Page 53: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 53

Program Output with Other Example Input

Answer the following questions

with either Y for Yes or N for No.

Are you employed? Y[Enter]

Have you graduated from college in the past two years? N[Enter]

Page 54: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 54

4.10 Logical Operators

• Logical operators connect two or more relational expressions into one, or reverse the logic of an expression.

Page 55: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 55

Table 4-6

Operator Meaning Effect

& & AND Connects two expressions into one. Bothexpressions must be true for the overallexpression to be true.

|| OR Connects two expressions into one. One orboth expressions must be true for the overallexpression to be true. It is only necessary forone to be true, and it does not matter which.

! NOT The ! operator reverses the “truth” of anexpression. It makes a true expression false,and a false expression true.

Page 56: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 56

Table 4-7

Expression 1 Expression 2 Expression 1 && Expression 2

True

False

False

True

False

True

False

True

False (0)

False (0)

False (0)

True (1)

Page 57: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 57

Program 4-18

// This program demonstrates the && logical operator.#include <iostream> using namespace std;

int main(){ char employed, recentGrad;

cout << "Answer the following questions\n"; cout << "with either Y for Yes or "; cout << "N for No.\n"; cout << "Are you employed? "; cin >> employed;

Page 58: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 58

cout << "Have you graduated from college "; cout << "in the past two years? "; cin >> recentGrad; if (employed == 'Y‘ && recentGrad == 'Y') // && Operator { cout << "You qualify for the special "; cout << "interest rate.\n"; } else { cout << "You must be employed and have \n"; cout << "graduated from college in the\n"; cout << "past two years to qualify.\n"; }

}

Page 59: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 59

Program Output with Example Input

Answer the following questionswith either Y for Yes orN for No.Are you employed? Y[Enter]Have you graduated from college in the past two years? N[Enter]You must be employed and havegraduated from college in thepast two years to qualify.

Page 60: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 60

Table 4-8

Expression 1 Expression 2 Expression 1 || Expression 2

True False True (1)

False True True (1)

False False False (0)

True True True (1)

Page 61: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 61

Table 4-9

Expression !(Expression)

True False (0)

False True (1)

Page 62: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 62

Program 4-20//This program asks the user for his annual income and

//the number of years he has been employed at his current job.

//The ! operator reverses the logic of the expression in the if/else statement.

#include <iostream>

using namespace std;

int main(){

float income; int years;cout << "What is your annual income? ";cin >> income;cout << "How many years have you worked at " << "your current job? ";cin >> years;if (!(income >= 35000 || years > 5)) // Uses the ! Logical operator{

cout << "You must earn at least $35,000 or have\n";cout << "been employed for more than 5 years.\n";

}else

cout << "You qualify.\n";

}

Page 63: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 63

Precedence of Logical Operators

!

&&

||

Page 64: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 64

4.11 Checking Numeric Ranges With Logical Operators

• Logical operators are effective for determining if a number is in or out of a range.

Page 65: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 65

4.15 The Conditional Operator

• You can use the conditional operator to create short expressions that work like if/else statements

expression ? result if true : result if false;

X < 0 ? Y = 1 0 : Z = 2 0 ;

Page 66: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 66

Program 4-29// This program calculates a consultant's charges at $50 per hour, for a minimum of

// 5 hours. ?: operator adjusts hours to 5 if less than 5 hours were worked.

#include <iostream>

using namespace std;

int main()

{

const float payRate = 50.0;

float hours, charges;

  cout << "How many hours were worked? ";

cin >> hours;

hours = hours < 5 ? 5 : hours;

charges = payRate * hours;

cout << "The charges are $" << charges << endl;

}

Page 67: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 67

Program Output with Example Input

How many hours were worked? 10 [Enter]

The charges are $500.00

Program Output with Example Input

How many hours were worked? 2 [Enter]

The charges are $250.00

Page 68: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 68

4.16 The switch Statement

• The switch statement lets the value of a variable or expression determine where the program will branch to.

Page 69: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 69

Program 4-31

// The switch statement in this program tells the user

// something he or she already knows: what they just entered!

 

#include <iostream>

using namespace std;

int main(void)

{

char choice;

 

cout << "Enter A, B, or C: ";

cin >> choice;

Page 70: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 70

switch (choice)

{

case 'A': cout << "You entered A.\n";

break;

case 'B’: cout << "You entered B.\n";

break;

case 'C’: cout << "You entered C.\n";

break;

default: cout << "You did not enter A, B, or C!\n";

}

}

Page 71: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 71

Program Output with Example Input

Enter A, B, or C: B [Enter]

You entered B.

Program Output with Different Example Input

Enter a A, B, or C: F [Enter]

You did not enter A, B, or C!

Page 72: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 72

Program 4-34

// The switch statement in this program uses the "fallthrough"

// feature to catch both upper and lowercase letters entered

// by the user.

 

#include <iostream>

using namespace std;

int main()

{

char feedGrade;

 

cout << "Our dog food is available in three grades:\n";

cout << "A, B, and C. Which do you want pricing for? ";

cin >> feedGrade;

Page 73: C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:

C++ Programming, Namiq Sultan 73

switch(feedGrade){

case 'a':case 'A': cout << "30 cents per pound.\n"; break;case 'b':case 'B': cout << "20 cents per pound.\n"; break;case 'c':case 'C': cout << "15 cents per pound.\n"; break;default: cout << "That is an invalid choice.\n";

}}