• 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

khalid question on loops

 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
outer:
for(int i=0; i<3; i++){
for(int j= 0; i<2; i++){
if(i==j){
continue outer;
}
System.out.println("i=" + i + ", j=" + j);
}
}
Select all valid answers.
(a) i=1, j=0
(b) i=0, j=1
(c) i=1, j=2
(d) i=2, j=1
(e) i=2, j=2
(f) i=3, j=3
(g) i=3, j=2
The answer is (a) and (d) could somebody who understands this please try to explain this. Go slow because I just don't get it.
Thanks.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(i=0;i<3;i++)
first iteration it will assign i=0 and execute
second iteration it will increment it by 1 -now i =1 and verify that i<3 it is tru so it executes.
Go on doing same for next iterations
You will get it
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anjan I understand the loop (i=0; i<3; i++). I am having trouble with the nested loop that i specified above.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for i=0 to 2, j can have values 0 and 1 only.
for i=0, j=0 so inner loop breaks and control goes to next i.
i.e. i=1, then again inner loop goes for j=0 and 1.
for i=1, j=0 , condition not satisfied , print i=1, j=0.
next j=1, condition satisfied, go to outer loop for next i.
i.e. i=2,
for i=2, j=0, condition satisfied, print i=2, j=0.
next j=1, condition satisfied, print 1=2, j=1.
inner loop condition false (j<2) as j becomes 2 now.
go to outer loop incrementing i to 3.
outer loop condition also false (i<3). program stops.
From the above three answers :
1) i=1,j=0 2) i=2,j=0 3) 1=2,j=1.
1) and 3) are given as choice and hence a) and d) are the options to be selected.
Pls. give any remarks if any.
with love and happy christmas and new year wishes
Bye
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic