Expandir minha barra de realizações na Comunidade.

SOLUCIONADO

An "or" Statement?

Avatar

Level 3

I am trying to create an or statement. I have tried for hours now with no success. Can someone (anyone) please tell me what I am doing wrong so I can move on in this project... This is what I have

I am trying to make a action take place only if the value of a radioButton is 3,4,5. And something else entirely if the value is 1,2. I am still kinda new to this, so don't be too hard on me. But here are a couple examples of what I have been trying to do. What am I doing wrong?

if(radioButton.rawValue == 1,2)

{

exampleSub1.presence = "visible";

exampleSub2.presence = "hidden";

}

else if(radioButton.rawValue == 3,4,5 & checkBox1.rawValue == 0)

{

another event happens

}

I have also tried using || in place of comma's and wrapping the numbers in (), and just for good measure I also tried wrapping them in [ ]

Thanks for any help

1 Solução aceita

Avatar

Resposta correta de
Level 6

You can use a switch statement with intentional fall-through, e.g.:

switch (radioButton.rawValue) {
case 1:
case 2:
    exampleSub1.presence = "visible";
    exampleSub2.presence = "hidden";
    break;
case 3:
case 4:
case 5:
    if (checkBox1.rawValue == 0) {
        // do something
    } else {
        // do something else?
    }
    break;
default:
    // May not be needed, but just in case...
    // do something if not 1-5
    break;
}

I find this to be a bit cleaner than something like:

var v = radioButton.rawValue;

if (v == 1 || v == 2) {
    exampleSub1.presence = "visible";
    exampleSub2.presence = "hidden";
}

if ((v == 3 || v == 4 || v == 5) && checkBox1.rawValue == 0) {
    //another event happens
}

Edited to correct typo

Ver solução na publicação original

2 Respostas

Avatar

Resposta correta de
Level 6

You can use a switch statement with intentional fall-through, e.g.:

switch (radioButton.rawValue) {
case 1:
case 2:
    exampleSub1.presence = "visible";
    exampleSub2.presence = "hidden";
    break;
case 3:
case 4:
case 5:
    if (checkBox1.rawValue == 0) {
        // do something
    } else {
        // do something else?
    }
    break;
default:
    // May not be needed, but just in case...
    // do something if not 1-5
    break;
}

I find this to be a bit cleaner than something like:

var v = radioButton.rawValue;

if (v == 1 || v == 2) {
    exampleSub1.presence = "visible";
    exampleSub2.presence = "hidden";
}

if ((v == 3 || v == 4 || v == 5) && checkBox1.rawValue == 0) {
    //another event happens
}

Edited to correct typo

Avatar

Level 3

Works like a charm! Thanks so much