Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If y = 1 and y = y++, why when I print y is the value 1? [duplicate]

What happens (behind the curtains) when this is executed?

int x = 7; x = x++; 

That is, when a variable is post incremented and assigned to itself in one statement? I compiled and executed this. x is still 7 even after the entire statement. In my book, it says that x is incremented!

like image 636
Michael Avatar asked Oct 27 '11 04:10

Michael


People also ask

What is the == in Python?

What Is the “==” Operator? There is a comparison operator (==) in Python used to measure the Python equality of two objects. It is also known as the equality operator (==). The above example, where we have compared the two empty lists using the (is) operator, did not provide the expected results.

How do you break an if statement in Python?

break statement is put inside the loop body (generally after if condition). It terminates the current loop, i.e., the loop in which it appears, and resumes execution at the next statement immediately after the end of that loop. If the break statement is inside a nested loop, the break will terminate the innermost loop.

Is for loop conditional?

The While loop and the For loop are the two most common types of conditional loops in most programming languages.

What does == 0 mean in Python?

== 0 means "equal to 0 (zero)".


1 Answers

x = x++; 

is equivalent to

int tmp = x; x++; x = tmp; 
like image 149
Prince John Wesley Avatar answered Sep 22 '22 17:09

Prince John Wesley