The Reason to Adopt “if” Statement

Lucas F. Lu
6 min readMar 24, 2020

--

We want you! To make a decision.

A guide for beginners of any background to start writing codes

In our previous articles, we learned about how to master a variety of loops. Giving you the ability to control the flow of a process by repeating a group of tasks. How about adding one more tool to your inventory and gain the ability to help computers make a decision?

Why do I want to learn “if” statement at all?

The short answer is, to solve dilemmas and perform tasks based on the decision within a program. Like always, the “why” can be explained from an example.

Let’s continue from our chat room example, which greets people by their name as they come in and limits the number of guests to just under 5. Let’s append one more interesting condition to this example, say forbid anyone who’s under 18 from entering the room?

Computers will need a way to examine this condition before it can proceed with the rest of the tasks. A loop simply isn’t up for the task since its purpose was to repeat. Certainly, you would argue to have seen condition checking within a loop statement. But that condition was used to decide if the loop should repeat or not.

We are scratching our heads because we can’t find a tool from our inventory to help computers make a decision whether or not to continue running when facing a dilemma.

Luckily, we have this wonderful yet powerful creature.

Introducing — The “If…else…” statement

This control structure is really simple to understand as the name itself is already self explanatory enough.

We understand it in a way such that, “if” something happens, we perform some tasks, but “else”, meaning if that something didn’t happen, we perform some other tasks.

To further explain this concept, let’s relate it to our example. We will then have, “if” the guest is under 18, I will display a message saying “You are under age.”. If a guest is not under 18 (“else”) I would do the normal thing by greeting the guest and update the guest count.

Pretty simple, right?

How to use the “If…else…” statement

Let’s translate what we talked about into codes and see how we come about implementing our intention into a program.

<?php
$counter=1;
for($counter; $counter<=5;$counter++) { $age = readline('Hi, how old are you? '); if($age<18){ echo 'I cannot let you in since you are underage.'; }else{ echo 'Good morning master '.readline('What is your name: '); } }
?>

Again, we use for loop to control the number of repetition to specifically 5

We declare a variable called age to store the guest age entered

We use keyword “if” to instruct the computer to perform a decision making process

In the brackets, we have the condition we want the computer to examine and make its decision based upon. In our case, it’s whether the value of variable “age” is less than 18

Again, curly brackets are used to define a scope of the operation. Grouping what’s inside the brackets to be the only thing executed by the computer when the decision is made.

When put under the keyword “if”, it’s meant to be the only group of codes getting executed when the condition examined is “true”, meaning if the guest is under 18.

We see the keyword “else” is used afterwards, signaling the computer to execute the following group of codes (the scope of “else”) only when the condition examined is not true, meaning the guest is over 18 years old.

That wasn’t too hard to understand right? The computer was facing a dilemma of which, 2 sets of tasks must be performed separately according to whether a guest is under 18 years old. We solved that dilemma by using the “if…else…” statement.

But, we might have a problem Houston.

If you haven’t noticed, the guest count is updated no matter what happens. The counter is increased by 1 even when the guest is under 18.

This is what we call a bug. It is the unintended or unexpected behavior of a program.

So, congratulations, you’ve got your first bug!

Troubleshooting

We often troubleshoot bugs with debugging programs, I will cover the use of those programs in later articles. For now, let’s run through our program logically.

  • We know we used a “for” loop and it will repeat what’s defined in its scope for 5 times
  • For the first time the loop executes its scope of codes, let’s assume a guest of age 10 enters the room
  • Program will first ask for the age to be entered, and of course, the guest enters 10
  • Value 10 is then stored inside a variable called age
  • Then we used “if” statement to check if it’s under 18
  • The condition is true because value of age is less than 18
  • Computer executes what’s defined within the scope for “true” condition by displaying the message
  • Computer skips running what’s defined within the scope of “else” condition because that condition was false.
  • Computer then moves on to the next loop. Wait! Not so fast. For loop will perform one extra task at the end of each loop, remember? That will be to increase the variable of “counter” by one.
  • But the guest is underage, we should not have increased the guest count by one. This is a problem, and indeed our bug.

We have found our bug. It is due to the fact that the “for” loop will increase the counter by 1 no matter what. Let’s see how we can go about fixing this issue below.

The fix of our bug

Due to the nature of a “for” loop which increases the value of the counter by 1 at the end of every loop. We then decide that maybe a “for” loop isn’t a good fit for our adjusted example. But what can we use to replace that?

We need a loop for sure, because we need to repeat and ask for names. We also need to increase the value of a counter by 1. Hmm…. how about a “while” loop again? Regardless, there’s no harm in trying to use that and see the result.

Let’s convert our current example into a “while” loop. Consider the following converted example codes.

<?php
$counter=1;
while ($counter<=5) { $age=readline('Hi, how old are you? '); if($age<18){ echo 'I cannot let you in since you are underage.'; }else{ echo 'Good morning master '.readline('What is your name: '); $counter++; } }
?>

I think this approach may just be a viable solution to our problem. What do you think?

Code Explanation:

The loop will run as long as our guest count is below or equal to 5

It will first ask for the age of the guest

It will then check to see if the age is below 18

It will not update the counter if the age is below 18. But conversely, increase the count by 1 if the age is greater than or equals to 18. Simply because the action of updating the guest count is defined within the scope of “else”, which is the scope of a condition triggered when age is more than or equals to 18

Awesome! That’s exactly what we wanted for our example.Now you know how to debug a program but most importantly, you now possess the skill to structure your thoughts and following the logic to decide what approach you need to implement your program.

Keep it simple and short

I want to lastly, touch on one more little detail. It’s going to be short, so please bear with me.

Remember how we want to keep everything as minimum and as short as possible? We can make a little adjustment and achieve that goal with our example above.

Turns out, we can ditch the declaration of variable “age” and again, insert the reading of a user’s input into where that variable is used, since the value coming back from the user is used only once here.

Thus, our program now looks like the following.

<?php
$counter=1;
while ($counter<=5) { If ( 18 > readline('Hi, how old are you? ') ){ echo 'I cannot let you in since you are underage.'; }else{ echo 'Good morning master '.readline('What is your name: '); $counter++; } }?>

Summary

  • We use the “if…else” statement because we have multiple sets of tasks for a computer to perform based on a dilemma.
  • We use the “if…else” statement also because only one of the many sets of tasks should be performed when facing a decision choice.
  • We define our dilemma within the brackets after the “if” keyword. A dilemma is like a condition that offers a choice for the computer to make
  • If the examination of condition is “true”, the scope defined under keyword “if” is executed
  • If the examination of condition is not “true”, the scope defined under keyword “else” is executed. Only one scope can be executed as the result of a dilemma.

I hope I did a pretty good job at showing you what a “if…else…” statement is, why we come about using it and how to use it. In the next article, we will discuss some tricks and features of this useful tool.

--

--

Lucas F. Lu

Laravel Expert, AI enthusiasts. Recently fell in love with helping people rebuild their lives by learning how to program.