Introducing Loops and Conditionals

Modern JavaScript Using Control Structures
4 minutes
Share the link to this page
Copied
  Completed

Transcript

In this section of the course, we're going to be talking about control structures, specifically, loops and conditionals. Now, when you're writing code, there are times when you need to make decisions. If some variable is equal to something or greater than something, you need to execute some code. Otherwise, you do not execute that code. That's a conditional. Conditional simply determines whether some code is executed or not.

There are also times when you're writing code that you may need to repeat code multiple times. That is what we use a loop for, instead of having to write that code multiple times you write it once you put it inside a loop. The Loop goes through the code until some condition is met. And then it exits out of the loop and continues on with the rest of the code. So those are the two types of control structures. We're going to look at, let's first introduce the F conditional.

Gonna take a look at a diagram first that describes how it works. The F conditional begins with the word if it then checks a condition to see if some variable is equal to some value greater than some value. Basically, it determines whether something is true or false. That condition is contained within parentheses. Based upon whether the condition is true or false. It will execute certain code if it's true.

If it's false, you may also execute some code or it may just continue with the rest of the code and the program, depending on whether you have an else clause within that if conditional. Let's look at how that is structured inside of JavaScript. As I mentioned, you begin with the word if that's a reserved in JavaScript, you then enter parentheses, and inside of the parentheses you express the condition. This is the condition that is going to evaluate. If the condition evaluates to true, it executes some code, it executes the code that's inside of curly braces. And we put the end curly brace there.

Now if the condition is false, it will execute some code is inside the second set of curly braces, which comes after the else else is also a reserved word in JavaScript. Now, you can have numerous lines of code between each curly brace. It's not just a single line of code. In fact, technically, if you have just a single line of code, the curly braces are optional. But in everything we will do in this course, we will add to the curly braces. It's a much better idea to enter the curly brace braces.

It makes your code much readable. It also makes it more manageable. So let's make sure we use curly braces in all of our if conditions. Alright, let's take a look at a loop now. loops are for the purpose of executing code one or more times. Let's look at how that at the diagram that describes how that is structured.

So you have the start of the loop, there are several different types of loops in JavaScript, we'll be looking at the different types. You check a condition, if that condition is true, you execute the code of the loop. If the condition is false, you continue with the rest of the program, you exit out of the loop and continue with the rest of the program. So let's look at look at a sample while loop and how it would be set up in JavaScript. So we first have the key word While which is a reserved word in JavaScript, we then have inside of parentheses the condition to check whether something is true or false. Then we have curly braces.

Inside the curly braces, we have the code that we want to execute with the while loop. While that condition is true, it will continue to execute that code. As soon as that condition becomes false, it exit exits out of the loop and continues on with the rest of your JavaScript code, the rest of the program. So there's a quick introduction to conditionals and loops. Now let's start applying it to our JavaScript.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.