Home >Web Front-end >JS Tutorial >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.
Conditional statements allow your code to make decisions based on certain conditions. JavaScript provides several ways to implement conditional logic.
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 }
Example:
let age = 18; if (age >= 18) { console.log("You are eligible to vote."); } else { console.log("You are not eligible to vote."); }
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"); }
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 }
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"); }
Loops are used to execute a block of code multiple times.
A for loop runs for a specific number of iterations.
Syntax:
for (initialization; condition; increment/decrement) { // Code to execute }
Example:
for (let i = 1; i <= 5; i++) { console.log("Count:", i); }
A while loop runs as long as a condition is true.
Syntax:
while (condition) { // Code to execute }
Example:
let count = 1; while (count <= 5) { console.log("Count:", count); count++; }
A do-while loop ensures the code executes at least once before checking the condition.
Syntax:
do { // Code to execute } while (condition);
Example:
let count = 1; do { console.log("Count:", count); count++; } while (count <= 5);
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); }
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 }
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."); }
Today, we learned:
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!