Using and in Boolean if Statements
Let’s first see an example using the
if statement. Here is a simple segment of code that describes a person based on his or her age, following an input statement, which will allow us to test it.
To be classified as an adult in this example means to be older than eighteen and up to and including sixty-five. So you need to combine two inequalities,
age is greater than
age is less than or equal to
65, with the operation
and to indicate that an age must meet both conditions to be considered an adult.
15 is greater than
15 is also less than or equal to
18. Since both these conditions are true, the
and of them is
True, and the program executes the then code for this conditional, telling us you’re an adolescent.
Become a Member to join the conversation.