Objective-C: Exclamation Point strcmp in "if" statement - objective-c

I am looking over some OpenGL ES code to multiplay matrices, but I'm not sure about how this if statement works:
for (int i = 0; i <_uniformArraySize; i++) {
**if (!strcmp(_uniformArray[i].Name, "ModelViewProjectionMatrix")) {**
GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply(_projectionMatrix, _modelViewMatrix);
glUniformMatrix4fv(_uniformArray[i].Location, 1, GL_FALSE, modelViewProjectionMatrix.m);
}
}
Does !strcmp mean that the strings are equal or not equal? I looked at the strcmp documentation and it returns numbers. So how does this exclamation point in an if statement affect a number (being the return value of strcmp)?
Thanks

Since Objective C, like C, allows integers in conditionals, using !expr is a common shorthand for expr== 0.
Your statement is equivalent to
if (strcmp(_uniformArray[i].Name, "ModelViewProjectionMatrix") == 0) {
...
}
Since strcmp returns zero when strings are equal to each other, the condition checks if the content of two C strings is the same.

strcmp() function returns the total number of differences found between each string, so a zero means there are no differences. It is kind of un-intuitive when you think of it in terms of strcmp true/false.
The exclamation mark is the 'NOT' operator. This means that it will test whether the value in front of it is NOT true, so the result is essentially a reversal of the original boolean value.
In this case, if !strcmp() means if the result of strcmp is NOT > 0 then the result is true.

strcmp will return zero when strings are equal. The exclamation point is the negation operator so the program will enter the if statement when strings are equal.

Related

What (if anything) does ++ do in VB.NET?

