Home > Web Front-end > JS Tutorial > Conditional Statements and Loops in JavaScript

Conditional Statements and Loops in JavaScript

Susan Sarandon
Release: 2024-12-15 15:50:15
Original
560 people have browsed it

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
}
Copy after login
Copy after login

Example:

let age = 18;
if (age >= 18) {
  console.log("You are eligible to vote.");
} else {
  console.log("You are not eligible to vote.");
}
Copy after login
Copy after login

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");
}
Copy after login

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
}
Copy after login

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");
}
Copy after login

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
}
Copy after login

Example:

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

While Loop

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

Syntax:

while (condition) {
  // Code to execute
}
Copy after login

Example:

let count = 1;
while (count <= 5) {
  console.log("Count:", count);
  count++;
}
Copy after login

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);
Copy after login

Example:

let count = 1;
do {
  console.log("Count:", count);
  count++;
} while (count <= 5);
Copy after login

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);
}
Copy after login

4. Real-World Examples

Password Validation

Check if a user’s password meets criteria.

Example:

if (condition) {
  // Code to execute if condition is true
} else {
  // Code to execute if condition is false
}
Copy after login
Copy after login

Counter

Use loops to count occurrences or perform repetitive actions.

Example:

let age = 18;
if (age >= 18) {
  console.log("You are eligible to vote.");
} else {
  console.log("You are not eligible to vote.");
}
Copy after login
Copy after login

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!

The above is the detailed content of Conditional Statements and Loops in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template