Control Structures
Control structures are fundamental tools that allow developers to dictate how their code is executed based on conditions and logic.
We’ll explore common control structures, starting with conditional statements and loops. That too, in a lighter sense, cos over explanation can lead to confusion in this section, you should try the code on your own and figure it out by yourself based on the explanation below.
Conditional Statements
Section titled “Conditional Statements”Conditional statements enable decisions in a program by branching into different paths depending on the given logical conditions.
if
, else if
, else
Section titled “if, else if, else”The if
statement checks a condition and executes a block of code if it evaluates to true
.
Additional conditions can be added using else if
, and a default fallback can be provided with else
.
// simple if-else statementif (condition1) { // Code to execute if condition1 is true} else { // Code to execute if all condition1 is false}
const age = 18;if (age < 18) { console.log("Access denied: Must be 18 or older");} else { console.log("Access granted");}
// if else-if else block
if (condition1) { // Code to execute if condition1 is true} else if (condition2) { // Code to execute if condition2 is true} else { // Code to execute if all conditions are false}
const age = 18;if (age < 18) { console.log("You are a minor.");} else if (age === 18) { console.log("You just became an adult!");} else { console.log("You are an adult.");}
switch
statements
Section titled “switch statements”switch
is an alternative to multiple if...else
conditions and checks a value against multiple cases.
const day = "Monday";switch (day) { case "Monday": console.log("Start of the work week!"); break; case "Friday": console.log("End of the work week."); break; default: console.log("It's another day of the week.");}
Here you can see break
statement which is crucial in switch statements
as it stops the execution from “falling through”
to a subsequent case.
You can also use return
statement in place of break
to stop the execution on matched key.
The default
clause acts as a catch-all for any value that doesn’t match any case. It’s similar to the else
in if-statements.
The following snippet shows to deal with multiple cases sharing the same code block.
switch (fruit) { case "apple": case "pear": case "banana": console.log("This is a fruit"); break; case "carrot": console.log("This is a vegetable"); break; default: console.log("Unknown food type");}
Used to repeatedly execute a block of code while a condition is met or for a specified number of times.
for
Loop
Section titled “for Loop”The for
loop is great for running a block of code a specific number of times.
for (let i = 0; i < 5; i++) { console.log(`Iteration: ${i}`);}
while
Loops
Section titled “while Loops”The while
loop runs while a condition is true
.
let count = 0;while (count < 3) { console.log(`Count: ${count}`); count++;}
do...while
Loops
Section titled “do...while Loops”The do...while
loop guarantees the block of code executes at least once, even if the condition is false
.
do { console.log(`Value: ${x}`); x--;} while (x > 0);
Loop Control
Section titled “Loop Control”break
and continue
break
is used to exit a loop prematurely, halting further iterations.
for (let i = 1; i <= 10; i++) { if (i > 5) { break; // Exit the loop when i > 5 } console.log(i);}
continue
Section titled “continue”continue
skips the current iteration and moves on to the next one.
for (let i = 1; i <= 5; i++) { if (i === 3) { continue; // Skip iteration when i equals 3 }
console.log(i);}