• 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

Nested loop

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can anyone explain how the answer is 012345 for the following nested loop mock exam question:



regards
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

outer while loop loops for three times.

first loop : inner do-while loops for four times j=0,j=1,j=2,j=3

second loop: since do-while j=4

third loop: j=5

outer loop breaks as i became 3

thankyou
Srinivas
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The outer while loop will run three times, with i = 0, with i = 1, and then with i = 2.

The first time the do loop runs, it will run 4 times. with j = 0, with j = 1, with j = 2, and then with j = 3. The reason there seems to be an extra iteration is because the condition check for the do-loop is at the end.

The second and third time the do-loop runs, they will both run once each -- with j = 4, and with j = 5. Just as before, since the checking is at the end, it seems to run at least one extra iteration.

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

System.out.print(j);
while (j++ < 3);}}
 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class JMM114
{
public static void main (String[] args)
{
int i = 0, j = 0;
while (i++ < 3)
{
do
{
System.out.print(j);
}while (j++ < 3);
}
}
}
so while(i++<3) is the outer loop. and do-while loop is inner loop. i think now ye doubt is cleard;
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...thanks for explanation.

this is a silly question but i want to know
On the first place, is it possible to have multiple lines in a while loop without starting and ending braces.

for eg. how "while(...)do{....}while(...);" is including multiple lines as the body of the outer while loop without braces.
Ideally it should be while(...){
do{....}
while(...);
}

as per my knowledge, without braces any loop will have only one (first) statement in the body of the loop.

why we are not getting problem in the above code
while(...)do{....}while(...);
reply
    Bookmark Topic Watch Topic
  • New Topic