Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 Selection Statements

Similar presentations


Presentation on theme: "Lecture 3 Selection Statements"— Presentation transcript:

1 Lecture 3 Selection Statements
Boolean Logic boolean variables can have only 2 possible values -- true or false boolean flag = true; Relational Operators Ordering < <= > >= Equality == != Warning: Don’t use = to compare! Evaluate: (7 == 7) (7 != 7) (4 >= 5) (4 <= (5 + 3)) (‘A’ == ‘a’) (‘B’ >= ‘A’)

2 IF Statements Syntax: if (BooleanExpression) Single Java Statement; where Statement is any Java statement. Semantics: If BooleanExpression is true, then execute the Single Java Statement. (Otherwise, skip the Statement.) a = 4; b = 5; if (a > b) a = a + b; System.out.println(a); System.out.println(b);

3 What if we want to execute several statements in the then-part?
Make a compound statement by using { and } if (num1 > num2){ num1 = num1 + 1; num2 = num2 - 1; } //if What is the output of these Java statements? (i) int x = 7; if(x > 8){ x = x * 2; System.out.println(x); } System.out.println(“Bye!”); (ii) int x = 7; if(x > 8)

4 Write code that reads in 2 numbers, and then prints the larger number, followed by the smaller number. System.out.println("Enter two integers:"); num1 = keyboard.nextInt(); num2 = keyboard.nextInt(); if (num1 > num2) { System.out.println("Larger one:" + num1); System.out.println("Smaller one:" + num2); } if (num1 <= num2)

5 if (BooleanExpression) Statement1; else Statement2;
Can you write code to just print out the larger of the two? if … else if (BooleanExpression) Statement1; else Statement2; If BooleanExpression is true, execute Statement1; otherwise, execute Statement2. if (num1 > num2) System.out.println("Larger one:" + num1); System.out.println("Larger one:" + num2);

6 Boolean Operators Truth Table Examples AND (&&) P Q (P && Q)
True True True True False False False True False False False False OR (||) P Q (P || Q) True False True False True True NOT (!) P !(P) True False False True

7 Operator Precedence Chart
First: the unary operators: +, -, and ! Second: the binary arithmetic operators: *, /, % Third: the binary arithmetic operators: +, - Fourth: the comparison operators: <, >, =<, >= Fifth: the Boolean operators: ==, != Sixth: the Boolean operator && Seventh: the Boolean operator || Eighth: the assignment operator = TIP: use the ()s for an expression to make the meaning of the expression perfectly clear.

8 What is the output of these Java statements with the given values of x and y?
if ((x >= y) || (x == 3)) { x = x - 1; y = y + 1; } //if else { x = x + 1; y = y - 1; } //else System.out.println(x); System.out.println(y); Case 1. x = 5 and y = 3 Case 2. x = 3 and y = 5 Case 3. x = 4 and y = 5

9 Nested if…else Statements
if (cond1) statement1; else if (cond2) statement2; else if (cond3) statement3; else statement4; if (temp >= 100) ans = “Very Hot”; else if (temp >= 90) ans = “Hot”; else if (temp >= 70) ans = “Warm”; else if (temp >= 50) ans = “Cool”; else ans = “Cold”; Write code that assigns a letter grade for a student, based on his/her score.

10

11 if (gender == ‘M’) if (age < 21) premium = ; else premium = ; IMPORTANT: an else is matched with the most recent non-matched if!!! { } //if

12 switch statements The switch statement provides an alternative to nested if/else statements. Syntax: switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; case valueN: statement(s)N; default: statement(s)-for-default; }

13 Rules of switch statement
The switch-expression must yield a value of int (byte, long) or char type since each case evaluates an == test. The value1, …, and valueN must have the same data type as the value of the switch-expression. They cannot contain variables in the expression. The keyword break is optional. The break statement immediately ends the switch statement. The case statements are checked in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end. See pages for more detail.

14 int score = ???; switch (score/10) { case 10: case 9: letterGrade = ‘A’; break; case 8: letterGrade = ‘B’; case 7: letterGrade = ‘C’; case 6: letterGrade = ‘D’; default: letterGrade = ‘F’; } //switch System.out.println(“Your letter grade is “ + letterGrade); //Rewrite using nested if-else.

15 Write an equivalent nested if-else.
//suppose score is out of 100 ans = score / 10; switch (ans) { case 0: case 1: case 2: case 3: case 4: case 5: System.out.println(“Grade is F”); break; case 6: System.out.println(“Grade is D”); case 7: System.out.println(“Grade is C”); case 8: System.out.println(“Grade is B”); case 9: case 10: System.out.println(“Grade is A”); default: System.out.println(“invalid grade!”); } // switch Write an equivalent nested if-else.


Download ppt "Lecture 3 Selection Statements"

Similar presentations


Ads by Google