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
}
Example:
let age = 18;
if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote.");
}
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");
}
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
}
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");
}
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
}
Example:
for (let i = 1; i <= 5; i++) {
console.log("Count:", i);
}
While Loop
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++;
}
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);
Example:
let count = 1;
do {
console.log("Count:", count);
count++;
} while (count <= 5);
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);
}
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.");
}
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);
}
Practice for Today
- Write a program to check whether a number is even or odd using an
if-elsestatement. - Create a
forloop to print the multiplication table for a given number. - Use a
whileloop to calculate the sum of numbers from 1 to 50. - Modify a
forloop to skip numbers divisible by 3 usingcontinue.
Summary of Day 4
Today, we learned:
-
Conditional Statements: Making decisions using
if-elseandswitch-case. -
Loops: Repeating actions with
for,while, anddo-whileloops. - Break and Continue: Controlling loop flow.
- 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)