Lecture 8: JavaScript Control Flow – If Statements & Loops


Welcome back, coders! 🎉 Now that you’ve gotten a taste of JavaScript, it’s time to take things further. In this lecture, we’ll explore one of the most important aspects of programming—control flow. Control flow allows you to control the flow of your code based on conditions or repeat actions multiple times. We’ll cover if statements and loops, two powerful tools in any JavaScript developer’s toolkit!

If Statements: Making Decisions

An if statement is a fundamental building block in programming that helps you make decisions based on conditions. Think of it as a way to ask a question in your code. If the answer is true, your code will perform one action. If the answer is false, it will perform another action (or nothing at all).

Here’s the basic syntax of an if statement:


  if (condition) {
    // Code to run if the condition is true
  }

Let’s look at an example:


  let age = 18;

  if (age >= 18) {
    alert("You are an adult!");
  } else {
    alert("You are a minor.");
  }

In this example, if the user’s age is 18 or older, it will display “You are an adult!” Otherwise, it will show “You are a minor.”

Else and Else If: Adding More Choices

Sometimes, you need more than just a true or false decision. For those cases, we can use else if and else to create multiple conditions:


  let score = 85;

  if (score >= 90) {
    alert("Excellent!");
  } else if (score >= 75) {
    alert("Good job!");
  } else {
    alert("Keep trying!");
  }

This code checks three different ranges for the score. It will display a message based on the score’s value.

Loops: Repeating Code

Sometimes, you’ll want to repeat code multiple times. This is where loops come in. A loop runs a block of code repeatedly as long as a condition is true. There are several types of loops in JavaScript, but we’ll focus on the two most common ones: the for loop and the while loop.

For Loop

A for loop is used when you know how many times you want to repeat a block of code. Here’s the basic syntax:


  for (let i = 0; i < 5; i++) {
    console.log(i);
  }

In this example, the loop will run 5 times, printing the numbers 0 through 4 to the console.

While Loop

A while loop is used when you want to repeat code as long as a condition remains true. Here’s the basic syntax:


  let i = 0;
  while (i < 5) {
    console.log(i);
    i++;
  }

This loop does the same thing as the previous one, but the condition is checked at the start of each iteration, and the loop continues until the condition becomes false.

Next Steps:

Now that you know how to control the flow of your code with if statements and loops, it’s time to practice! Here are some exercises:

  • Write an if statement that checks if a user’s input is a positive number and displays a message accordingly.
  • Write a for loop that prints out all even numbers between 1 and 20.
  • Use a while loop to count down from 10 to 1, printing each number.

In our next lecture, we’ll dive into JavaScript functions, learning how to create reusable blocks of code that can make your programs cleaner and more efficient.

Until next time, happy coding! 🌐💻

Post a Comment

0 Comments