Nested Conditional Statements
Nested conditional statements
are when you have conditional statements inside other conditional statements. This is often done to provide more specific and detailed conditions to check.
For example:
In this example, there are two levels of conditional statements. The outer if-else
statement checks the country, and then the inner if-else
statement checks the age.
If the country is "USA"
, the inner if-else
statement checks if the age is greater than or equal to 21
. If it is, it outputs "You are old enough to drink." If it is not, it outputs "You are not old enough to drink."
If the country is "Canada"
, the inner if-else
statement checks if the age is greater than or equal to 19
. If it is, it outputs "You are old enough to drink." If it is not, it outputs "You are not old enough to drink."
If the country is not "USA"
or "Canada"
, it outputs "Sorry, we do not have drinking age information for your country."
Nested conditional statements can also be used with switch statements, and with multiple levels of nesting. However, it is important to keep them as simple and clear as possible to avoid confusion and mistakes.
In the exercise below, select all conditional statements from JavaScript