0
Module Quiz 3
Two parts on the quiz(Q. 1, 2) bot confused me and cant find an explanation to online. My first question is: 'What is "i++" or "++i" and what do the represent/stand for?' and 'What is "+=" or "=+" and what does that represent/stand for?' If you look at the first two questions of the "Array" module quiz, there are examples of both of these within the question. Thank you.
3 Respostas
+ 2
i++ is a post increment and ++i is a pre increment. There are also pre and post decrement --i, i--
With the post version, the value of the variable i is first used and then the variable i is incremented/decremented. When using the pre version, the value of the variable i is first incremented/decremented and then that new value is used.
int i = 0;
int j = i++; // j is set to 0 then i is incremented to 1
int k = ++i; // i is first incremented from 1 to 2 and then k is set to 2
System.out.println(j++); // the value of j which is currently 0 is output then it is incremented to 1.
+= is the addition assignment operator
it is the same as:
x = x + y
int x = 1;
int y = 2;
x += y; // x is now 3
x += 4; // x is now 7
=+ is not a single operator, but may be read as two separate operators. The first being the assignment operator = and the second being the unary positive operator + (which is not needed) as all numeric literals/variables are positive by default if the unary negative operator - is not present.
int x =+6; // sets x to the value of 6
is the same as
int x = 6;
+ 2
Thank you! This helped me out a lot. It's much more clear. I keep trying to plug this into the first two questions on the quiz and can't seem to figure out how the answer was achieved. I'm probably just having a brain-fart, thank you for the help!
0
i just want a simple answer :(