JavaScript Control Structures

πŸ“Œ What are Control Structures?

Definition: Control structures are the building blocks that control the flow of a program based on conditions and repetitions.

Meaning: They allow you to make decisions (like β€œif this, do that”), repeat tasks, or choose between options.

Purpose: To make your code dynamic and responsive by enabling decision-making, looping, and flow control.

πŸ”€ Types of Control Structures

1. Conditional Statements - Detailed Explanation

πŸ”Ή if Statement

Purpose: Execute a block of code only if a specific condition is true.

How it works: The condition inside parentheses if(condition) is evaluated. If true, the code block inside curly braces {} runs. If false, it skips.

let age = 16;
if (age < 18) {
  console.log("You are a minor.");
}
// Output: You are a minor.
  

πŸ”Ή if...else Statement

Purpose: Choose between two alternatives. Run one block if the condition is true, otherwise run the other block.

How it works: If the condition in if() is true, run that block; if false, run the block inside else.

let age = 20;
if (age < 18) {
  console.log("You are a minor.");
} else {
  console.log("You are an adult.");
}
// Output: You are an adult.
  

πŸ”Ή if...else if...else Statement

Purpose: Test multiple conditions in sequence, running the first block where the condition is true, or the else block if none are true.

How it works: Checks conditions in order. When one is true, its block executes and the rest are skipped.

let age = 70;

if (age < 18) {
  console.log("You are a minor.");
} else if (age < 60) {
  console.log("You are an adult.");
} else {
  console.log("You are a senior citizen.");
}
// Output: You are a senior citizen.
  

2. Looping Statements

Use: Repeat a block of code multiple times until a condition is met.

πŸ” for loop

for (let i = 1; i <= 5; i++) { console.log("Count: " + i); }

πŸ”„ while loop

let i = 1; while (i <= 3) { console.log("Looping: " + i); i++; }

πŸ”ƒ do...while loop

let j = 1; do { console.log("Running at least once: " + j); j++; } while (j <= 2);

🧭 for...of (array iteratoin )

let fruits = ["apple", "banana", "cherry"]; for (let fruit of fruits) { console.log(fruit); }

πŸ“š for...in (object iteration)

let person = { name: "Alice", age: 25 }; for (let key in person) { console.log(key + ": " + person[key]); }

3. Branching Statements

Use: Exit or skip parts of loops or blocks.

πŸ”š break

for (let i = 0; i < 5; i++) { if (i === 3) break; console.log(i); // Output: 0, 1, 2 }

⏭️ continue

for (let i = 0; i < 5; i++) { if (i === 2) continue; console.log(i); // Output: 0, 1, 3, 4 }

πŸ” return (inside function)

function greet(name) { if (!name) return "Name is required."; return "Hello " + name; }

πŸ“ Assignments: Conditionals & Loops

Conditionals Assignments
  1. Write a program to check if a number is positive, negative, or zero.
  2. Create a program that determines if a user is eligible to vote (age >= 18).
  3. Write a program to find the largest of three numbers using if...else.
  4. Check if a given year is a leap year.
  5. Write a program to print "Good Morning", "Good Afternoon" or "Good Evening" based on current hour.
  6. Use ternary operator to decide if a number is even or odd.
  7. Create a login simulation: print welcome message if username matches, else show error.
  8. Write a program to check if a character is a vowel or consonant.
  9. Determine grade based on score (A, B, C, D, F) using if...else if.
  10. Write a program that outputs "Weekend" or "Weekday" based on day input.
Loops Assignments
  1. Print numbers from 1 to 50 using a for loop.
  2. Calculate the factorial of a given number using a while loop.
  3. Print all even numbers between 1 and 100.
  4. Sum all numbers from 1 to n, where n is user input.
  5. Print the multiplication table of a given number.
  6. Find the sum of digits of a number using a loop.
  7. Print the Fibonacci series up to n terms.
  8. Display all prime numbers between 1 and 100.
  9. Use a for...of loop to print all elements of an array.
  10. Write a program to reverse a string using a loop.