Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

Conditional Statements

Werner Otto
Greenhorn
Posts: 2
Problem:
class I {
public static void main(String[] args) {
int i = 0, j = 9;
do
{
if (j < 4) {
break;
} else if (j-- < 7)
continue;
}
i++
} while (i++ < 7);
System.out.println(i + "," + j);
}
}
Can anyone explain to me how to reach this answer?

Elliot Shanahan
Greenhorn
Posts: 2
Originally posted by Werner Otto:
Problem:
Can anyone explain to me how to reach this answer?

if 8,4 is what you want to get, all you need to change is:
while(i-- < 7) --> while(i < 7)
......I think...

Herb Schildt
Author
Ranch Hand
Posts: 253
6
Werner, your program had two errors. First, braces were out-of-balance after the else/if statement. Second, you were missing a semicolon after the i++ statement. Here is corrected version of your program:
class I {
public static void main(String[] args) {
int i = 0, j = 9;
do
{
if (j < 4) {
break;
} else if (j-- < 7) { // added this brace
continue;
}
} while (i++ < 7);
System.out.println(i + "," + j);
}
}
After making this fixes, I got the output 8,4, as you suggest.

CallMeWhatever Otto
Greenhorn
Posts: 3
Ok, now that we have the syntax correct, and we agree that the answer is 8,4.
Can someone please explain to me the logical sequence of exection that this
loop will follow, resulting in 8,4.