Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=, <, <=  Pay attention to the equality operator, TWO equal signs.

Similar presentations


Presentation on theme: "Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=, <, <=  Pay attention to the equality operator, TWO equal signs."— Presentation transcript:

1 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

2 Decisions  Compare two Strings  Character by character from left to right  Compare their ASCII values  Once they are not equal, comparison stops

3 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

4 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.

5 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.

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

7 Decisions  Truth table  T --- True, F --- False PqP && qp || q!p TTTTF TFFTF FTFTT FFFFT

8 Decisions  if statement  switch statement

9 if statement  Simple if statement if (condition) { statements; } if condition is true, execute the statements, otherwise skip it.

10 if statement  if-else statement if (condition) { Tstatements; } else { Fstatements; }  if condition is true, execute the Tstatements  if condition is false, execute the Fstatements

11 Multi-choice if statement if (condition 1) { statements 1; } else if (condition 2){ statements 2; } else if …… …… [ else { last statements; } ]

12 Multi-choice if statement If 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 false a) if there is an else statement, execute the laststatements b) if there is no else statement, simply go out the if statement.

13 Multi-choice if statement  At most one set of the statements is executed!!!  The conditions should be exclusive and not overlapped.

14 switch and case statement switch (expression) { case constant1: statements1; break; case constant2: statement2; break; …… [ default: default statements; ] }

15 switch and case statement expression --- an integer expression Start with version 7, expression could be an String expression constant1, constant2… --- integer constants In version 7, could use String constants

16 switch and case statement If 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 defaultstatements b) if there is no default clause, simply go out the switch statement.

17 switch and case statement  At most one set of the statements is executed!!!  The constants should be different.

18 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.

19 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.

20 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.

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


Download ppt "Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=, <, <=  Pay attention to the equality operator, TWO equal signs."

Similar presentations


Ads by Google