If will an IF statement return when we compare a NSString and a BOOL type variable - objective-c

In Objective-C, is
if(abc && def) allowed???
here abc is of BOOL type and def is of NSString type.
This condition is present in the code snippet I am going through. When will it return YES and when will it return FALSE?

The if statement will be true only if abc is unequal to NO and def is not nil.
The expression is equivalent to:
if (abc != 0 && def != 0) {
}
abc is not equal to 0 when it is unequal to NO. def is not equal to 0 when it is not nil.

if (abc && def) {
}
this condition will return YES if abc is not ZERO and def is also not nil (means it contains some not nil value). in either case it will return NO.
i hope you will understand this.

Related

IDEA doesn't offer to replace a null check + dot action to `?.`

I have the following code example below.
Replacing the following null check + dot action
a != null && a.toInt() == b
with ?.
a?.toInt() == b
"seems" to do the same job and even clearer.
But, as you can see in the image, IDEA doesn't suggest the replacement.
Perhaps the two expressions aren't equivalent?
Example code:
fun main() {
val a: String? = initA()
val b = 1
if (a != null && a.toInt() == b) {
println("true")
} else {
println("false")
}
}
Screencap:
The second I clicked post I understood.
If b's type is changes to nullable, meaning:
val b: Int?
Those expressions cease to be equivalent.
If both a and b are null, the behavior will be different.

Multiple #if statements in Apache Velocity

I want to write the following if - else logic in Velocity
If $var1 == NONE
( If $subvar1 != 'null'
return True
else
return Failed_Sub1)
Else
If $subvar2 != 'null'
return True
else
return Failed_Sub2
So basically $subvar2 is only evaluated if $var != NONE and $subvar1 is only evaluated if $var == NONE
I tried something like
#if($var1 != 'NONE')
#if($subvar2 != 'null')True
#{else}Failed_Sub2
#end
#else
#if($subvar1 != 'null')True
#{else}Failed_Sub1
#end
#end
But its returning nothing to me. What am I doing wrong?
Do you want to avoid null values or strings containing 'null'?
In velocity, you can check for nulls using any non assigned reference, for instance $null :
#if($var1 == $null)
...
Otherwise than that, your code looks fine and nested #if statements are definitely possible.
Here's the relevant documentation.

Operator '==' cant be applied to 'Boolean' and 'Char'

So i want to compare three members of an array with as little code as possible. Heres what i did:
for(i in 0..2) {
if(board[i][0] == board[i][1] == board[i][2]) {
return true
} else if(board[0][i] == board[1][i] == board[2][i]) {
return true
}
}
(All of the values ar Char's FYI) But it didnt work. I get this error message "Operator '==' cant be applied to 'Boolean' and 'Char'". I also tried using .equals, but that just didnt work. Any ideas on what to do?
You can write a small function to keep it more readable and tidy, especially if You need to do that comparison often:
fun allEqual(vararg items: Any) : Boolean {
for(i in 1..(items.size-1)){
if(items[0] != items[i]) return false
}
return true
}
And invoke simply by comma separating values:
allEqual(board[i][0], board[i][1], board[i][2])
I don't know Kotlin specifically, but most* languages don't allow you to compare 3 values at the same time. What your error message is communicating is that your code ends up comparing
"Is board[i][0] equal to board[i][1]?" which is true/false (Boolean)
to
board[i][2], which is a Char.
*I don't know of any, but maybe there's one out there that does.
You have included this condition:
if(board[i][0] == board[i][1] == board[i][2])
Firstly, this one is compared: board[i][1] == board[i][2]
After comparing, it returns true. After that if logic converts to:
if(board[i][0] == true)
Now, board[i][0] is a char and you are trying to compare it to a boolean which is not possible. That's why you are getting this error.
You have to change the logic to:
if((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]))
So, your code will be:
for(i in 0..2) {
if((board[i][0] == board[i][1]) && (board[i][1] == board[i][2])) {
return true
} else if((board[0][i] == board[1][i]) && (board[1][i] == board[2][i])) {
return true
}
}
Another approach:
for (i in 0..2) {
if (board[i].toSet().size == 1)
return true
else if (board.map { it[i] }.toSet().size == 1)
return true
}
As the others said, your first comparison returns Boolean, and the second compares Boolean to Char.
You can use an extension function, and transitivity to simplify things:
fun Any.equalsAll(vararg others: Any):Boolean
{
others.forEach {
if(it!=this)
return false
}
return true
}
and call:
if (board[0][i].equalsAll(board[1][i], board[2][i]))

