• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Conditional Statements

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic