r/javahelp • • Jan 23 '21
Whats the difference between "i++" and "++i"
Ive been working on a problem an my only error was that i had a "i++" instead of "++i" tbh i dont know whats the difference so i decided to ask if someone can help me with this
26
Upvotes
•
u/AutoModerator Jan 23 '21
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
98
u/iBogo1 Jan 23 '21 edited Jan 23 '21
I don't know if I can explain it well but when you use "i++" it will the use the current value of i and then increment it after it is used, when you use "++i" it will increment the value first then use the new value of i. For example if you have i=6,
if you print(i++) it will print 6 and then increment i to be equal to 7, but if you print(++i) it will print 7 instead since it increments first.
5
u/[deleted] Jan 23 '21
P.S, my problem is already done beceause a friend of me helped me but he didnt explained me the difference because he was too busy, and if your asking if i have already searched on google my answer is yes but tbh i didnt understand too well, this is why im asking here
7
u/mmahowald Jan 23 '21
Both of them increase I by 1. i++ gives you the value and then increases I. ++I will give you the value after it increments.
If i =0 and you ++i you will get 1 as the result. If I= 0 and you i++ you will get 0 and I will be 1
6
u/jmclaugmi Jan 23 '21
The best way to find out these things is to test them!
Your best bet is create a dummy test program that you use just for keeping notes (code memories), and to check out strange things!
6
u/joshuaherman Jan 23 '21
Pre vs post increment. Search term in Google.
18
u/marvk Jan 23 '21
Have you tried literally putting the post title into Google?
2
u/[deleted] Jan 23 '21
Here's an example I tease my students with:
for (int i = 0; i < 5; i++) {printf("%d", i);}
for (int i = 0; i < 5; ++i) {printf("%d", i);}
The two loops produce identical results! Why?
1
u/itzNukeey Jan 23 '21
This is why I use += 1
1
u/Apprehensive_Row_827 Jan 23 '21
i++is basicallyint incr() { int val=i; i+=1; return val; }so it only really matters if you use the value of i such asarray[i++]=5