This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Conditional Statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Conditional Statements" Watch "Conditional Statements" New topic
Author

Conditional Statements

Werner Otto
Greenhorn

Joined: Sep 28, 2003
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);
}
}
Answer: 8,4
Can anyone explain to me how to reach this answer?
Elliot Shanahan
Greenhorn

Joined: Sep 30, 2003
Posts: 2
Originally posted by Werner Otto:
Problem:
Answer: 8,4
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

Joined: Oct 01, 2003
Posts: 252
    
    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;
}
i++; // added this semicolon
} while (i++ < 7);
System.out.println(i + "," + j);
}
}
After making this fixes, I got the output 8,4, as you suggest.


For my latest books on Java, including Introducing JavaFX 8 Programming, see HerbSchildt.com
CallMeWhatever Otto
Greenhorn

Joined: Sep 12, 2003
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conditional Statements
 
It's not a secret anymore!