Home > Enterprise >  If Statement is not processing the variable properly even though the variable is appearing correctly
If Statement is not processing the variable properly even though the variable is appearing correctly

Time:11-25

I have the following piece of code that calls a function to return the number of moves the player shall receive. This function accepts a char variable being T in this case.

    printf("Number Of Moves: %d\n\n", getRoll('T'));

This function then generates a random number between 1 and 10 which then goes through an if statement. My issue is that the if statement isn't working and I am not sure why. I have printed the random variable before the if statement and it is working correctly being a number between 1 and 10.

(I also realize there is currently only one case in my switch statement but there will be others.)

int getRoll(char letter) {

    int num = (rand() % 10)   1;
    char plyr = letter;
    int numMoves;

    switch (plyr) {
        case 'T':
            printf("This is the random number: %d", num);// This is working
            if (1 <= num <= 5) {
                numMoves = 3;
            } else if (6 <= num <= 7) {
                numMoves = -6;
            } else if (8 <= num <= 10) {
                numMoves = 1;
            }
            break;
        }
    }

    return numMoves;
}

CodePudding user response:

This doesn't do what you think it does:

(1 <= num <= 5)

It actually parses as:

((1 <= num) <= 5)

So first 1 <= num is evaluated, resulting in either 0 or 1. Then the result is compared with 5, i.e. 0 <= 5 or 1 <= 5. Both are true so the expression is always true.

You need two separate comparisons here, separated by a logical AND

(1 <= num && num <= 5)

You'll need to make a similar fix for (6 <= num <= 7) and (8 <= num <= 10)

CodePudding user response:

Just a little thing you did there. When you write X< num < y it does not compare the three numbers with eachother, try using and operator there.

 if (1 <= num && num <= 5) {
                numMoves = 3;
            } else if (6 <= num && num <= 7) {
                numMoves = -6;
            } else if (8 <= num && num <= 10) {
                numMoves = 1;
            }

CodePudding user response:

The expression for example in this if statement

if (1 <= num <= 5) {

may be equivalently rewritten like

if ( ( 1 <= num ) <= 5) {

According to the C Standard (6.5.8 Relational operators)

6 Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.

Thus as the value of the variable num is always greater or equal to 1 due to its initialization

int num = (rand() % 10)   1;

then the subexpression ( 1 <= num ) always evaluates to the integer value 1 according to the quote from the C Standard. And in fact you have

if ( 1 <= 5) {

This means that the whole expression of the if statement also always evaluates to logical true.

So the if statement evaluates to true independent of the value stored in teh variable num.

It is evident that in this if statement and other similar if statements you want to use the logical AND operator like

if ( 1 <= num && num <= 5) {

that for readability can be written also like

if ( ( 1 <= num ) && ( num <= 5 ) ) {

Thus you will have

if ( ( 1 <= num ) && ( num <= 5 ) ) {
    numMoves = 3;
} else if ( ( 6 <= num ) && ( num <= 7 ) ) {
    numMoves = -6;
} else if ( ( 8 <= num ) && ( num <= 10 ) ) {
    numMoves = 1;
}

As the num is always greater than or equal to 1 then you may rewrite the if statements also the following simplified way

if ( num <= 5 ) {
    numMoves = 3;
} else if ( num <= 7 ) {
    numMoves = -6;
} else if ( num <= 10 ) {
    numMoves = 1;
}
  • Related