Home > OS >  How to combine operators in an IF [closed]
How to combine operators in an IF [closed]

Time:09-17

I'm having doubts in coding a loop repeat where conv0 == 255 and conv1 == 216 and conv3 can have 16 different types of combinations. I'm having difficulties in this section of the code.

if( conv0 == 255 && conv1 == 216 && 
    (
      conv2 == 224 || conv2 == 225 ||
      conv2 == 226 || conv2 == 227 ||
      conv2 == 228 || conv2 == 229 ||
      conv2 == 230 || conv2 == 231 ||
      conv2 == 232 || conv2 == 233 ||
      conv2 == 234 || conv2 == 235 ||
      conv2 == 236 || conv2 == 237 ||
      conv2 == 238 || conv2 == 239 
    )
  )
{
    printf("OK \n ");
}

Thank you.

CodePudding user response:

It seems like conv2 must be between 224 and 239, so you can write:

if (conv0 == 255 && conv1 == 216 && conv2 >= 224 && conv2 <= 239)

CodePudding user response:

Go slow. Don't try to do too much in a single statement...

if (conv0 == 255) {
    if (conv1 == 216) {
        if ((224 <= conv2) && (conv2 <= 239)) {
            printf("all ok\n");
        } else {
            printf("conv2 failed.\n");
        }
    } else {
        printf("conv1 failed.\n");
    }
} else {
    printf("conv0 failed.\n");
}
  • Related