Why does the Objective-C Compiler return a "y" as a "0" but then skips over the "if input==0" section?

int side1test;
NSLog(#"Is your triangle setup as in an Angle-Side-Angle? (Use 1 for Yes and 0 for No.)");
scanf(" %i", &side1test);
Returns "0" when the user enters a "y." However,
if (side1test != 1 && side1test != 0){
NSLog(#"Please use a '1' for YES and '0' for NO.");
}
Then does not catch.
The program drops into my else clause, and outputs all the NSLogs, skipping the scanf() commands, taking each of them as "0." What is wrong here?
I'm not a c++ dev but from googling that function returns the number of valid matches. If it returns 0 you should assume invalid input. side1test has not been set which is why it's 0.
Your code should probably be:--
int side1test;
NSLog(#"Is your triangle setup as in an Angle-Side-Angle? (Use 1 for Yes and 0 for No.)");
int result = 0;
while (result==0)
{
result =scanf(" %i", &side1test);
}
if (side1test != 1 && side1test != 0){
NSLog(#"Please use a '1' for YES and '0' for NO.");
}

Check if a variable has any value from a given set

How can I check if a condition passes multiple values?
Example:
if(number == 1,2,3)
I know that commas don't work.
if (number == 1 || number == 2 || number == 3)
If you are using PHP, then suppose your list of numbers is an array
$list = array(1,3,5,7,9);
then for any element, you can use
if(in_array($element, $list)){
//Element present in list
}else{
//not present.
}
Function structure:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Hope that helps.
if ((number >= 1) && (number <= 3))
What language?
For example in VB.NET you use the word OR, and in C# you use ||
Since you specify no language I add a Python solution:
if number in [1, 2, 3]:
pass
In T-SQL you can use the IN operator:
select * from MyTable where ID in (1,2,3)
If you are using a collection there may be a contains operator for another way to do this.
In C# for another way that may be easier to add values:
List<int> numbers = new List<int>(){1,2,3};
if (numbers.Contains(number))
I'll assume a C-Style language, here's a quick primer on IF AND OR logic:
if(variable == value){
//does something if variable is equal to value
}
if(!variable == value){
//does something if variable is NOT equal to value
}
if(variable1 == value1 && variable2 == value2){
//does something if variable1 is equal to value1 AND variable2 is equal to value2
}
if(variable1 == value1 || variable2 = value2){
//does something if variable1 is equal to value1 OR variable2 is equal to value2
}
if((variable1 == value1 && variable2 = value2) || variable3 == value3){
//does something if:
// variable1 is equal to value1 AND variable2 is equal to value2
// OR variable3 equals value3 (regardless of variable1 and variable2 values)
}
if(!(variable1 == value1 && variable2 = value2) || variable3 == value3){
//does something if:
// variable1 is NOT equal to value1 AND variable2 is NOT equal to value2
// OR variable3 equals value3 (regardless of variable1 and variable2 values)
}
So you can see how you can chain these checks together to create some pretty complex logic.
For a list of integers:
static bool Found(List<int> arr, int val)
{
int result = default(int);
if (result == val)
result++;
result = arr.FindIndex(delegate(int myVal)
{
return (myVal == val);
});
return (result > -1);
}
In Java you have objects that wrap primitive variables (Integer for int, Long for long, etc). if you look to compare values between a lot of complete numbers (ints), what you can do is initiate a bunch of Integer objects, stuff them inside an iterable such as an ArrayList, iterate over them and compare.
something like:
ArrayList<Integer> integers = new ArrayList<>();
integers.add(13);
integers.add(14);
integers.add(15);
integers.add(16);
int compareTo = 17;
boolean flag = false;
for (Integer in: integers) {
if (compareTo==in) {
// do stuff
}
}
of course for a few values this may be a bit unwieldy, but if you want to compare against a lot of values, it'll work nicely.
Another option is to use java Sets, you can place a lot of different values (the collection will sort your input, which is a plus) and then invoke the .contains(Object) method to locate equality.