Download presentation
Presentation is loading. Please wait.
1
Selections Java
2
A program can decide which statements to execute based on a condition
Java provides selection statements – statements that let you choose actions with alternative courses Selection statement use conditions that are Boolean expressions (true or false)
3
Boolean Data Type Declares a value with a data with a value either true or false Are literals For example: boolean lightsOn = true;
4
Comparing Variables Can variables be compared? For example, how do you know if the radius is larger than zero? How to compare 2 values? By using Comparison Operators – or Relational Operators. These are the same operators used in math!
5
Comparison Operators Relational Operators
Operator Name < strictly less than <= less than or equal to > strictly greater than >= greater than or equal to == equal != not equal
6
Comparison Operators == equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to
7
Examples int value1 = 1; int value2 = 2; if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 > value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 <= value2) System.out.println("value1 <= value2");
8
DO NOT DO!!!! Use relational operators incorrectly:
1 <= numberOfDaysInAMonth <= 31 Should be: 1 <= numberOfDaysInAMonth && numberOfDaysInAMonth <= 31 Use the assignment instead of relational: if (a = b){ if (a == b){ Use relational operators with String type Use relational operators with care for float and double type.
9
If statements Sometimes in your programs you need choices. if statements can provide choices. For example, if it is raining, I want an umbrella The if part is executed when the condition is true There is only one if
11
example if (radius >= 0){ area = radius * radius * PI; System.out.println(“The area “ + for the circle of of radius “ + radius + “ is “ + area); }
12
Note
13
Caution Adding a semicolon at the end of an if clause is a common mistake. if (radius >= 0); { area = radius*radius*PI; System.out.println("The area for the circle of radius " + radius + " is " + area); } This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error. This error often occurs when you use the next-line block style. Wrong
14
The if...else Statement if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case;
15
Nested if…else Statments
When you need to make more than one choice.
16
Note The else clause matches the most recent if clause in the same block. Correct way
17
Note, cont. Nothing is printed from the preceding statement. To force the else clause to match the first if clause, you must add a pair of braces: int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) System.out.println("A"); } else System.out.println("B"); This statement prints B.
18
Multiple Alternative if Statements
Better way
19
Multi-Way if-else Statements
20
Trace if-else statement
animation Trace if-else statement Suppose score is 70.0 The condition is false if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F';
21
Trace if-else statement
animation Trace if-else statement Suppose score is 70.0 The condition is false if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F';
22
Trace if-else statement
animation Trace if-else statement Suppose score is 70.0 The condition is true if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F';
23
Trace if-else statement
animation Trace if-else statement Suppose score is 70.0 grade is C if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F';
24
Trace if-else statement
animation Trace if-else statement Suppose score is 70.0 Exit the if statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F';
25
Logical Operators The results of condition is a Boolean value:
True False Boolean values have Boolean Operators or Logical Operators. Logical operators are used to combine conditions to form compound Boolean expressions
26
Logical Operators are also called Boolean Conditionals
Name Meaning ! NOT logical negation If both the operands are true, then the condition becomes true. && AND logical conjuction If any of the two operands are true, then the condition becomes true. || OR Locigal disconjunction Use to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make false. ^ EXCLUSIVE OR XOR Logical exclusion “one or the other but not both”
27
Logical Operators with Boolean Operands
Unary Operators: NOT - ! Binary Operators: AND - &&, OR - ||, XOR - ^
28
Logical NOT boolean a = true; if (! a){ System.out.println ("a is not true" ); } else{ System.out.println (“a is true“);
29
Logical NOT boolean b = false; if (!(b == false) ){
System.out.println ("if statement because evaluated to true" ); } else{ System.out.println ("else statement because evaluated to false “);
30
Logical AND if ( a && b ){ System.out.println ("a and b are true“); } else{ System.out.println (" Either a is not true or b is not true , or both a and b are not true " );
31
Logical OR if ( a || b ){ System.out.println( "Either a is true or b is true or both are true" ); } else{ System.out.println ("Neither a is true nor b is true" );
32
Logical XOR if ( a ^ b ){ System.out.println( "Either a is true or b is true " ); } else{ System.out.println (“Either both a and b are true or both are false" );
33
Note
34
switch Statements switch (status) {
case 0: compute taxes for single filers; break; case 1: compute taxes for married file jointly; case 2: compute taxes for married file separately; case 3: compute taxes for head of household; default: System.out.println("Errors: invalid status"); System.exit(1); }
35
switch Statement Flow Chart
36
switch Statement Rules
The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. Note that value1, ..., and valueN are constant expressions, meaning that they cannot contain variables in the expression, such as 1 + x.
37
switch Statement Rules
The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. The case statements are executed 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.
38
Trace switch statement
animation Trace switch statement Suppose ch is 'a': switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); }
39
Trace switch statement
animation Trace switch statement ch is 'a': switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); }
40
Trace switch statement
animation Trace switch statement Execute this line switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); }
41
Trace switch statement
animation Trace switch statement Execute this line switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); }
42
Trace switch statement
animation Trace switch statement Execute this line switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); }
43
Trace switch statement
animation Trace switch statement Execute next statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Next statement;
44
Trace switch statement
animation Trace switch statement Suppose ch is 'a': switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); case 'c': System.out.println(ch); }
45
Trace switch statement
animation Trace switch statement ch is 'a': switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); case 'c': System.out.println(ch); }
46
Trace switch statement
animation Trace switch statement Execute this line switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); case 'c': System.out.println(ch); }
47
Trace switch statement
animation Trace switch statement Execute this line switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); case 'c': System.out.println(ch); }
48
Trace switch statement
animation Trace switch statement Execute next statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Next statement;
49
Conditional Operator if (x > 0) y = 1 else y = -1; is equivalent to
y = (x > 0) ? 1 : -1; (boolean-expression) ? expression1 : expression2 Ternary operator Binary operator Unary operator
50
Conditional Operator if (num % 2 == 0)
System.out.println(num + “is even”); else System.out.println(num + “is odd”); System.out.println( (num % 2 == 0)? num + “is even” : num + “is odd”);
51
Conditional Operator, cont.
(boolean-expression) ? exp1 : exp2
52
Order of Operations Just as in math, the operators have a precedence order for deciding what operators are evaluated first.
53
Operator Precedence and Associativity
The expression in the parentheses is evaluated first. (Parentheses can be nested, in which case the expression in the inner parentheses is executed first.) When evaluating an expression without parentheses, the operators are applied according to the precedence rule and the associativity rule. If operators with the same precedence are next to each other, their associativity determines the order of evaluation. All binary operators except assignment operators are left-associative.
54
Operator Precendence Operator Symbols postfix expr++ expr--
unary ++expr --expr +expr –expr (type)(casting) (int)(3.02) NOT ! multiplicative * / % additive + - shift << >> >>> relational < > <= >= instanceof equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || Ternary ? : assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
55
Operator Associativity
When two operators with the same precedence are evaluated, the associativity of the operators determines the order of evaluation. All binary operators except assignment operators are left-associative. a – b + c – d is equivalent to ((a – b) + c) – d
56
Assignment operators Assignment operators are right-associative. Therefore, the expression a = b += c = 5 is equivalent to a = (b += (c = 5))
57
Example Applying the operator precedence and associativity rule, the expression * 4 > 5 * (4 + 3) - 1 is evaluated as follows:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.