Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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! 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading