Java Reference
In-Depth Information
false
score >= 90
false
true
score >= 80
grade = 'A'
false
true
score >= 70
grade = 'B'
false
true
score >= 60
grade = 'C'
true
grade = 'D'
grade = 'F'
F IGURE 3.5
You can use a multi-way if-else statement to assign a grade.
The if statement in Figure 3.4a is equivalent to the if statement in Figure 3.4b. In fact,
Figure 3.4b is the preferred coding style for multiple alternative if statements. This style, called
multi-way if - else statements , avoids deep indentation and makes the program easy to read.
multi-way if statement
3.8
Suppose x = 3 and y = 2 ; show the output, if any, of the following code. What is
the output if x = 3 and y = 4 ? What is the output if x = 2 and y = 2 ? Draw a flow-
chart of the code.
if (x > 2 ) {
if (y > 2 ) {
z = x + y;
System.out.println( "z is " + z);
Check
Point
}
}
else
System.out.println( "x is " + x);
3.9
Suppose x = 2 and y = 3 . Show the output, if any, of the following code. What is
the output if x = 3 and y = 2 ? What is the output if x = 3 and y = 3 ? ( Hint : Indent
the statement correctly first.)
if (x > 2 )
if (y > 2 ) {
int z = x + y;
System.out.println( "z is " + z);
}
else
System.out.println( "x is " + x);
 
Search WWH ::




Custom Search