Why using i += 1 instead of i++ in the third part of for declaration? (Example) (2024)

Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (1)

Carlos Ramirez

5,084 Points

Posted by Carlos Ramirez

Carlos Ramirez

5,084 Points

Why using i += 1 instead of i++ in the third part of for declaration. I have seen many videos which uses i += 1. Why?

2 Answers

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (2)

Greg Kaleka

39,019 Points

Hey Carlos,

These two are exactly the same. It's just two different ways of writing the same thing. i++ is just a shortcut for i += 1, which itself is a shortcut for i = i + 1. These all do the same thing, and it's just a question of how explicit you want to be.

Cheers Why using i += 1 instead of i++ in the third part of for declaration? (Example) (3)

-Greg

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (4)

Carlos Ramirez

5,084 Points

Carlos Ramirez

Carlos Ramirez

5,084 Points

Thank you. My question is why teachers in treehouse decide to use i += 1 instead of i++ (which is more commonly used at least in Java). You know i += 1 is harder to write and also is more easy to make mistakes. I saw a question months ago which someone following the courses wrote i =+ 1. What I mean is that must be a very good reason for using i += 1, I want to know which is.

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (5)

Greg Kaleka

39,019 Points

There's not really a reason other than preference.

If you want a reason for why someone might have a preference for += 1 over ++, a recent change to Swift (for iOS programming) gives a great explanation.

Swift did away with the ++ operator altogether. Here are the reasons Chris Latner, the creator of Swift, cited (not all of these apply to Javascript, but many do):

  1. These operators increase the burden to learn Swift as a first programming language - or any other case where you don't already know these operators from a different language.
  2. Their expressive advantage is minimal - x++ is not much shorter than x += 1.
  3. Swift already deviates from C in that the =, += and other assignment-like operations returns Void (for a number of reasons). These operators are inconsistent with that model.
  4. Swift has powerful features that eliminate many of the common reasons you'd use ++i in a C-style for loop in other languages, so these are relatively infrequently used in well-written Swift code. These features include the for-in loop, ranges, enumerate, map, etc.
  5. Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage "overly tricky" code which may be cute, but difficult to understand.
  6. While Swift has well defined order of evaluation, any code that depended on it (like foo(++a, a++)) would be undesirable even if it was well-defined.
  7. These operators are applicable to relatively few types: integer and floating point scalars, and iterator-like concepts. They do not apply to complex numbers, matrices, etc.

Finally, these fail the metric of "if we didn't already have these, would we add them to Swift 3?"

Here's a Stack Overflow thread specific to Javascript that has good arguments on both sides of the "debate".

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (6)

Steven Parker

218,654 Points

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (7) Note that "i++", "i += 1", and "i = i + 1" are all the same when used as stand-alone statements. But when used as an expression, "i++" returns the value before incrementing, while the others return the final value. A fourth option is "++i" which also returns the final value.

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (8)

Greg Kaleka

39,019 Points

Good point Steven - that distinction is really the reason for item 6 on Chris's list. The fact that you can do that is also the reason for # 5. If you're taking advantage of the difference between i++ and ++i, you're probably getting too cute.

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (9)

Steven Parker

218,654 Points

I don't think I'd use the term "cute", but I agree that making use of the difference between the pre-increment and post-increment operators is definitely an advanced topic and not recommended for beginning programming. I only brought it up as a point of information.

For most students, I recommend sticking to the addition assignment operator ("+=") for consistency and readability.

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (10)

Greg Kaleka

39,019 Points

My mistake Why using i += 1 instead of i++ in the third part of for declaration? (Example) (11)

Sure - there are arguments to be made for the operators. They exist, after all! Absolutely worth bringing up.

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (12)

dylan jasper

1,928 Points

I watched a video from Douglas Crockford (guy who wrote JavaScript: The Good Parts) who said that he never uses the prefix or postfix ++ operator as some people don’t understand the difference and it can lead to mistakes. He just sticks with x += 1 style.

Posting to the forum is only allowed for members with active accounts.
Please sign in or sign up to post.

Why using i += 1 instead of i++ in the third part of for declaration? (Example) (2024)
Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 6285

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.