• 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

How a semicolon after for loop affect the statement below loop

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


Can anyone tell me what ; means after for loop . I know it do nothing but actually in my code it increment the last cell of array let say my first array of data is [0,1,2] then it goes to [0,1,3]...[0,1,6],[0,2,6],[0,3,6]...[0,5.6], [1,5,6],[2,5,6]...[4,5,6] . I want to know how this loop is working and why produce this output .

Note: n,k is a parameter i put from cmd and in that case i put n=7 and k=3
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means that there is no loop body.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:It means that there is no loop body.


It also means that there may be a side effect to the loop itself. In this case 'i' is not local to the loop but gets modified by the loop and the modified 'i' is available after the loop completes.
 
johnsoan smith
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok , how then this is incremented . This is what i do not get
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indent your code properly and it's easier to see the structure of the code.

Proper indentation isn't just something nice; it's mandatory for code to be considered acceptable.

(Hint: it will also show that the code snippet you posted is either incomplete or non-compilable.)
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

johnsoan smith wrote:Ok , how then this is incremented . This is what i do not get


Not sure what you're asking here. I mentioned that the loop will calculate 'i'.

Also, as a matter of simplifying the syntax:
A = A + B

can be changed to
A += B

therefore your expression could have been written as
ans.data[i] += 1

There is a special increment operator (++) that can be used when the right side is 1
ans.data[i]++
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:It means that there is no loop body.

I am going into pedantic mode; it means the ; constitues the loop body and ; on its own is the empty statement. That means the loop body does nothing.

To avoid such problems we have some recommendations about formatting: note the bit about braces. You are using two loops with the same loop variant (i); they wil interfere with each other and there is a risk of your going into an infinite loop.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you are using "i" as the loop counter of both the inner loop and the outer loop. As written, if this.k is greater than 1 the outer loop may never terminate.
 
reply
    Bookmark Topic Watch Topic
  • New Topic