• 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

Could some one explain the follwoing..

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class JMM111 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i < 2; i++) do
System.out.print(i);
while (j++ < 2);
}}

What is the result of attempting to compile and run the program?

a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Run-time error
j. Compile-time error
k. None of the above


Answer is A: 0001. why?
thanks in advance,
reena
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do {} while loop will print three times zero. It do something first and then evaluating the condition.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi...

jus watch that you have embedded a do while loop inside a forloop...

so first time it will enter into the do loop it will print the value tobe 0, then in will check for the while condition which will be 0 < 2....so The CATCH point here is it will execute only the do loop and not the for loop till j is 2...so only 0 will get printed...when j>2 for loop will be executed wherein i becomes 1....

i guess you understand..
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code is the same as:



Hope it helps
 
Radoslaw Sztando
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code is the same as:



Hope it helps
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The for statement executes the first statement after the for since there are no {}
the first statement after the for is the do
so its because of the do statement that the print happens..and when it goes to the next iteration of the do then println does not execute...because j>2
try this and it should be clear

class A {
public static void main (String[] args) {
int j = 0;

for (int i = 0; i < 2; i++)
do
{
System.out.print(" J:" + j);
System.out.print(" I:" + i);
}
while (j++ < 2);

}}
reply
    Bookmark Topic Watch Topic
  • New Topic