Nested Conditions Lesson

A Small Programming Scene

A nested condition is an if statement placed inside another if statement. This is useful when one decision should only happen after a first decision is already true.

The order matters. A program should check whether the user is logged in before checking whether that user has admin access, because the admin check only makes sense for someone who is already inside the system.

The Code Moment

Code Runner Challenge

Nested Conditions Lesson

View IPYNB Source
%%js

// CODE_RUNNER: Nested Conditions Lesson

let isLoggedIn = true;
let accountType = "admin";

if (isLoggedIn) {
  if (accountType === "admin") {
    console.log("Open admin tools");
  } else {
    console.log("Open user dashboard");
  }
} else {
  console.log("Show login screen");
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

What Changes

Nested conditions should stay readable. If the logic becomes too deep, the program may need a helper function or a simpler condition.