Small Steps I — Creating with Text and Numbers
Combining two conditions
Print Outside when the age is below 13 or above 19. Test with 20.
What you will learn
&& means both conditions are true, || means at least one is true, and ! reverses true and false.
Try it
Example · example.cpp
void small_main()
{
int age = 15;
if (age >= 13 && age <= 19)
{
print("Teen");
}
}
Check whether the age is at least 13 and at most 19. Try 12, 13, 19, and 20 to check the boundaries.
!ready asks whether you are not ready. && skips the right side if the left is false; || skips it if the left is true.
Expected output:
Teen
Exercise
Print Outside when the age is below 13 or above 19. Test with 20.
Exercise starter · exercise_starter.cpp
void small_main()
{
int age = 15;
if (age >= 13 && age <= 19)
{
print("Teen");
}
}
Hint
Use || between the two conditions.
show solution
void small_main()
{
int age = 20;
if (age < 13 || age > 19)
{
print("Outside");
}
}