- Conditionals allow executing code based on certain conditions
- if/else statements:
- if checks a condition and runs block if true
- else runs if condition is false
- Multi-way decisions with else if
- switch statements:
- Switch on a variable/expression
- Case labels to match values
- Default case catches the rest
- Examples:
if (age >= 18) { // allow vote } else { // too young }
switch(day) { case "Monday": // code here break; default: // default case }
- Loops allow repeating code:
- for loop - iterate over arrays, ranges
- while loop - repeat until condition is false
- do-while - run once before checking condition
- Loop examples:
for (let i = 0; i < 5; i++) { // repeats 5 times }
while (count > 0) { // repeats until count = 0 }
- Break and continue keywords
The goal is to explain the main conditional and looping structures with examples so readers can control program flow based on conditions.