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

# Is any simple RULE avialable for this kind...

Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:
class xyz {

public static void main(String args[]) {
int i,j,k;
for (i = 0; i < 3; i++) {
for(j=1; j < 4; j++) {
for(k=2; k<5; k++) {
if((i == j) && (j==k))
System.out.println(i);
}
}
}
}
what will be the output?
any rule/maths avialable for this kind of problems
}

Ranch Hand
Posts: 241
• Number of slices to send:
Optional 'thank-you' note:
Hi, Balaguru.
I'm not sure what kind of magic bullet you're looking for....Usually, with this kind of question, you have little choice but to put yourself in the JVM's shoes and forge ahead. But in specific cases, you may be able to apply logic to make your work easier:
For example, here, "i" is only printed if i == j and j == k. "i" will take on values of 0, 1 or 2; j will take on values of 1, 2 or 3; and k will take on values of 2, 3 or 4. Since these for loops are nested, all permutations will be subject to the if-test, and the only one where all three integers can be equal is the case where i == j == k == 2.
Other than that, just know that the innermost loop will run through all its iterations before the next-outermost loop iterates once. When I'm confronted with a question like this, and if I can't logically distill the problem to an answer quickly, I create three columns: "i", "j", and "k", and change their values as I mentally loop through the code.
Good luck, Balaguru.
Art

Balaguru Janarthanan
Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:
Thank You, Art.