R: += (Plus Equals) and ++ (Plus Plus) Equivalent from C++/C#/Java, etc.

R: += (plus equals) and ++ (plus plus) equivalent from c++/c#/java, etc.?

No, it doesn't, see: R Language Definition: Operators

Is there an implementation of += and -= operators for R?

It does not exist in R default operators however you can manage to do something similar to it using roperators package as follows;

install.packages('roperators')
require(roperators)

# Assignment
a <- 1
print(a)
# [1] 1

# To incremenet value
a %+=% 2
print(a)
# [1] 3

# To decrement value
a %-=% 2
print(a)
# [1] 1

Two Equal Signs in One Line?

The value of the expression (a = b) is the value of b, so you can chain them this way. They are also right-associative, so it all works out.

Essentially

ArcChar = ArcBit = 0;

is (approximately1) the same as

ArcBit = 0;
ArcChar = 0;

since the value of the first assigment is the assigned value, thus 0.

Regarding the types, even though ArcBit is an unsigned char the result of the assignment will get widened to int.


1   It's not exactly the same, though, as R.. points out in a comment below.

What is the difference between ++i and i++?


  • ++i will increment the value of i, and then return the incremented value.

     i = 1;
    j = ++i;
    (i is 2, j is 2)
  • i++ will increment the value of i, but return the original value that i held before being incremented.

     i = 1;
    j = i++;
    (i is 2, j is 1)

For a for loop, either works. ++i seems more common, perhaps because that is what is used in K&R.

In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.

There's a couple of comments regarding the efficiency of ++i and i++. In any non-student-project compiler, there will be no performance difference. You can verify this by looking at the generated code, which will be identical.

The efficiency question is interesting... here's my attempt at an answer:
Is there a performance difference between i++ and ++i in C?

As @OnFreund notes, it's different for a C++ object, since operator++() is a function and the compiler can't know to optimize away the creation of a temporary object to hold the intermediate value.

String.equals versus ==

Use the string.equals(Object other) function to compare strings, not the == operator.

The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==, but it's better not to rely on that.

if (usuario.equals(datos[0])) {
...
}

NB: the compare is done on 'usuario' because that's guaranteed non-null in your code, although you should still check that you've actually got some tokens in the datos array otherwise you'll get an array-out-of-bounds exception.

What is the difference between the | and || or operators?

Just like the & and && operator, the double Operator is a "short-circuit" operator.

For example:

if(condition1 || condition2 || condition3)

If condition1 is true, condition 2 and 3 will NOT be checked.

if(condition1 | condition2 | condition3)

This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good performance boost by using them.

There is one big caveat, NullReferences or similar problems. For example:

if(class != null && class.someVar < 20)

If class is null, the if-statement will stop after class != null is false. If you only use &, it will try to check class.someVar and you get a nice NullReferenceException. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad, but it's something to keep in mind.

No one ever uses the single & or | operators though, unless you have a design where each condition is a function that HAS to be executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The & operator does "run these 3 functions, and if one of them returns false, execute the else block", while the | does "only run the else block if none return false" - can be useful, but as said, often it's a design smell.

There is a Second use of the | and & operator though: Bitwise Operations.

How to round up the result of integer division?

Found an elegant solution:

int pageCount = (records + recordsPerPage - 1) / recordsPerPage;

Source: Number Conversion, Roland Backhouse, 2001

Difference between pre-increment and post-increment in a loop?

a++ is known as postfix.

add 1 to a, returns the old value.

++a is known as prefix.

add 1 to a, returns the new value.

C#:

string[] items = {"a","b","c","d"};
int i = 0;
foreach (string item in items)
{
Console.WriteLine(++i);
}
Console.WriteLine("");

i = 0;
foreach (string item in items)
{
Console.WriteLine(i++);
}

Output:

1
2
3
4

0
1
2
3

foreach and while loops depend on which increment type you use. With for loops like below it makes no difference as you're not using the return value of i:

for (int i = 0; i < 5; i++) { Console.Write(i);}
Console.WriteLine("");
for (int i = 0; i < 5; ++i) { Console.Write(i); }

0 1 2 3 4

0 1 2 3 4

If the value as evaluated is used then the type of increment becomes significant:

int n = 0;
for (int i = 0; n < 5; n = i++) { }


Related Topics



Leave a reply



Submit