How do I make AND or OR expressions? - objective-c

I wrote this:
if( a == -11 && b == -1 ){
{
if( a == -1) AND ( b == -1)...
But neither work, and I have the same problem with OR. How do I write expressions that include OR or AND?

You use && for “and”, and || for “or”.

(a == -11 && b == -1) is fine and correct. Objective-C uses all of the same logical operators as C. || is the logical-or operator.

if (a==-11 && b==-1) {
if perfectly legal in C, and therefore Objective-C.
Your second example is not C or Objective-C

Related

how do i correctly use >= and <= in code?

I have tried many thing involving this, >=, >==, =>, ==>.i can not find one that works. hey all return either primary expression needed or expected initializer before '>'. I am creating a IR receiver latch switch and thus have to create parameters for the code because the receiver is not constant in all conditions. Full code below. Any suggestions to fix the code please reply and don't DM me. Thank you.
code:
int LEDState = 0;
int LEDPin = 8;
int dt = 100;
int recieverOld ==> 500 and recieverOld ==< 2000;
int recieverNew;
int recieverPin = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LEDPin, OUTPUT);
pinMode(recieverPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
recieverNew = digitalRead(recieverPin);
if((recieverOld >== 0 && recieverOld <== 10) && (recieverNew >== 500 && recieverNew <== 2000) {
if(LEDState == 0) {
digitalWrite(LEDPin, HIGH);
LEDState = 1;
}
}
recieverOld = recieverNew;
delay(dt);
}
error:
expected initializer before '==' token
if one = used line 4 and related, return error expected primary-expression before '>' token
if > before = line 4 and related, return error expected initializer before '>=' token
Any solutions or suggestions welcome.
TL;DR
Operators that do no exist, and that you should NOT use:
==>, ==<, >==, <==
Operators that works and you can use them:
>= - MORE THAN OR EQUAL, compare operator, for example X >= 5
<= - LESS THAN OR EQUAL, compare operator, for example X <= 5
> - MORE THAN, compare operator, for example X > 5
< - LESS THAN, compare operator, for example X < 5
== - compare operator, when you want to compare values of the variables if they have the same value, for example X == 5, Y == X, 10 == 7
=== - equality operator, similar to compare operator ==, but aditionally checks the type of a variable. for example X === Y, '10' === 10
= - assign operator, when you want to assign something to the variable, for example X = 5
<> OR != - NOT EQUAL, compare operator, for example X != 5, Y <> 10
!== - similar to != or <>, but also checks the type of a value. For example 10 !== '10', and will return opposite result of the equality operator ===

How can I get the value of the existing variable in a IF clause?

I am working on Objective C and I want to do something like this:
if (a && !b) {
// a do something...
} else if (!a && b) {
// b do something...
}
I wondered if there is something simpler, like:
if (a XOR b) {
// the existing variable do something...
}
Thanks in advance!!
Objective C is a superset of C, use the ^ operator. Or you can think logically (since xor is only true if either is true and the other is false) and use:
// This won't work for all types, be careful
if (a != b){
if (a){
// a do something
}
if (b){
// b do something
}
}
Note this solution, expanded from the xor, is more lengthy than the one you provided.
well I'm not sure if I misunderstood, but I guess a possibly solution would be to use the ? operator.
void *aux;
if( aux = a ? (b ? NULL : a) : (b ? b : NULL) )
//working with aux here
Never the less, if the idea is to keep it simple, this is quite unreadable. Also, this would expand to something like:
void *aux;
if(a){
if(b)
aux = NULL;
else
aux = a;
}else{
if(b)
aux = b;
else
aux = NULL;
}
My suggestion is that you leave the code as is. It's more readable and in terms of performance, I don't believe you'll notice much difference between the approaches
edit for clarity:
BTW, Inside the if block, the aux var will contain the value that exists. And if aux is NULL the if block won't be entered. Also, aux doesn't have to be void * or a pointer, it only has to be compatible with a and b datatypes.
if( !a != !b ) // same as a xor b

Is it possible with Antlr to detect in which rule of my grammar/part of my grammar an error happend?

I build an application where an user can create boolean rules and customizable functions based on boolean rules:
Here an Example:
((A == True) || (B == True)) && ( C >= 3
&& D == 60) && count{[E == True, F == False, G ==
True,] > 2}
the first part of this rule are common boolean comparisons. the second part starting with "count" is a function, where at least 2 statements have to true for the second part to be true.
Is it possible with Antlr in which kind of my rule an error happend?
Yes, look at the parse tree with grun (TestRig) or intellij plugin etc...

if statement gone wrong -xcode

Guys what am I doing wrong?
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
}
else if (numberstring.intValue >16 && <=40){
rankLabel.text = #"A2";
}
I get an error on the "<=40" ..
You missed off a variable reference:
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
} // vv here vv
else if (numberstring.intValue >16 && numberstring.intValue <= 40){
rankLabel.text = #"A2";
}
As an optional extra, it looks like numberstring is an NSString object, which you are repeatedly converting to an integer in order to test various ranges. That operation is quite expensive, so you are better off doing the conversion once:
int value = [numberstring intValue];
if (value <=15) {
rankLabel.text = #"A1";
}
else if (value >16 && value <= 40){
rankLabel.text = #"A2";
}
Also note that the intValue method is not a property so I would avoid using the Objective-C 2.0 dot syntax to access it and use the normal method calling mechanism.
The && operator links two clauses together. However, each clause is independent, so each one has to be syntactically correct on its own if the other was removed. If you apply this rule to your condition, you can see that "<=40" is not syntactically correct on its own. Thus you need to reference the value being compared, as follows:
if (numberstring.intValue > 16 &&
numberstring.intValue <= 40) // this is syntactically correct on its own

How do I express the "OR" logic operator in Objective-C?

Let's say that I want to check that one of two terms are effective, how do I express it?
if (value == 1 **--OR--** value == nil) {
do something;
}
||
eg: if (value == 1 || value == nil)