Operators confusion | Sololearn: Learn to code for FREE!


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

5/22/2017 3:37:25 PM

Mitchell Stallard

29 Answers

New Answer


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


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


@Rrestoring Faith. Faith has officially been restored. +1 Someone please bookmark this thread.


Haha.. @Restoring faith...😂😂😂 Savage


Haha .. True @Shamima


@Rrestoring Faith, it'd be easier for you if you explained instead 😂


Well at least someone uses the search function @Rrestoring faith. Kudos m8.


Restoring Faith lolol I might as well add you XD you always answer quickly and effectively. Thank you!! I appreciate you.


Please refer to this code for reference https://code.sololearn.com/W9BBJnbCBEv7/?ref=app


😷😷 following


that is an impressively long list lmao




++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


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


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.


Programming in c++


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.


Find and write the output of the following python code: def fun(s): k=len(s) m=" " for i in range(0,k): if(s[i].isupper()): m=m+s[i].lower() elif s[i].isalpha(): m=m+s[i].upper() else: m=m+'bb' print(m) fun('[email protected]') How the output is SCHOOLbbbbCOM


Programacion lineal


Int main(); { Int x,y; X=5; Y=++x; //here x=6 & y=6 Int x,y; X=5; Y=x++; /* here the value of x increases and value of y will be 5 will not increment. */