Professional Documents
Culture Documents
Chapter3 Control Statements IF
Chapter3 Control Statements IF
Statements
The if statement
Executes a block of statements only if a test is true
if (test) {
statement;
...
statement;
}
• Example:
double gpa = console.nextDouble();
if (gpa >= 2.0) {
System.out.println("Application accepted.");
}
2
The if/else statement
Executes one block if a test is true, another if false
if (test) {
statement(s);
} else {
statement(s);
}
• Example:
double gpa = console.nextDouble();
if (gpa >= 2.0) {
System.out.println("Welcome to Mars University!");
} else {
System.out.println("Application denied.");
}
3
Relational expressions
• if statements and for loops both use logical tests.
for (int i = 1; i <= 10; i++) { ...
if (i <= 10) { ...
• Example:
if (x > 0) {
System.out.println("Positive");
} else if (x < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
6
Nested if/else/if
– If it ends with else, exactly one path must be taken.
– If it ends with if, the code might not execute any path.
if (test) {
statement(s);
} else if (test) {
statement(s);
} else if (test) {
statement(s);
}
• Example:
if (place == 1) {
System.out.println("Gold medal!");
} else if (place == 2) {
System.out.println("Silver medal!");
} else if (place == 3) {
System.out.println("Bronze medal.");
7
}
Nested if structures
• exactly 1 path (mutually exclusive) • 0 or 1 path (mutually exclusive)
if (test) { if (test) {
statement(s); statement(s);
} else if (test) { } else if (test) {
statement(s); statement(s);
} else { } else if (test) {
statement(s); statement(s);
} }
– Whether you made the dean's list (GPA ≥ 3.8) or honor roll (3.5-3.8).
• (3) nested if / else if
9
Logical operators
• Tests can be combined using logical operators:
Operator Description Example Result
&& and (2 == 3) && (-1 < 5) false
|| or (2 == 3) || (-1 < 5) true
! not !(2 == 3) true
10
Evaluating logic expressions
• Relational operators have lower precedence than math.
5 * 7 >= 3 + 5 * (7 - 1)
5 * 7 >= 3 + 5 * 6
35 >= 3 + 30
35 >= 33
true
12
Factoring if/else code
• factoring: Extracting common/redundant code.
– Can reduce or eliminate redundancy from if/else code.
• Example:
if (a == 1) {
System.out.println(a);
x = 3;
b = b + x; System.out.println(a);
} else if (a == 2) { x = 3 * a;
System.out.println(a); if (a == 2) {
x = 6; y = y + 10;
y = y + 10; }
b = b + x; b = b + x;
} else { // a == 3
System.out.println(a);
x = 9;
b = b + x;
} 13
if/else with return
// Returns the larger of the two given integers.
public static int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
14
All paths must return
public static int max(int a, int b) {
if (a > b) {
return a;
}
// Error: not all paths return a value
}
– The compiler thinks if/else/if code might skip all paths, even
though mathematically it must choose one or the other.
15
if/else, return question
• Write a method quadrant that accepts a pair of real numbers
x and y and returns the quadrant for that point:
y+
quadrant 2 quadrant 1
x- x+
quadrant 3 quadrant 4
y-
17
switch Structures
If a programmer has to choose one among many alternatives,
if...else can be used but, this makes programming logic complex.
This type of problem can be handled using switch...case statement
Syntax:
switch (expression)
{
case constant1:
Note the colon!
Instruction/s to be executed if expression equals to constant1;
break;
case constant2:
Instruction/s to be executed if expression equals to constant3;
break;
….
default:
Instruction/s to be executed if expression doesn't match to any
cases;
}
18 18
switch Statements
Flowchart:
19 19
Switch Statement
• If the expression not matches any of the constant in case, then
the default statement is executed.
• when the value in a case matches the test value, all of the
actions in the rest of the structure take place.
• This can be corrected by use of the break statement.
20 20
Switch Statement Rules
1. Switch expression must be of integral Type ( int, char)
2. Switch case should have at most one default label
3. Default label is Optional
4. Default can be placed anywhere in the switch
5. Break Statement takes control out of the switch
6. Two or more cases may share one break statement
7. Nesting ( switch within switch ) is allowed.
8. Relational Operators are not allowed in Switch Statement.
9. Case Label must be unique
10. Case Labels must ends with Colon
11. break; is optional (but NEEDED! )
21 21
switch Example
switch (ch) {
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
System.out.println("vowel“);
break;
default:
System.out.println("not a vowel“);
}
22 22