I'm fairly new to Visual Basic (my background is mostly C#) so I only recently found out that ++i is not the same thing as i+=1 in VB.NET. However, my VB.NET code still compiles when I pass ++i as a parameter, even though it doesn't increment the variable:
++i 'doesn't compile
i++ 'doesn't compile
Foobar(i++) 'doesn't compile
Foobar(++i) 'compiles, but doesn't increment i
The fact that the last statement above doesn't cause an error leads me to wonder if ++ actually does mean something in VB.NET, just not what I thought it meant at first. If it doesn't, is there some other reason it isn't causing an error?
It is just the unary version of the + operator (see docs) applied twice.
Foobar(++i)
Foobar(+(+i))
Foobar(+(+(i)))
' These are all the same
For numerical values, the unary + (i.e. the + operator without second operand) does nothing:
If expression2 is absent, the + operator is the unary identity operator for the unchanged value of an expression.
However, it is not entirely clear from the docs what it will do to non-numeric values. The docs explain various cases with two operands, which all don't seem to apply here.
There is even one sentence that sounds like it could be applied, but it does not do what it says if used with unary +:
If either Object expression evaluates to Nothing or DBNull, the + operator treats it as a String with a value of "".
So you would expect that +Nothing gives "" as well, but it gives 0 instead. In fact, it appears that the unary + converts non-numerical types to Double, including strings for which + would otherwise mean concatenation (for example +"1.234" gives 1.234, and +"Hello" gives an error that this string cannot be converted to Double - and with Option Strict On, you can't convert any string implicitly anyway). It seems to behave more like a binary + with 0.0 as first operand.
You can also overload the unary + separately from the binary + and give it a different meaning entirely*. (Or do the opposite - make it do nothing even on a non-numeric type, such as what TimeSpan does - it returns the original timespan again when unary + is applied on it, and not a Double.)
*: Which probably is not such a good idea though. When overloading an operator, the meaning of it should always be intuitive.
There is no ++ operator in VB. The + unary operator is just like the - unary operator applied to a number. Numbers are positive by default but, just as you can explicitly make a number negative by prefixing it with a - operator, you can make it explicitly positive by prefixing it with a + operator. You can use as many + operators as you like. Similarly, you can use as many - operators as you like. The difference is that + operators don't change the value where - operators do.
In C-based languages, assignments actually return a value where they don't in VB. In C#, you can do this:
i += 1
and it will get the value of i, add 1 to it, assign the result back to i and then return that result, so you can use that expression where a value is expected. In VB, it does all the same things up to the assignment but it does not return a value, so you cannot use that expression where a value is expected.
In C-based languages, where you place the ++ operator makes a difference. The expression:
++i
increments i and returns the final value, whereas this expression:
i++
increments i and returns the original value. That's why some argue that the C++ language should actually be named ++C.

How does the Imp Logical Operator Work in VBA?

I came across some code in VBA & I'm trying to understand how it works, but I've never encountered this operator.
What exactly does Imp operator do in VBA?
If (a <> 0 Imp b = 0) Then
MsgBox ("IMP Operator Result is : True")
Else
MsgBox ("IMP Operator Result is : False")
End If
Are there examples of when we would use it?
Obviously, this is not a bitwise comparison, but something else.
The documentation from the comment actually is not that good. This is what it says:
The Imp operator performs a bitwise comparison of identically positioned bits in two numeric expressions and sets the corresponding bit in result according to the following table.
If you take a look at the example:
You would see that the result is always True(or 1) in any possible cases but one. The single case, where the result is False (or 0) is when the first operand is True and the second operand is False.

VBA assignment statement with an OR clause

I've come across some code that looks something like this
If (condition) Then
array(index) = array(index) Or variable
End If
Being unfamiliar with VB, it looks to me like some kind of horrid result of a love affair between an assignment statement and a condition clause.
Functionally, I thought it looked like some kind of ?: ternary operation. I now assume it is, though certainly not in a form I'm used to.
I'm aware that the first branch will assign array(index) to itself, that's essentially how the code I'm working on works. I don't THINK it's relevant to the question, but it kinda weirds me out, and there is often more going on than what I realize with VB.
Not a duplicate of Is there a conditional ternary operator in VB.NET? since that question is asking if there IS one, rather than "what the heck does this mean"
What it's not.
There is no ternary operator in VBA. The closest you get is IIf, which evaluates both true and false expressions (so don't use it when either branch has side-effects!):
IIf({bool-expression}, {value-if-true}, {value-if-false})
So it's not anything like a ternary.
We don't know what's in array(index), but let's assume it's some Long:
array(index) = SomeLongInteger Or variable
What you're looking at is a regular assignment:
foo = 42 Or variable
The runtime is going to assign foo with a value. So first it must compute the right-hand side of the assignment (=) operator:
42 Or Variable
The Or operator is often used as a logical operator in Boolean expressions.
However the above expression isn't a Boolean expression, and the Or operator isn't a logical operator here.
What it is.
It's a bitwise operator (in VB the logical and bitwise operators are the same, which is indeed a bit (pun not intended) confusing). In the assignment foo = 42 Or Variable, foo will take the value of the bitwise comparison of 42 Or Variable.
Debug.Print 42 Or 12 'prints 46
How?
Think in binary. This is 42:
00101010
This is 12:
00001100
Then you bitwise-or the two values (remember your truth tables? True Or True = True; True Or False = True; False Or False = False) and you get this:
00101110
..which is the binary representation for - that's right - 46.
So, array(index) = array(index) Or variable does the following:
Read the value of array(index)
Read the value of variable
Bitwise-Or the two values
Assign the result of that operation back to array(index)

Multi-semicolon Line Endings: Acceptable?

So, while I was working on some code for my next app update, I noticed something strange: You can put a very large amount of semicolons at the end of Objective-C statements and it will compile just fine! Heck, it runs the same as well. Why in the world does this work?
Semicolons are just used to end the current statement. Empty statements are permitted in C-like languages, for example:
int len = 0;
while(str[len++]); // count the length of a null-terminated string
if (1) {} else { printf("Uh oh... this can't be happening!\n"); }
They don't do anything.
Placing an arbitrarily large number of semicolons at the end of the line is just an extreme case of this.
Each extra semicolon represents a statement which does nothing.
Also, simple expressions like 0; are legal code which do nothing.

problem with an if statement

For some reason the argument I pass to my if statement is not true for even though it should be heres my code:
if (currentAttribute == cBusName)
{
NSLog(#"currentAttribute == cBusName");
}
current attribute and cBusName are both NSMutableStrings an both equal "1" but the NSLog never outputs the string in the console is there something I am missing???
The == operator is comparing that those objects are the same object (IE they point to the same address in memory), not that their values are the same.
Try
if ([currentAttribute isEqualToString: cBusName])
{
NSLog(#"currentAttribute == cBusName");
}
which compares the values of the two strings, not their location in memory.
String objects cannot be compared
using the equality (==) operator. The
reason for this is that any attempt to
perform a comparison this way will
simply compare whether the two string
objects are located at the same memory
location. Let's take a look at this
via an example:via an example:
from http://www.techotopia.com/index.php/Working_with_String_Objects_in_Objective-C, search for Comparing Strings