Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting multiple integers for "If" statement test value

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.

like image 499
TerranceW Avatar asked May 18 '15 22:05

TerranceW


People also ask

Can you make an if statement with multiple conditions?

The multiple IF conditions in Excel are IF statements contained within another IF statement. They are used to test multiple conditions simultaneously and return distinct values. The additional IF statements can be included in the “value if true” and “value if false” arguments of a standard IF formula.

How do you create an IF THEN statement in Excel with multiple conditions?

Another way to get an Excel IF to test multiple conditions is by using an array formula. To complete an array formula correctly, press the Ctrl + Shift + Enter keys together. In Excel 365 and Excel 2021, this also works as a regular formula due to support for dynamic arrays.

How do you do an if statement for a range of numbers?

Step 1: Put the number you want to test in cell C6 (150). Step 2: Put the criteria in cells C8 and C9 (100 and 999). Step 3: Put the results if true or false in cells C11 and C12 (100 and 0). Step 4: Type the formula =IF(AND(C6>=C8,C6<=C9),C11,C12).

How do I do an IF THEN formula in Excel?

Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false. For example: =IF(A2>B2,"Over Budget","OK") =IF(A2=B2,B4-A4,"")


2 Answers

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...)
}
like image 109
nhgrif Avatar answered Nov 14 '22 09:11

nhgrif


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 &&.

like image 43
Christian Avatar answered Nov 14 '22 09:11

Christian