#Does order of incrementing/decrementing multiple variables matter in for loops?

1 messages · Page 1 of 1 (latest)

worthy notch
#

So I was trying to solve N-Queen's problem using java and tried to use

for (int r = row, c = col; r >= 0 && c < board.length; r--, c++) {
    // Loop body
}

which according to me should check the upper right diagonal, but for some reason my solution was producing weird output and I checked some solutions where they have used

for (int r = row, c = col; r >= 0 && c < board.length; c++, r--) {
    // Loop body
}

So that got me to wonder does this order of c++, r-- in the for loop matters? If yes, how exactly?

quasi hingeBOT
#

This post has been reserved for your question.

Hey @worthy notch! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

clear canopy
#

No the order of incrementation does not matter. It also doesn't matter if you use ++c instead of c++

#

The order would only matter if one variable is dependent on another.
For example:

for (int r = 10, c = 0; r >= 0; r-=c, c++) //6 iterations
for (int r = 10, c = 0; r >= 0 ; c++, r-=c) //5 iterations
worthy notch
#

and tbh I didn't really understood his point

#

if we are doing c++ how can we go left. it just doesn't make any sense

clear canopy
#

The movement described is an upwards right diagonal. Regardless of the order of incrementation because the variables are not dependant on one another

worthy notch
#

thanks!

quasi hingeBOT
# worthy notch thanks!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.