24
CS 1400 Chapter 4, sections 1 - 6

CS 1400 Chapter 4, sections 1 - 6. Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=

  • View
    217

  • Download
    1

Embed Size (px)

Citation preview

CS 1400

Chapter 4, sections 1 - 6

Relational operators

• Less than <

• Greater than >

• Less than or equal <=

• Greater than or equal >=

• Equal ==

• Not equal !=

Relational expressions

• A comparison is a relational expression

• A simple relational expression compares two operands and is either true or false

• Examplescost < 56

time >= 1.5

25 != age

bool variables

• A bool variable can hold true or false

• Examples:bool answer, flag;

answer = age < 25;

flag = cost == price;

What is “truth”?

• In C++: true is non-zero, false is zero

• The words true and false are actually reserved constants.bool valid, correct;

valid = true;

correct = false;

Condition statements

• Recommended forms

if (rel-expr) if (rel-expr)

{ statements { statements

} }

else

{ statements

}

Condition statements…

• Each condition statement divides a program into two separate paths– the true path– the false path (which may be absent)

• Only one of these two paths are taken!

Examples…

• Write a program to output ADULT if a person’s age is 21 or over.

• Write a program to output ADULT for 21 and over and MINOR otherwise.

• Write a program to output TWENTY-ONE if a person’s age is exactly 21.

What do braces mean?

• Braces are a way to associate a set of statements to be treated as one statement. – In other words, any place you can put

one statement, you can put a set of statements in braces!

• A set of statements in braces is commonly referred to as a block.

Condition statements revisited…

• General forms

if (rel-expr) if (rel-expr)

statement statement

else

statement

Braces are not required for a single statement

• But an else is always associated with the closest possible if above it!

• Example: Skill level >1 is advanced, level >2 is advanced master, lower levels are beginners.

if (skill > 1)

cout << “advanced “;

if (skill > 2)

cout << “master”;

else cout << “beginner”;

Solution – use braces!

if (skill > 1)

{ cout << “advanced “;

if (skill > 2)

{ cout << “master”;

}

}

else cout << “beginner”;

Nested conditions (if/else/if)• Example: Output a letter grade based

upon a test score– 90 to 100 is an A– 80 to below 90 is a B– 70 to below 80 is a C– 60 to below 70 is a D– below 60 is an F

• Solution: Divide the program into two partsStepwise Decomposition!

cout << “Enter grade: “;

cin >> grade;

if (grade >= 90)

{ cout << “A”;

}

else

{

}

grade must be B or below

First, the A’s and non A’s

// cloud of non A’s

Now divide the non-A’s into two parts (B’s and non B’s)…

if (grade >= 80)

{ cout << “B”;

}

else

{

}

grade must be C or below

// cloud of non B’s

and so on…

if (grade >= 70)

{ cout << “C”;

}

else

{

}

grade must be D or below

// cloud of non C’s

Finally, grade must be D or F

if (grade >= 60)

cout << “D”;

else

cout << “F”;

#include <iostream>using namespace std;int main(){ float grade;

cout << "Enter grade: ";cin >> grade;if (grade >= 90){ cout << "A";}else{ if (grade >= 80)

{ cout << "B";}else{ if (grade >= 70)

{ cout << "C";}else{ if (grade >= 60)

cout << "D";else

cout << "F";}

}}

}

#include <iostream>using namespace std;int main(){ float grade;

cout << "Enter grade: ";cin >> grade;if (grade >= 90)

cout << "A";else if (grade >= 80)

cout << "B";else if (grade >= 70)

cout << "C";else if (grade >= 60)

cout << "D";else

cout << "F";}

Braces are actually not needed for a single statement!

Examples…

• Input a grade and output “passing” if the grade is 60 or above.

• Input an age and output one of three clasifications: adult (21 to 64), minor (under 21), or senior (65 and over).

• Input a price and validate with the message “valid” if the price is between $1500 and $3500 (inclusive)

Question: What would the output be for inputs of 1, 2, and 3?

int answer;

cin >> answer;

if (answer == 1)

cout << “A “;

if (answer < 3)

cout << “B”;

else cout << “C”;

Example;

A person 15 years of age may acquire a driver’s lic. with permission from parents. A person over 15 is eligible without parental permission. A person under 15 is not eligible.

• Write a small program to input an age and output one of 3 messages: “permission required”, “eligible”, or “not eligible”.

Example;

• A person 15 years of age and female or 16 years of age and male may acquire a driver’s lic. with permission from parents. A person over 16 is eligible without parental permission. A person under 15 is not eligible.

Summary

• relational operators

• relational expressions

• bool variables

• truth

• condition statements

• braces and blocks

• stepwise decomposition