Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: Booleans and if-else Statements Yoni Fridman 7/3/01 7/3/01.

Similar presentations


Presentation on theme: "Lecture 4: Booleans and if-else Statements Yoni Fridman 7/3/01 7/3/01."— Presentation transcript:

1 Lecture 4: Booleans and if-else Statements Yoni Fridman 7/3/01 7/3/01

2 OutlineOutline ä Performing comparisons ä Logical operators  The if statement  The if statement’s format  The if-else statement  The if-else statement’s format  Nested if-else statements ä More on booleans  The switch statement  The switch statement’s format ä Performing comparisons ä Logical operators  The if statement  The if statement’s format  The if-else statement  The if-else statement’s format  Nested if-else statements ä More on booleans  The switch statement  The switch statement’s format

3 Performing Comparisons ä Relational operators:, =. ä Relational operators test the relationship between two expressions and give a boolean result:  1 < 2 gives true.  3.0 >= 3 gives true.  1* 2 <= 3 – 3 gives false. ä Equality operators: ==, !=. ä Equality operators compare two expressions:  2.0 == 1 + 1 gives true.  2.1 != 1 + 1 gives true.  WARNING: 1.2 – 1.1 == 0.1 gives false. ä Relational operators:, =. ä Relational operators test the relationship between two expressions and give a boolean result:  1 < 2 gives true.  3.0 >= 3 gives true.  1* 2 <= 3 – 3 gives false. ä Equality operators: ==, !=. ä Equality operators compare two expressions:  2.0 == 1 + 1 gives true.  2.1 != 1 + 1 gives true.  WARNING: 1.2 – 1.1 == 0.1 gives false.

4 Logical Operators  How do you test if grade is between 80 and 89?  80 <= grade <= 89 gives an error. Why?  Instead, write 80 <= grade && grade <= 89. ä Logical operators: && (and), || (or), ! (unary not). ä Examples:  What’s the result of !true ? !false ?  What’s the difference between grade 89). ä ä  How do you test if grade is between 80 and 89?  80 <= grade <= 89 gives an error. Why?  Instead, write 80 <= grade && grade <= 89. ä Logical operators: && (and), || (or), ! (unary not). ä Examples:  What’s the result of !true ? !false ?  What’s the difference between grade 89). ä ä

5 The if Statement ä So far, our programs have executed every instruction, in order – this is very limited.  What if we want to display the letter associated with grade ?  Example: if (grade >= 90) System.out.println(“A”); System.out.println(“A”);  This is an example of the if statement. ä So far, our programs have executed every instruction, in order – this is very limited.  What if we want to display the letter associated with grade ?  Example: if (grade >= 90) System.out.println(“A”); System.out.println(“A”);  This is an example of the if statement.

6 The if Statement’s Format if (expression) statement expression is the condition being tested. It’s result must be boolean. statement is any Java statement. It’s executed only if expression is true. Indentation: Indicates nesting (this statement is inside the if statement). Parentheses: Required around the expression.

7 The if Statement ä This is still limited – what if we want to display the letter grade and then change it?  First try: if (grade >= 90) System.out.println(“A”); System.out.println(“A”); if (grade >= 90) grade += 2; grade += 2;  Better: if (grade >= 90) { System.out.println(“A”); System.out.println(“A”); grade += 2; grade += 2;} ä A block (code within the {}) acts as one statement. ä This is still limited – what if we want to display the letter grade and then change it?  First try: if (grade >= 90) System.out.println(“A”); System.out.println(“A”); if (grade >= 90) grade += 2; grade += 2;  Better: if (grade >= 90) { System.out.println(“A”); System.out.println(“A”); grade += 2; grade += 2;} ä A block (code within the {}) acts as one statement.

8 The if-else Statement ä This is better, but still limited – what if we want to display A’s and B’s?  First try: if (grade >= 90) System.out.println(“A”); System.out.println(“A”); if (grade < 90) System.out.println(“B”); System.out.println(“B”);  Better: if (grade >= 90) System.out.println(“A”); System.out.println(“A”);else System.out.println(“B”); System.out.println(“B”);  This is an example of the if-else statement. ä This is better, but still limited – what if we want to display A’s and B’s?  First try: if (grade >= 90) System.out.println(“A”); System.out.println(“A”); if (grade < 90) System.out.println(“B”); System.out.println(“B”);  Better: if (grade >= 90) System.out.println(“A”); System.out.println(“A”);else System.out.println(“B”); System.out.println(“B”);  This is an example of the if-else statement.

9 The if-else Statement’s Format if (expression) statement1 else statement2 expression is the same as in the if statement. statement1 is executed if expression is true. statement2 is executed if expression is false. Indentation: Note that the if and the else are not indented, but both statements are.

10 The if-else Statement ä We have one last limitation to overcome: What if we want to display more than two grades?  Solution: Use cascaded if-else statements: ä if (grade >= 90) System.out.println(“A”); System.out.println(“A”); else if (grade >= 80) System.out.println(“B”); System.out.println(“B”); else if (grade >= 70) System.out.println(“C”); System.out.println(“C”); else if (grade >= 60) System.out.println(“D”); System.out.println(“D”);else System.out.println(“F”); System.out.println(“F”); ä We have one last limitation to overcome: What if we want to display more than two grades?  Solution: Use cascaded if-else statements: ä if (grade >= 90) System.out.println(“A”); System.out.println(“A”); else if (grade >= 80) System.out.println(“B”); System.out.println(“B”); else if (grade >= 70) System.out.println(“C”); System.out.println(“C”); else if (grade >= 60) System.out.println(“D”); System.out.println(“D”);else System.out.println(“F”); System.out.println(“F”);

