Implementation of AND with the if statement

Hello,
I want to implement (h >= 1) && (h <= 99) with an if statement. Is it possible?

Thank you.

Yes, of course. I guess you don’t want me to spoil it for you?

I suggest you review the relevant section in the manual and when you encounter problem reaching your goal then you come back here and try to be as specific as possible about what you tried and what didn’t work :slight_smile:

Hi,
Thank you so much.

print("Please enter a number: ")
a=parse(Int,readline())
if (a>=1)
    if (a<=99)
        print("OK")
    end
end

Does this make sense in terms of programming?

Yes.

Perhaps a bit nicer to read is to combine the conditions:

print("Please enter a number: ")
a=parse(Int,readline())
if (a>=1) && (a<=99)
    print("OK")
end

Also note that you don’t need the parenthesis for the condition of the if. For more complex conditions (like in my code), the parenthesis can help increase legibility but for very simple conditions (your code) one typically would omit them:

print("Please enter a number: ")
a=parse(Int,readline())
if a >= 1
    if  a <= 99
        print("OK")
    end
end

Finally, for this specific example there are alternative ways of writing the condition (not better by any means I am just showing you some more of the language):

variant1 = a in 1:99
variant2 = 1 <= a <= 99
1 Like

Hi,
Thank you so much. :heart: