DEV Community

Cover image for Conditional Statements and Loops in JavaScript
arjun
arjun

Posted on

Conditional Statements and Loops in JavaScript

Day 4: Conditional Statements and Loops in JavaScript

Welcome to Day 4 of learning JavaScript! Today, we’ll focus on conditional statements and loops, which form the backbone of logic and iteration in programming. By the end of this lesson, you’ll be able to make decisions in your code and repeat actions efficiently.


1. Conditional Statements

Conditional statements allow your code to make decisions based on certain conditions. JavaScript provides several ways to implement conditional logic.

If-Else Statement

The if statement checks a condition and executes code if the condition is true. The else statement provides an alternative path when the condition is false.

Syntax:

if (condition) {
  // Code to execute if condition is true
} else {
  // Code to execute if condition is false
}
Enter fullscreen mode Exit fullscreen mode

Example:

let age = 18;
if (age >= 18) {
  console.log("You are eligible to vote.");
} else {
  console.log("You are not eligible to vote.");
}
Enter fullscreen mode Exit fullscreen mode

Else-If Ladder

Use else if to test multiple conditions.

Example:

let score = 75;
if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 75) {
  console.log("Grade: B");
} else {
  console.log("Grade: C");
}
Enter fullscreen mode Exit fullscreen mode

Switch-Case Statement

The switch statement is an alternative to multiple if-else blocks. It’s ideal when you have many conditions based on a single variable or expression.

Syntax:

switch (expression) {
  case value1:
    // Code to execute if expression === value1
    break;
  case value2:
    // Code to execute if expression === value2
    break;
  default:
    // Code to execute if no cases match
}
Enter fullscreen mode Exit fullscreen mode

Example:

let day = 3;
switch (day) {
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  case 3:
    console.log("Wednesday");
    break;
  default:
    console.log("Invalid day");
}
Enter fullscreen mode Exit fullscreen mode

2. Loops

Loops are used to execute a block of code multiple times.

For Loop

A for loop runs for a specific number of iterations.

Syntax:

for (initialization; condition; increment/decrement) {
  // Code to execute
}
Enter fullscreen mode Exit fullscreen mode

Example:

for (let i = 1; i <= 5; i++) {
  console.log("Count:", i);
}
Enter fullscreen mode Exit fullscreen mode

While Loop

A while loop runs as long as a condition is true.

Syntax:

while (condition) {
  // Code to execute
}
Enter fullscreen mode Exit fullscreen mode

Example:

let count = 1;
while (count <= 5) {
  console.log("Count:", count);
  count++;
}
Enter fullscreen mode Exit fullscreen mode

Do-While Loop

A do-while loop ensures the code executes at least once before checking the condition.

Syntax:

do {
  // Code to execute
} while (condition);
Enter fullscreen mode Exit fullscreen mode

Example:

let count = 1;
do {
  console.log("Count:", count);
  count++;
} while (count <= 5);
Enter fullscreen mode Exit fullscreen mode

3. Break and Continue

  • Break: Exits the loop immediately.
  • Continue: Skips the current iteration and moves to the next one.

Example:

for (let i = 1; i <= 10; i++) {
  if (i === 5) break; // Stops the loop when i is 5
  console.log(i);
}

for (let i = 1; i <= 10; i++) {
  if (i === 5) continue; // Skips iteration when i is 5
  console.log(i);
}
Enter fullscreen mode Exit fullscreen mode

4. Real-World Examples

Password Validation

Check if a user’s password meets criteria.

Example:

let password = "abc123";
if (password.length >= 8) {
  console.log("Password is valid.");
} else {
  console.log("Password must be at least 8 characters long.");
}
Enter fullscreen mode Exit fullscreen mode

Counter

Use loops to count occurrences or perform repetitive actions.

Example:

let start = 1;
let end = 10;
console.log("Counting from 1 to 10:");
for (let i = start; i <= end; i++) {
  console.log(i);
}
Enter fullscreen mode Exit fullscreen mode

Practice for Today

  1. Write a program to check whether a number is even or odd using an if-else statement.
  2. Create a for loop to print the multiplication table for a given number.
  3. Use a while loop to calculate the sum of numbers from 1 to 50.
  4. Modify a for loop to skip numbers divisible by 3 using continue.

Summary of Day 4

Today, we learned:

  1. Conditional Statements: Making decisions using if-else and switch-case.
  2. Loops: Repeating actions with for, while, and do-while loops.
  3. Break and Continue: Controlling loop flow.
  4. Real-world examples like password validation and counters.

Next Steps

In Day 5, we’ll dive into Functions and Scope, focusing on how to organize and reuse code effectively. Stay tuned for Dec 12, 2024!

Top comments (0)