Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

I am not getting the required output...  RSS feed

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Options:
1)7
2)8
3)9
4)10
5)11
Answer :10
According to me when k is incremented to 1 i.e it is greater than j(0)..so It will break the loop at the first instance ..
So how did 'c' get the value 10??
Sonir
 
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonir,
the break statement only breaks the enclosing loop, the others are fine...
so the inner loop is broken every time k is bigger than j.
A good thing is too print the values of i,j and k
before the if statement.
You get:
i=0,j=0,k=0,c=1
i=0,j=0,k=1,c=2
i=0,j=1,k=0,c=3
i=0,j=1,k=1,c=4
i=0,j=1,k=2,c=5
i=1,j=0,k=0,c=6
i=1,j=0,k=1,c=7
i=1,j=1,k=0,c=8
i=1,j=1,k=1,c=9
i=1,j=1,k=2,c=10
Now you can see that all variables (i,j,k) are incrementing but as soon as k gets bigger than j the inner loop is broken...
HIH
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!