Skip to Content
Learn
Conditional Expressions
Nested Conditionals

Similar to the concept of a “dream inside of a dream” from the movie Inception, a conditional can exist inside of another conditional. This occurrence is called a nested conditional.

By nesting a conditional inside of another conditional, we can achieve more complex and precise logical solutions.

For example, remember Conditional Creek? We can use a nested conditional to replicate this situation:

var sailColor = "red" var sailPattern = "striped" if (sailColor == "red") { println("Follow the lower river.") if (sailPattern == "striped") { println("Then take the right path.") } else { println("Then take the left path.") } } else { println("Take the higher river.") }

The first conditional checks the truth value of sailColor while the nested conditional checks the truth value of sailPattern. This program will output the following:

Follow the lower river. Then take the right path.

Instructions

1.

In order to be considered a planet, a celestial body must accomplish three things according to the International Astronomical Union:

  1. Must orbit a star (like the sun).
  2. Must have enough mass in order to achieve hydrostatic equilibrium (a nearly round shape).
  3. Must have enough mass to clear debris/small objects from its orbit.

The difference between a planet and a dwarf planet comes from rule 3.

In Planet.kt create an if expression that checks if orbitsStar AND hydrostaticEquilibrium are true.

Leave the body of the if expression blank for now.

2.

Inside the if expression, create a nested conditional that checks if the value of clearedOrbit is true.

Inside the body of this nested if exression, create a println() statement that outputs "Celestial body is a planet."

3.

Underneath the nested if expression, create an else expression that outputs "Celestial body is a dwarf planet." using a println() statement.

Folder Icon

Take this course for free

Already have an account?