Difference between i ++ and ++ i

Overview

The pre-increment operator (++ i) and the post-increment operator (i ++) are both operators that assign the result of i + 1 to i, but I will briefly explain the difference.

Pre-increment (++ i)

Returns the value of the expression after incrementing, that is, i + 1.

i = 1;
j = ++i; //j is 2

Post-increment (i ++)

Returns the value of the expression before incrementing, that is, the value of i.

i = 1;
j = i++; //j is 1

Supplement

for (int i = 0; i < n; ++i) for (int i = 0; i < n; i++)

Both are for loops that you often see, but if ++ i and i ++ appear independently like this and the value of the expression is not used, there is no difference in operation. Also, with recent compilers, there is no difference in execution speed depending on the optimization, so you can use the one you like.

Recommended Posts

Difference between i ++ and ++ i
Difference between vh and%
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
[Rails] I investigated the difference between redirect_to and render.
[Java beginner] Difference between length and length () ~ I don't know ~
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between addPanel and presentModally of FloatingPanel
[Ruby] Difference between print, puts and p
Difference between int and Integer in Java
[Rails] Difference between redirect_to and render [Beginner]
[Java] Understand the difference between List and Set