In our fourth post, we talked about the debtors and searching a bag for an item. We saw how break helps us not waste time searching once we've found the item we're looking for, and how continue lets the worker skip the items or people we're not interested in and move on to the next one.
But before either of these can happen, we need to tell the worker when to stop searching or when to skip something. That's why we need conditional statements.
In programming, a condition is a question the computer can answer with a simple yes or no (true or false), like "Has this debtor paid in full?" This is where we use the if and else statements, to tell the computer what to do depending on whether a condition is met.
It's the conditional statement that helps you tell the computer, in its language, what to do when a condition is met. For example, you can tell it that if a debtor has paid in full, the worker should skip that debtor's name and move on to the next one. The if statement checks whether the condition is true, while continue tells the worker to skip that debtor. In the same way, you can use if with break to stop the loop completely once a particular condition is met.
Let's look at an example using our bag of items:
let bag = ["book", "shoe", "ball", "pen"];
for (let i = 0; i < bag.length; i++) {
if (bag[i] === "ball") {
console.log("Found the ball!");
break;
}
}
Here, the if statement checks whether the current item is a ball. If the condition is true, the worker says "Found the ball!" and the break statement stops the loop.
We can also use a conditional statement with continue, like we did in our debtors example:
let debtors = [
{ name: "Tunde", paidInFull: true },
{ name: "Ada", paidInFull: false },
{ name: "John", paidInFull: false }
];
for (let i = 0; i < debtors.length; i++) {
if (debtors[i].paidInFull) {
continue;
}
console.log("Go to " + debtors[i].name + "'s house to collect the money.");
}
In this example, the if statement checks whether each debtor has paid in full. If the condition is true, continue skips that debtor and moves on to the next one. Otherwise, the worker is told to visit the debtor's house.
What about else?
Sometimes you want the worker to do something when the condition is not met. That's what else is for. It tells the computer what to do when the condition is false.
Here's the same debtors example, written with else instead of continue:
for (let i = 0; i < debtors.length; i++) {
if (debtors[i].paidInFull) {
console.log(debtors[i].name + " has paid in full. Skip.");
} else {
console.log("Go to " + debtors[i].name + "'s house to collect the money.");
}
}
This prints:
Tunde has paid in full. Skip.
Go to Ada's house to collect the money.
Go to John's house to collect the money.
The if checks whether the debtor has paid in full. If that's true, the worker skips him. If it's false, the else part runs, and the worker goes to the debtor's house. Only one of the two runs for each debtor.
You can use conditional statements not only in loops but also in functions and other parts of your code. They're useful anywhere you need the computer to make a decision based on whether a particular condition is met.
Thanks for reading. I hope you find this helpful.
Top comments (0)