decisions relational operators operate on two numbers or two strings ==, !=, >, >=,

21
Decisions Relational operators Operate on two numbers or two Strings ==, !=, >, >=, <, <= Pay attention to the equality operator, TWO equal signs. All relational operators result in boolean value: true or false

Upload: clifton-james

Post on 04-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Decisions Relational operators Operate on two numbers or two

Strings ==, !=, >, >=, <, <= Pay attention to the equality operator,

TWO equal signs. All relational operators result in

boolean value: true or false

Page 2: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Decisions

Compare two Strings Character by character from left to

right Compare their ASCII values Once they are not equal,

comparison stops

Page 3: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Decisions Logical operators

Operate on logical expressions Logical expression: any expression whose value is a boolean value

Three operators:&& --- logical and|| --- logical or! --- logical not

Page 4: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Decisions

Logical “and”: p && q, where p and q are logical expressions Any one of p and q is false

the result is false Both p and q must be true to

get the result true.

Page 5: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Decisions

Logical “or”: p || q, where p and q are logical expressions Any one of p and q is true the result is true

Both p and q must be false to get the result false.

Page 6: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Decisions

Logical “not”: !p, where p is a logical expression If p is true, !p is false if p is false, !p is true

Page 7: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Decisions

Truth table T --- True, F --- False

P q P && q p || q !pT T T T F

T F F T F

F T F T T

F F F F T

Page 8: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Decisions

if statement switch statement

Page 9: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

if statement

Simple if statementif (condition) {statements;

}if condition is true, execute the statements, otherwise skip it.

Page 10: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

if statement

if-else statementif (condition) {

Tstatements;} else {

Fstatements;} if condition is true, execute the

Tstatements if condition is false, execute the

Fstatements

Page 11: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Multi-choice if statementif (condition 1) {

statements 1;} else if (condition 2){

statements 2;} else if …………[ else {

last statements;} ]

Page 12: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Multi-choice if statementIf condition 1 is true, execute statements

1 Go out of the if statement;If condition 1 is false, check condition2. If

condition 2 is true, execute statements 2 Go out of the if statement;

……If all conditions are falsea) if there is an else statement, execute the

laststatementsb) if there is no else statement, simply go

out the if statement.

Page 13: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Multi-choice if statement At most one set of the

statements is executed!!! The conditions should be exclusive and not overlapped.

Page 14: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

switch and case statementswitch (expression) {

case constant1:statements1;break;

case constant2:statement2;break;

……[ default:

default statements; ]}

Page 15: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

switch and case statementexpression --- an integer expression

Start with version 7, expression could be an String expression

constant1, constant2… --- integer constantsIn version 7, could use String constants

Page 16: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

switch and case statementIf expression equals constant1, statements1 is

executed; Go out of the switch statement;If expression does not equals constant1, check

constatant2, if they are equal, statements2 is executed; Go out of the switch statement;

……If the expression does not equal to any constant, a) if there is a default clause, execute the

defaultstatementsb) if there is no default clause, simply go out the

switch statement.

Page 17: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

switch and case statement At most one set of the

statements is executed!!! The constants should be different.

Page 18: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Programming Problems

1. Write a program that reads the number of hours (less than 60) worked in a week and hourly rate from the console, computes the gross income and displays the result on the console. Note that if the worked more than 40 hours, the extra hours should pay 1.5 times of the base rate.

Page 19: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Programming Problems

2. Write a program that reads the grades (20-100) from the console, computes the letter grade and displays it on the console. 20-59: F; 60-69: D; 70-79: C; 80-89: B; 90-100: A.

Page 20: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Programming Problems

1. Write a program that reads the number of hours (less than 60) worked in a week and hourly rate from the console, computes the gross income and displays the result on the console. Note that if the worked more than 40 hours, the extra hours should pay 1.5 times of the base rate.

Page 21: Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,

Programming Problems

3. Write a program that gives comments according to the letter grades.