• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

enhanced for loop problem -- from a mock

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this code from a mock .....


output is ::

1
2
3
4
arr[1] = 0
arr[0] = 0
arr[3] = 0
arr[0] = 0
After ....
0
0
3
0


Why in the second loop array index's are like 1,0,3, 0 shouldn't it be arr[0], arr[1], arr[2], arr[3]
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the loop runs the first time, ii contains 1 as arr[0] contains the value 1. The loop prints arr[1]=0 (as ii contains the value 1). Then you set arr[ii] to 0 i.e. arr[1] is set to 0.
Then when the loop run's 2nd time, ii contains the value of arr[1] which is 0. The loop prints arr[0]=0 (as ii contains the value 0). Then you set arr[ii] to 0 at this point ii is 0 so you set arr[0] to 0.
Then when the loop run's 3rd time, ii contains the value of arr[2] which is 3. The loop prints arr[3]=0 (as ii contains the value 3). Then you set arr[ii] to 0 at this point ii is 3 so you set arr[3] to 0.
Then when the loop run's 4th time, ii contains the value of arr[3] which is 0. The loop prints arr[0]=0 (as ii contains the value 0). Then you set arr[ii] to 0 at this point ii is 0 so you set arr[0] to 0...
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good example for the fundamentals . Each time the for loop modifies the contents of the array and the iterator works accordingly !!
 
author
Posts: 23958
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

Js Kaur wrote:I got this code from a mock .....



Please QuoteYourSources -- meaning which mock?

Henry
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic