• 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

loop question doubt

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javaprepare.com/quests/operat_q.html
24.Which all lines are part of the output when the following code is
compiled and run. Select all correct answers.

public class test {
public static void main(String args[]) {
for(int i = 0; i < 3; i++) {
for(int j = 3; j <= 0; j--) {
if(i == j) continue;
System.out.println(i + " " + j);
}
}
}
}

A.0 0
B.0 1
C.0 2
D.0 3
E.1 0
F.1 1
G.1 2
H.1 3
I.2 0
J.2 1
K.2 2
L.2 3
M.3 0
N.3 1
O.3 2
P.3 3
Q.The program does not print anything.
correct answer is q, how ??? is it bcoz the second loop checks for j<=0 ???
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your guess is correct.
The print statement is inside the inner for statement, which never gets to run because the index counter is initialized to a value which does not meet the qualification to run.
I think they were trying to fool you into thinking that "j--" executes, and the next time you try that for statement, you'll begin with j initialized to 2 instead of 3. That never happens, of course, because you've exited the for loop and are starting it again fresh with the same incompatible values.
 
reply
    Bookmark Topic Watch Topic
  • New Topic