Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript5 tutorials
  • Node.js Introduction2 tutorials
  • JavascriptJavascript · Beginner · Mid
  • JavascriptJavascript · Beginner · Short
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 6

Making Decisions: Conditionals and Loops

So far, our programs have run from top to bottom, one line after another. Real programs need to make decisions ("if the cart is empty, show a message") and repeat work ("for each item in the list, add tax"). This chapter covers the two tools for that: **conditionals** (choosing which code runs) and
Level: BeginnerLength: Mid2 min read

Read at another depth

Making Decisions: Conditionals and Loops

So far, our programs have run from top to bottom, one line after another. Real programs need to make decisions ("if the cart is empty, show a message") and repeat work ("for each item in the list, add tax"). This chapter covers the two tools for that: conditionals (choosing which code runs) and loops (running code many times).

Conditionals: Choosing with `if`

An if statement runs a block of code only when a condition is true. A condition is an expression that evaluates to true or false, usually built with comparison operators like === (equal to) or > (greater than).

Code example in javascript
const temperature = 28;

if (temperature > 25) {
  console.log("It's warm — wear a t-shirt.");
}

Add else to handle the other case, and else if to test more options:

Code example in javascript
const temperature = 12;

if (temperature > 25) {
  console.log("Warm");
} else if (temperature > 10) {
  console.log("Mild");
} else {
  console.log("Cold");
}

Only the first condition that matches runs; the rest are skipped.

A common trap: `=` vs `===`

  • = assigns a value: x = 5
  • === compares values: x === 5

Writing if (x = 5) assigns instead of comparing, which is almost always a bug.

Loops: Repeating Work

A for loop repeats a block a set number of times. It has three parts: a starting counter, a condition to keep going, and a step.

Code example in javascript
for (let i = 1; i <= 5; i++) {
  console.log(`Count is ${i}`);
}

Here, i starts at 1, and the loop body runs as long as i <= 5. After each run, i++ adds 1. The output is 1, 2, 3, 4, 5.

A while loop repeats as long as its condition stays true. Use it when you don't know in advance how many times you need to repeat:

Code example in javascript
let cups = 0;

while (cups < 3) {
  cups++;
  console.log(`Drank cup number ${cups}`);
}

Combining Both

Conditionals inside loops let you filter or act on some items:

Code example in javascript
const scores = [45, 78, 92, 60];

for (let i = 0; i < scores.length; i++) {
  if (scores[i] >= 60) {
    console.log(`Score ${scores[i]}: pass`);
  } else {
    console.log(`Score ${scores[i]}: fail`);
  }
}

scores.length is the number of items in the array, so the loop automatically adapts to any list size.

Flowchart starting at the loop condition, then a diamond decision checking if the score is at least 60, printing pass or fail, then looping back
100%
Flow of an if/else statement checked at each loop iteration
Show diagram source
flowchart TD
  A[Start loop: i = 0] --> B{i < scores.length?}
  B -- no --> E[Done]
  B -- yes --> C{scores[i] >= 60?}
  C -- yes --> F[Log pass]
  C -- no --> G[Log fail]
  F --> H[i++]
  G --> H
  H --> B

Exercise

What does this print? let n = 2; while (n < 10) { n = n * 2; } console.log(n);

Show answer

16. The loop doubles n (2 → 4 → 8 → 16) and stops when n is no longer less than 10, so the final value is 16.

Exercise

Rewrite the scores loop using a for loop that counts only the failing scores into a variable, then log how many failed.

Show answer

let fails = 0; for (let i = 0; i < scores.length; i++) { if (scores[i] < 60) { fails++; } } console.log(fails);

Spotted a mistake, or code that does not work?

Report an issue

Chapter: Making Decisions: Conditionals and Loops

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that