Setting multiple integers for "If" statement test value - swift

I am trying to set multiple integer tests for a single variable in the if statement. The logical operators won't work due to the fact that they must be boolean.
For example:
if self.nodeAtPoint(location) == self.fake {
groundspeed = 35.0
self.button1value++
if(button1value == 2) {
groundspeed = 5.0
}
if(button1value == 4){
groundspeed = 5.0
}
if(button1value == 6) {
groundspeed = 5.0
}
}
The goal is to be able to put all of the even numbers shown into just one if statement.

If we just want to check whether or not button1value is even, we can do that using the modulo (%) operator:
if button1value % 2 == 0 {
// button1value is even
groundspeed = 5.0
}
If we're checking for some other sort of set, we can use a switch statement:
switch button1value {
case 2,4,6:
// button1value is 2, 4, or 6
groundspeed = 5.0
default:
// button1value is something else
}
We can do other neat tricks with Swift's switch statement too, if we want:
switch (button1value % 2, button1value % 3) {
case (0,0):
// button1value is an even multiple of 3 (6,12,18...)
case (0,_):
// button1value is an even number not a multiple of three (2,4,8,10,14...)
case (_,0):
// button1value is an odd multiple of three (3,9,15,21...)
default:
// button1value is none of the above: (1,5,7,11...)
}

Check and accept nhgrif's answer for a better variant. But just for the sake of completeness if you want to keep your way, you can use the logical OR operator ||
if(button1value == 2 || button1value == 4 || button1value == 6) {
groundspeed = 5.0
}
That checks if any of the given boolean-values is true.
There is also a logical AND operator &&.

You can use contains to check for multiple values. Just pass an array containing the values you want to test and the variable as the second parameter:
if contains([2, 4, 6], button1value) {
groundspeed = 5.0
}

Related

How to activate an if statement with a range of numbers in swift

I am making a game where if a variable is equal to one number in a range of numbers an if statement will take effect. e.g. if var1 = 4, then the if will activate but it will also give an output if var1 was to equal 5, 6, 7 or 8 for example (the numbers in a set range).
I currently have no code for this part of the game so I won't be able to add it to this question.
Sorry if this question is badly explained or too vague to answer, I am new to stack overflow. Any help would be appreciated. Thank you
Probably the best way to do this is using Swift's powerful switch statements.
You can do this:
let x = 5
switch(x){
case 0..<4:
// Will match 0-3
print("one")
case 4..<10:
// will match 4-9
print("two")
case 10...:
// will match >= 10
print("three")
default:
print("other")
}
EDIT:
If that's too robust for your situation, you can also do this:
if (0..<4).contains(x){
print("yes")
}else {
print("no")
}
or even more simply:
if x >= 0 && x < 4{
print("yes")
}else {
print("no")
}

Swift Forin-where perform filter or skip (continue)?

I just gain a knowledge about Swift's forin-where, but it seems very lack of documents. So there's a question in my mind: Does it perform filter then loop or just loop with condition? given the below code
var arr = [1, 2, 3, 4, 5]
for i in arr where i > 3 {
print(i)
}
does the machine do like this:
for i in arr.filter { $0 > 3 }
or like this?
for i in arr {
guard i > 3 else { continue }
print(i)
}
Should I use forin-where or just filter then foreach?
It is the latter (iterate over all elements, execute the body only for
elements satisfying the condition).
The for-statements takes an arbitrary sequence, not only arrays.
Filtering the sequence first would not only be inefficient (memory- and
time-wise), but also impossible for sequences producing “infinitely many” values, like in this example:
for x in 1... where x % 3 == 0 {
print(x)
if x > 10 { break }
}
Here 1... is a “partial range” representing all integers greater than
or equal to one.

How to check if list contains all the same values?

I have a List[<DataType>] as input. I want to check if the list contains all the same values(not datatype).
Is there inbuilt method or intuitive way in Scala to do this instead of iterating over list and checking.
This will terminate on the first non-equals element. The element type has to support a comparator like == or !=.
lst.forall(_ == lst.head) // true if empty or all the same
lst.exists(_ != lst.head) // false if empty or all the same
One (inefficient but elegant) way is
List(1, 2, 2, 1, 1).distinct.length == 1 // returns false
List(1, 1, 1, 1, 1).distinct.length == 1 // returns true
List().distinct.length == 1 // empty list returns false
Note that they must be of the same type
List(4, 4.0, "4").distinct.length == 1 // returns false
I just had to do this for an unrelated problem, so to improve on the above ever so slightly: lst.tail.forall(_ == lst.head). This avoids checking that the head of the list equals itself, which you already know is true.

C-Style i++ for is deprecated in swift issue

I want to increment index at some point for this loop it prints 1,3,5 which is i want to. I get the warning
C-Style for statement is deprecated and ...
i know what it means.
for var index=0; index<5; index++ {
//If condition A == true
index++
//else without index++
print(index) // print 1, 3, 5
}
So i changed it to:
for var index in 0..<5 {
//If condition A == true
index += 1
//else without index++
print(index) // print 1,2,3,4,5 Should 1,3,5 from my side
}
I just wondering why index not mutable? Even though i have set it to var or any solutions for my issue.
The index is not mutable because
for var x in y {
...
}
is equivalent to
for temp in y {
var x = temp
...
}
where the var just makes x a copy of temp. When you modify x, it won't modify the real index temp (This is also a reason why SE-0003 is introduced)
The C-style for loop can just be reduced to a while loop:
var index = 0
while index < 5 {
if conditionA {
index += 1
}
print(index)
index += 1
}
If you just need to enumerate odd numbers, the simplest way will be to use stride:
for i in 1.stride(through: 5, by: 2) {
print(i) // prints 1, 3, 5
}
Here is one way to get the results you want:
for var index in 0..<3 {
print(2 * index + 1)
}
I believe swift loop variables are immutable when using the for in loop. Mutating a loop variable inside the loop is usually (one could argue always) a bad idea so it makes sense the swift designers didn't allow it. The desired result can be accomplished in cleaner ways, for instance using continue.
for i in 1...5
{
if i%2==0
{
continue
}
print(i)
}

Determine whether number is a multiple of 5

How would I go about determining if a number is a multiple of 5?
In my app, I want one method to run if a certain number is not a multiple of 5, and another method to run if the method is a multiple of 5.
Thanks for your help!
Swift 5 UPDATE
According to newly released language version you can determine this using isMultiple(of:) method
let num = 75
if num.isMultiple(of: 5) {
// multiple of 5
} else {
// not a multiple of 5
}
Use the modulus operator to check the remainder of integer division.
if (num % 5 == 0) {
// multiple of 5.
}
else {
// not a multiple of 5.
}
Use the modulus operator:
if (num % 5 == 0)
//the number is a multiple of 5.
else
// the number is not a multiple of 5.
The modulus operator returns the remainder of a division instead of the division itself, so this logic will work with any number, not just 5. i.e. if (num % 3 == 0) //multiple of 3
Check by this simple logic.
Find the remainder, if it is 0, that means it is completely divisible by 5.
if(number % 5 == 0) {
NSLog(#"Multiple of 5");
//[self multipleOfFive];//your method
}
else{
NSLog(#"Not a multiple of 5");
//[self notMultipleOfFive];//your method
}
Note: You can only check % (modulus) for integers
for floats or doubles use :
double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);