r/javahelp • u/[deleted] • 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
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.
2
Jan 23 '21
This is an excellent answer, just providing some code
int i = 6; System.out.ptintln(i++); // output is 6, because it's used by the method before the increment //At this point i has a value of 7On the other hand
int i = 6; System.out.ptintln(++i); // output is 7, because it's incremented before it's used by the method //At this point i has a value of 7Preincrement and postincrement are different only when used as parameters.
int i = 6; i++; // or ++i // At this point i has the value of 7 either you used ++i or i++ System.out.ptintln(i);I don't remember the source, but once I read that preincrement and postincrement (used as parameters) are considered bad practice because they can be hard to read and error prone. And I agree
1
18
u/funnythrone Jan 23 '21
You did explain it well. :)
1
u/OutOfTempo_ Jan 23 '21
If you replace x with i++ it prints "5 1" and if you write ++i instead it prints "6 1".
I don't remember java straw when not at my computer sorry, so I wrote it in C instead.
int i = 0; int a[2] = {5, 6}; printf("%d d", a[x], i);
1
1
u/sepp2k Jan 23 '21
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
This isn't entirely correct. Given a function call
f(i++);, the value ofiwill be incremented beforefis called, but the argument tofwill be the old value ofi, not the incremented value.To see the difference, consider this code:
class Foo { static int i = 42; static void print(int x) { System.out.println("The argument to print is " + x); System.out.println("The value of i is " + i); } public static void main(String[] args) { print(i++); } }When the body of
iwill already be43, soiwas incremented before the call to
5
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
2
u/macnor Jan 23 '21
So you mentioned that your error was "i++" instead of "++i". What was the actual error. Were you trying to print out the "i++" and it was off by one perhaps?
10
u/bizzygreenthumb Intermediate Brewer Jan 23 '21
Mf off by one errors.
There’s only two hard problems in computer science: cache invalidation, naming things, and off by one errors.
2
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
18
2
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
1
u/Apprehensive_Row_827 Jan 23 '21
i++ is basically int incr() { int val=i; i+=1; return val; } so it only really matters if you use the value of i such as array[i++]=5
•
u/AutoModerator Jan 23 '21
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
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.