reading-notes

Operators and Loops

JavaScript expression

An expression is any valid unit of code that resolves to a value.

There are two primary categories of expressions:

Using a loop

We use loops because they offer a quick and easy way to perform an action repeatedly. They allow a programmer to execute the same block of code a specific number of times or until a certain condition is met, essentially acting as a computerized way to handle repetitive tasks.

Excecuting the ending of a `for` loop

A for loop stops executing when its specified condition evaluates to false. During the loop’s execution cycle, the condition expression is checked; if it is found to be false, the loop terminates immediately, and the program moves to the next statement after the loop.

`while` loop execution characteristic

The number of times a while loop executes depends entirely on its condition:


Analogy for Understanding Loops:

You can think of a loop like a track runner. The initialization is the runner stepping onto the starting line, the condition is the official checking if the race is still ongoing (e.g., “Have you finished 10 laps yet?”), and the afterthought is the runner completing a lap and incrementing their lap count. The runner keeps going as long as the condition to continue is “true,” but as soon as they reach their goal and the condition becomes “false,” they stop running.