+ 50

Operators confusion

This is where I am getting completely lost... Int x = 34 (declaring x is equal to 34) Int y = ++x; (no idea what this is declaring) //y is 35 (this is a note) Int x = 34; (stating x is 34) int y = x++ (Again, no idea what this is trying to declare) // y is 34 (another note) How the heck does this work, what are we doing/accomplishing by putting this in?.. I do not understand. Anyone please help with clarification. Also looking at the increment and decrements lesson I am also clueless as to what we are stating / trying to achieve with the formula. Or how it even works.. Please anyone lol

22nd May 2017, 3:37 PM
Mitchell Stallard
Mitchell Stallard - avatar
29 Answers
+ 44
Past explanations: (there are lots of answers to this question already here) <redacted list, original post spans 20+ threads on prefix/postfix operators. Newer threads were removed due to duplication> https://www.sololearn.com/Discuss/160327/?ref=app https://www.sololearn.com/Discuss/69892/?ref=app https://www.sololearn.com/Discuss/252933/?ref=app https://www.sololearn.com/Discuss/147409/?ref=app
22nd May 2017, 3:58 PM
Rrestoring faith
Rrestoring faith - avatar
+ 42
Explanation: int x = 34; Does NOT "declare x is equal to 34" It declares x as an integer variable and assigns 34 to it. int y = ++x; This declares y as an integer variable and assigns the value of x+1 to it. ++x is the same as x+1 This is known as a Pre-increment and it increases the value of x before executing the statement. In this case ++x means that the value of x has increased by 1 and is now 35. So, y=35; int x = 34; (34 is assigned to x) int y = x++; This declares y as an integer variable and assigns the value of x to it but the value will be increased by one after being used. x++ is also the same as x+1 This is known as a Post-increment and unlike the Pre-increment, it increases the value of x after executing the statement. In this case x++ means that the value of x is still unchanged until after it is first used. So, //1st execution System.out.println("y="+y); //y=34 //2nd execution to test the value of x System.out.println("x="+x); //x=35 https://code.sololearn.com/csst17nt85Iz/?ref=app
23rd May 2017, 1:10 AM
STAPS Guru
STAPS Guru - avatar
+ 26
Haha.. @Restoring faith...😂😂😂 Savage
22nd May 2017, 4:04 PM
Frost
Frost - avatar
+ 25
Haha .. True @Shamima
22nd May 2017, 4:14 PM
Frost
Frost - avatar
+ 25
@Rrestoring Faith. Faith has officially been restored. +1 Someone please bookmark this thread.
22nd May 2017, 4:18 PM
Hatsy Rei
Hatsy Rei - avatar
+ 22
@Rrestoring Faith, it'd be easier for you if you explained instead 😂
22nd May 2017, 4:09 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 19
Well at least someone uses the search function @Rrestoring faith. Kudos m8.
22nd May 2017, 10:30 PM
Ace
Ace - avatar
+ 15
Restoring Faith lolol I might as well add you XD you always answer quickly and effectively. Thank you!! I appreciate you.
22nd May 2017, 5:57 PM
Mitchell Stallard
Mitchell Stallard - avatar
+ 12
Please refer to this code for reference https://code.sololearn.com/W9BBJnbCBEv7/?ref=app
22nd May 2017, 4:19 PM
Siddharth Saraf
+ 6
😷😷 following
1st Jun 2017, 1:23 PM
Isomer
Isomer - avatar
+ 6
that is an impressively long list lmao
10th Aug 2017, 2:05 AM
Jordan Chapman
Jordan Chapman - avatar
25th Jun 2017, 12:09 PM
Limitless
Limitless - avatar
+ 4
++a and a++ are equals... in there own why Int a = 3, b = 5 ; ++a; Cout << a; B++; Cout << b; Output will be 4 6 (A nd b will both increment by 1) But post increment or decrement will use with assingment operator " = " then ++a(pre increment) & a++(post increment) are have different reasults lets see Int a = b = c = d = 0; a = b++; c = ++d; Cout << a << endl << b << endl << c << endl << d; Output : 0(here post increment assing its value to a and the increamnt by 1) 1 (now B's value is 1 bcz of increament) 1( Here first D is increment bye 1 then assing its value to C 1 I hope u will understand
9th Dec 2018, 4:51 PM
waqar younis
waqar younis - avatar
+ 3
so when you use ++ before an integer, 1 is added to that integer on that line. therefore, x = 16 y = ++x; ( y equals x plus 1) then y = 17 and x = 17 but if you use ++ after an integer the plus one wont be added until after the line it was used on. so, x = 80 y = x++ (y equals x and plus one is added afterwards so y is just...) y= 80 but x = 81 because we still added one.
5th Aug 2020, 10:11 PM
Kevin Holmes
+ 2
When we use the increment at the beginning ie pre increment it will first increment the value and then store it to the variable while in the other case it will first assign the value to the variable and after that it increments the x
25th Sep 2019, 7:22 AM
Syed Ali Haider
Syed Ali Haider - avatar
+ 1
Programming in c++
4th Oct 2019, 4:49 AM
Md Tausif Rja
Md Tausif Rja - avatar
+ 1
Programacion lineal
11th Nov 2019, 12:01 AM
Carolina Elizabeth Ramirez
Carolina Elizabeth Ramirez - avatar
+ 1
Hmm
17th Jul 2020, 1:10 AM
Augustine Gale Mensah
Augustine Gale Mensah - avatar
+ 1
Look It is very simple, just try to understand it with a calm mind. In your first example : int x=34; int y= ++x; // this is a prefix increment operator. This operator first increments the value of x by 1 and then uses the new value. A simple way to remeber this is : Prefix : increment and then use In your second example : int x = 34 ; int y = x++; // this is a postfix increment operator. This uses the initial value first (in this case 34) and then increments the value. Take another example of postfix : int x = 34 ; int y = x++ ; int z = x ; In is code, y =34 and z=35 This shows that the value of "x" is used first to declare value of "y" , then the value of "x" is incremented by 1. Then the value of "x" is assigned to "z". An easy way to remeber postfix increment operator is : Use and then increment Hope your doubt is cleared. If you still have any doubts, you can reach out to me, I'll clear them all.
2nd Aug 2020, 7:22 AM
Aayush Joshi
Aayush Joshi - avatar
+ 1
Вообще понять не могу все это
24th Mar 2021, 11:33 AM
Бахытхан Арысбаев
Бахытхан Арысбаев - avatar