• 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

Something is fishy out here..

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will the following program print?

Options :
1)0 3
2)0 2
3)3 0
4)3 3
5)2 2
Answer : 4)3 3
My question is i<3, so how can 'i' ever reach to value 3 in the output
Sonir
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

well the above code will iterate
i = 0, j = 3 and then continue called
i = 1, j = 3 and then continue called
i =2, j = 3 and then continue called
and then i = 3 then compared to i > 3
( Is 3 > 3 ) will return false
So code will print
i = 3 & j = 3
Hope this helps!!!
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amish,


and then i = 3 then compared to i > 3


but i<3, so how can i ever reach 3???
Sonir
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonir
Sorry I meant ( Is 3 < 3 ) will return false
The value of i does reach 3 and at 3 your outer exits. Think of for loop like this

first do the comparison
then execute your block
& at the end do the increement

for e.g.
int i = 0;
for ( i = 1; i < 3; i++; )
{
}
before even u execuet the for loop u would do the intialization
which is i = 1;
Iteration 1
then you would do the comparison
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonir
Sorry I meant ( Is 3 < 3 ) will return false
The value of i does reach 3 and at 3 your outer exits. Think of for loop like this

first do the comparison
then execute your block
& at the end do the increement

for e.g.
int i = 0;
for ( i = 1; i < 3; i++; )
{
// body of the loop
}
before even u execuet the for loop u would do the intialization
which is i = 1;
you would do the comparison
execute the body of the loop
then increment your i
again do the comparison
execute the body of the loop
then increment your i
The important thing to remember is that in a for loop the increment occurs after the body of the loop executes.
Hope this helps
 
reply
    Bookmark Topic Watch Topic
  • New Topic