Differencebetween a += B and a =+ B , Also A++ and ++A

What is the difference between a += b and a =+ b , also a++ and ++a?

a += b is equivalent to a = a + b

a = +b is equivalent to a = b

a++ and ++a both increment a by 1.
The difference is that a++ returns the value of a before the increment whereas ++a returns the value after the increment.

That is:

a = 10;
b = ++a; //a = 11, b = 11

a = 10;
b = a++; //a = 11, b = 10

Difference between a+++++b and a++ + ++b

Since ++ is a token, the parser interprets a+++++b the same as a ++ ++ + b, which is not the same as a ++ + ++ b!

Difference between !(a==b) and a!=b

There is no difference in the semantics of the two expressions. I would say there is no good reason to ever write the former.

But now I am confused, because as a teacher I assume you know a lot of your subject, so could there be a good reason why she used her method over mine?

The best thing to do is to ask your teacher. The most important thing to remember about teachers - as human beings - is that they have experiences, prejudices and are fallible.

In other words, she might have been bitten by some problem in the past which was solved by writing it like this - ask her about it, you might learn something.

Or, she perhaps thinks it's better, and can't articulate a strong benefit over personal preference; or that was the way she learnt, and hasn't seen a strong need to change - you learn something from that, insofar as there is more than one way to articulate the same semantics.

(I have a strong preference for != because it's neater - fewer parentheses; and why would the language designers bother providing != if you weren't intended to use it - but these are my personal preferences).

Or, she maybe meant to use !=, and forgot to go back to fix it. It's easy to forget to clean things up.

Difference between a[:] = b and a = b[:]? (Python)

[:] is the slice operator.

When it's on the left side, it overwrites the contents of the list without creating a new reference.

When it's on the right side, it creates a copy of the list with the same contents.

Difference between a//b and int(a/b)

From int docs:

For floating point numbers, this truncates towards zero.

From // docs:

Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result.

What is the performance difference between a=b and (ab) OR (a==b)?

It depends on programming language, hardware and compiler, but, if you are about C++ over common x86 CPU and compiler that can not optimize this statement, the difference is about assebmly code generation.

On Assembler level, comparison is subtraction with further flags check. a <= b may become something like that:

cmp eax, ebx
jle a_le_b

We suppose that variables are in registers already to simplify the example. You see, we need 2 instruction only.

(a<b) | (a==b) literally means this:

cmp eax, ebx
jl a_le_b
cmp eax, ebx
je a_le_b

Again, here I translated the C++ to assembler without any optimization, just "as is". If compiler has optimization enabled, it will most probably generate the same code for both statements.

Now, in C++ the operators may be overloaded. It means that <= (theoretically) can perform completely different operations of different cost that < and =. However, in practice usually <= will be implemented exactly like call to < followed by call to == or vice versa.

Conclusion: in practice, for optimized compiler on common processor type and semantically correct class that calls comparison operator, you will not have any observable difference. However, semantics of these 2 statements are not exactly the same, so theoretically it is even possible that a <= b will give different result from a < b | a == b.

What is the difference between a = b and a = b[:]?

The difference between a and b here is that a is another reference to lst, but b is a reference to a new list. You can see this by modifying a. It results that lst is modified (since a and lst refer to the same object), but b is not modified.

>>> lst = [1,2,3] 
>>> a = lst
>>> b = lst[:]
>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
>>> a[0] = 3
>>> a
[3, 2, 3]
>>> b
[1, 2, 3]
>>> lst
[3, 2, 3]

However, although the operator [:] creates a copy rather than a reference, it still creates a shallow copy, which means that the copy is only one layer deep. In particular, this means that if the elements of the list are more complex objects, those objects will not be copied, but will just be references to the same objects in the new list. This can be seen by trying the same thing with a list of lists:

>>> list2
[[0, 1], [2, 3]]
>>> list3 = list2
>>> list4 = list2[:]
>>> list3
[[0, 1], [2, 3]]
>>> list4
[[0, 1], [2, 3]]
>>> list4[0][0] = 2
>>> list3
[[2, 1], [2, 3]]
>>> list4
[[2, 1], [2, 3]]

Notice that modifying an element of the element of the list list4 also modifies list3. This is because although they are different lists, their first elements both refer to the same list.

What is the difference between a[:]=b and a=b[:]

c[:] = a it means replace all the elements of c by elements of a

>>> l = [1,2,3,4,5]
>>> l[::2] = [0, 0, 0] #you can also replace only particular elements using this
>>> l
[0, 2, 0, 4, 0]

>>> k = [1,2,3,4,5]
>>> g = ['a','b','c','d']
>>> g[:2] = k[:2] # only replace first 2 elements
>>> g
[1, 2, 'c', 'd']

>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> c[:] = a #creates a shallow copy
>>> a[0].append('foo') #changing a mutable object inside a changes it in c too
>>> a
[[1, 2, 3, 'foo'], [4, 5, 6], [7, 8, 9]]
>>> c
[[1, 2, 3, 'foo'], [4, 5, 6], [7, 8, 9]]

d = b[:] means create a shallow copy of b and assign it to d , it is similar to d = list(b)

>>> l = [1,2,3,4,5]
>>> m = [1,2,3]
>>> l = m[::-1]
>>> l
[3,2,1]

>>> l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> m = l[:] #creates a shallow copy
>>> l[0].pop(1) # a mutable object inside l is changed, it affects both l and m
2
>>> l
[[1, 3], [4, 5, 6], [7, 8, 9]]
>>> m
[[1, 3], [4, 5, 6], [7, 8, 9]]

Difference between $a=&$b , $a = $b and $a= clone $b in PHP OOP

// $a is a reference of $b, if $a changes, so does $b.    
$a = &$b;

// assign $b to $a, the most basic assign.
$a = $b;

// This is for object clone. Assign a copy of object `$b` to `$a`.
// Without clone, $a and $b has same object id, which means they are pointing to same object.
$a = clone $b;

And check more info with References, Object Cloning.

Difference between a = b and b = a?

Generally, a = b means "a becomes b". However, this is only part of the story when we talk about Objects (as opposed to primitives). After the assignment both variables or fields reference the same object. When object allows changes (which String does not) any changes you perform on a take an effect on b as well, and vice versa.

This can be illustrated with a simple diagram:

Assignment before and after.



Related Topics



Leave a reply



Submit