Search code examples
ccs50boolean-expression

Confusion with Boolean Expressions in C / < and > are reversed?


I am a bit confused.

< means that the right side of the number is bigger, no?

So natually, the while condition

while (height > 0 && height < 9); 

in

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    // Ask for height:
    int height;
    do
    {
        height = get_int("Height: ");
    }

    // Has to be a number between greater than 0 and smaller than 9

    while (height > 0 && height < 9);

    int height_start = 0;
    while (height_start < height)
    {
        height_start++;
        printf("#\n");
    }

    return 0;
}

should print only hashtags if the input is less than 9, right?

Well strangely enough it's doing the complete opposite and only allowing me to input everything bigger 8.


Solution

  • yes the condition evaluates to true for any height between (0; 9). And when it does, the loop continues for another iteration. It stops if the condition is not true anymore, which is when height is <= 0 or >= 9. Hope that helped, have fun in CS50! :)