Upload
clifton-james
View
215
Download
0
Embed Size (px)
Citation preview
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
Decisions
Compare two Strings Character by character from left to
right Compare their ASCII values Once they are not equal,
comparison stops
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
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.
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.
Decisions
Logical “not”: !p, where p is a logical expression If p is true, !p is false if p is false, !p is true
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
Decisions
if statement switch statement
if statement
Simple if statementif (condition) {statements;
}if condition is true, execute the statements, otherwise skip it.
if statement
if-else statementif (condition) {
Tstatements;} else {
Fstatements;} if condition is true, execute the
Tstatements if condition is false, execute the
Fstatements
Multi-choice if statementif (condition 1) {
statements 1;} else if (condition 2){
statements 2;} else if …………[ else {
last statements;} ]
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.
Multi-choice if statement At most one set of the
statements is executed!!! The conditions should be exclusive and not overlapped.
switch and case statementswitch (expression) {
case constant1:statements1;break;
case constant2:statement2;break;
……[ default:
default statements; ]}
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
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.
switch and case statement At most one set of the
statements is executed!!! The constants should be different.
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.
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.
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.
Programming Problems
3. Write a program that gives comments according to the letter grades.