Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 838 Bytes

03_control_flow.md

File metadata and controls

43 lines (33 loc) · 838 Bytes

Control Flow

Conditional statements

Conditional statements execute different blocks of code based on conditions. For example:

let temperature = 25;

if (temperature > 30) {
  console.log("It's hot outside");
} else if (temperature > 20) {
  console.log("It's warm outside");
} else {
  console.log("It's cool outside");
}

Loops

Loops are used to repeatedly execute code. JavaScript has for, while, and do...while loops. Here's an example using a for loop:

for (let i = 0; i < 5; i++) {
  console.log(i);
}

Break and continue statements

The break statement is used to exit a loop, while the continue statement skips the current iteration. Example:

for (let i = 0; i < 5; i++) {
  if (i === 2) {
    continue;
  }
  console.log(i);
  if (i === 3) {
    break;
  }
}