Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

What is the point of having pre-increment operation in for-loop?

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this question incorrect in Enthuware:

What will be the output if you run the following program?




I thought the output would be

0 1
1 1

because ++j is preincrement. Then I read the explanation:

j will be less than 1 for only first iteration. So, first it'll print 0, 0. Next, i and j are incremented.
as j is not less than 1 at the start of the loop so the condition fails and it comes out of the loop. Finally, it will print 1,1.



So then what difference does it make if the code would have post-incremented j, i.e. j++ instead of ++j. Is this code design used in the real world? Or is this another exam trick question?

 
Saloon Keeper
Posts: 12303
259
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no difference. Be consistent, use either the post- or pre-increment operator.

I think one should always strive to write code that doesn't care whether you use one or the other, meaning, you should never use the result of the increment/decrement operations.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So then what difference does it make if the code would have post-incremented j, i.e. j++ instead of ++j. Is this code design used in the real world? Or is this another exam trick question?



No difference. Changing between them does not alter the flow in the loop in any way. They do however alter the time at which the ++ reflect on any said variable. Which of course, should not really matter in a good program, like Stephan pointed out.

You can find the minor difference using this modified snippet. Try changing ++j to j++ and vice versa.

 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that there is the potential for pre-increments in loops to change behavior dramatically. Try this code, then try changing the increment operator on j to a pre-increment.

(By the way - I deliberately wrote code that I think should never be used in real life: you should not have i = j = 0 or have some increment operations in the test for termination section of the loop. And you should never allow situations where the pre or post increment can have unexpected side effects. All of which I have broken in this code.)
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Stephan: That's a good point. Guess most people stick with post-increment (since that is what is mainly taught).

@ Deepak, @ Andrew: Thank you for the code. Changed it from post-increment to pre-increment and noticed the difference.
 
rubbery bacon. rubbery tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic