...

The AND Operator (&&)

How would you write a program that tells if someone is a teenager? We would need two conditions to be true:

  • age > 12 can tell if they're 13 or over.
  • age < 20 can tell if they're 19 or under.

How can we write an if statement to check both conditions? A person is a teenager if age > 12 AND age < 20.

The AND Operator (&&)

This is how we can check if age > 12 AND age < 20 using the && operator:

if (age > 12 && age < 20) { ...

The && operator evaluates to true only when both conditions are true. Run the code sample in this exercise to see the && operator in action.

Loading...

Output (Console)