11 Nested if-else Statements ä We can also nest if-else statements.  Example: if (grade >= 90) { System.out.print(“A”); System.out.print(“A”); if (grade <= 92) if (grade <= 92) System.out.print(“-”); System.out.print(“-”); System.out.println(); System.out.println();} else if (grade >= 80) … ä Watch out for the “dangling else.” ä ä ä We can also nest if-else statements.  Example: if (grade >= 90) { System.out.print(“A”); System.out.print(“A”); if (grade <= 92) if (grade <= 92) System.out.print(“-”); System.out.print(“-”); System.out.println(); System.out.println();} else if (grade >= 80) … ä Watch out for the “dangling else.” ä ä

12 More on Booleans  All lecture we’ve been implicitly using booleans – the results of an equality, the if expression, etc. ä Booleans can be used explicitly – assume we want to remember whether someone got an “A.” ä boolean gradeIsA;  First try: if (grade >= 90) gradeIsA = true; gradeIsA = true;else gradeIsA = false; gradeIsA = false;  Better: gradeIsA = (grade >= 90);  Now we can write this: if (gradeIsA) …  All lecture we’ve been implicitly using booleans – the results of an equality, the if expression, etc. ä Booleans can be used explicitly – assume we want to remember whether someone got an “A.” ä boolean gradeIsA;  First try: if (grade >= 90) gradeIsA = true; gradeIsA = true;else gradeIsA = false; gradeIsA = false;  Better: gradeIsA = (grade >= 90);  Now we can write this: if (gradeIsA) …

13 A Word of Warning  Always write if (gradeIsA) … rather than if (gradeIsA == true) … ä Both do the same, so why is the first one better? ä It’s shorter. ä It’s closer to English (thus easier to read). ä It can prevent hard-to-find bugs. ä In lecture 2, I mentioned that “=” is assignment, not equality. ä What will this statement do? ä if (gradeIsA = true) …  Always write if (gradeIsA) … rather than if (gradeIsA == true) … ä Both do the same, so why is the first one better? ä It’s shorter. ä It’s closer to English (thus easier to read). ä It can prevent hard-to-find bugs. ä In lecture 2, I mentioned that “=” is assignment, not equality. ä What will this statement do? ä if (gradeIsA = true) …

14 The Switch Statement  Consider the following cascaded if-else statement to print out the day of the week: ä if (day == 1) System.out.println(“Monday”); System.out.println(“Monday”); else if (day == 2) System.out.println(“Tuesday”); System.out.println(“Tuesday”); else if (day == 3) System.out.println(“Wednesday”); System.out.println(“Wednesday”); else if (day == 4) System.out.println(“Thursday”); System.out.println(“Thursday”); else if (day == 5) System.out.println(“Friday”); System.out.println(“Friday”);  Consider the following cascaded if-else statement to print out the day of the week: ä if (day == 1) System.out.println(“Monday”); System.out.println(“Monday”); else if (day == 2) System.out.println(“Tuesday”); System.out.println(“Tuesday”); else if (day == 3) System.out.println(“Wednesday”); System.out.println(“Wednesday”); else if (day == 4) System.out.println(“Thursday”); System.out.println(“Thursday”); else if (day == 5) System.out.println(“Friday”); System.out.println(“Friday”);

15 The Switch Statement ä The switch statement provides a better way to do the same thing: ä Switch (day) { case 1: System.out.println(“Monday”); case 1: System.out.println(“Monday”); break; break; case 2: System.out.println(“Tuesday”); case 2: System.out.println(“Tuesday”); break; break; case 3: System.out.println(“Wednesday”); case 3: System.out.println(“Wednesday”); break; break; case 4: System.out.println(“Thursday”); case 4: System.out.println(“Thursday”); break; break; case 5: System.out.println(“Friday”); case 5: System.out.println(“Friday”); break; break;} ä The switch statement provides a better way to do the same thing: ä Switch (day) { case 1: System.out.println(“Monday”); case 1: System.out.println(“Monday”); break; break; case 2: System.out.println(“Tuesday”); case 2: System.out.println(“Tuesday”); break; break; case 3: System.out.println(“Wednesday”); case 3: System.out.println(“Wednesday”); break; break; case 4: System.out.println(“Thursday”); case 4: System.out.println(“Thursday”); break; break; case 5: System.out.println(“Friday”); case 5: System.out.println(“Friday”); break; break;}

16 The switch Statement’s Format switch (expression) { case label: statements … case label: statements default: statements } expression is usually of type int. Labels must be constants, and must be the same type as expression. default statements (optional) are executed if expression doesn’t match any of the case labels. Curly braces are required. Any number of statements that are executed if the correspond- ing case label matches expression.

17 The Switch Statement ä The default case isn’t required, but is often used to catch errors.  Example: … default: System.out.println(“Illegal value!”); … ä Notice that breaks aren’t required. ä But what happens if you leave them out? ä ä  Why use a switch statement rather than a cascaded if-else statement? ä Can make a program easier to read. ä Speeds up program execution. ä The default case isn’t required, but is often used to catch errors.  Example: … default: System.out.println(“Illegal value!”); … ä Notice that breaks aren’t required. ä But what happens if you leave them out? ä ä  Why use a switch statement rather than a cascaded if-else statement? ä Can make a program easier to read. ä Speeds up program execution.

18 HomeworkHomework ä Read: 4.6 (to end of p. 154), 4.7, 8.3, 4.8, 8.4 – in that order. ä Due Thursday. ä Read: 4.6 (to end of p. 154), 4.7, 8.3, 4.8, 8.4 – in that order. ä Due Thursday.


Download ppt "Lecture 4: Booleans and if-else Statements Yoni Fridman 7/3/01 7/3/01."

Similar presentations


Ads by Google