What's The Difference Between an 'if' And a 'while'


Hey 👋,

if you are reading this it might be because you are a little bit confused about when to use if and when to use a while and what’s the difference between them.

If that’s the case, keep reading.

Story time

Some years ago I was helping a first-year Computer Science student with some programming tasks.

One of the main reasons she kept failing and failing on the exercises was because she didn’t know the difference between an if and a while.

At the time we were using Python but the good news is that if and while concepts are used the same in all the other programming languages: PHP, JS, Java, Kotlin, …

And even better news: as soon as she understood the difference she started understanding better the exercises and doing them correctly.

Flashforward to the present, I was checking StackOverflow and found another person with the same exact problem. So, I decided to write this quick article in my blog in case it’s any helpful for anyone else in the future.

The problem

If I had to bet on why this is difficult to understand for some people I would blame language. Not in a programming language sense, language in a communication sense.

I don’t know if it’s because of being a non-native english speaker, but in some languages while and if translate to very similar words. Or at least, depending on the situation you could use one or the other interchangeably.

Read the following piece of code:

💡 Psst! I am using here PHP but this code must look very similar in any other language. Just take into account that echo is used to print in the screen some text.

if ($age > 18) {
	echo "You are at least 18 years old!\n";
}

In your mind you probably have read something like: if $age is greater than 18, display in the screen “You are at least 18 years old!”.

Now, let’s read the following one:

while ($age > 18) {
	echo "You are at least 18 years old!\n";
}

This might have resulted in something very similar in your head: while $age is greater than 18, display in the screen “You are at least 18 years old!”.

If this is exactly the kind of situation you are facing, I got you!. Let’s see how we can fix that.

Solution: Change your mental model.

If the above situation was confusing to you, then I want you to trust me and from now on do what I say. No questions asked.

From now on I want you to stop thinking of while and if as “similar” things. Get a big box and put the while on one mental box and the if in another.

Let’s label the boxes with “conditionals” and “loops”. And add the if into the conditionals box and the while into the loops box.

boxes

Now, let me try to clarify what’s the difference between both boxes:

So, if you want to run one piece of code or not depending on some condition you want to use the conditionals box (if). If you want to run a piece of code multiple times, then you need the loops box (while).

Hopefully that makes sense to you. But you might still not seeing a clear picture on both differences. Maybe you are not fully understanding why you would need to run a piece of code multiple times?

Let’s pause for a bit. Remember at the beginning I asked you to trust me? Then, trust me.

Try to imagine the boxes. The elements we added inside and the labels we gave each box. Pause and do it for some seconds.

Now let me give you a small “trick” you will use from now on when reading code.

Let’s use what we have learnt by reading again the previous pieces of code.

if ($age > 18) {
	echo "You are at least 18 years old!\n";
}

The new reading: if $age is greater than 18, display in the screen “You are at least 18 years old!” once.

while ($age > 18) {
	echo "You are at least 18 years old!\n";
}

The new reading: while $age is greater than 18, display in the screen “You are at least 18 years old!” repeatedly until $age is not greater than 18.

This means that the second example (while) will print the text multiple times, until the condition ($age > 18) is false.

⚠️ Warning! Since we are not changing $age at all in our code block (we are only printing the text) this while will run forever. So basically we created an infinite loop.

Is that any better now? If you are still a bit confused I can give you some tips:

Conclusion

First of all let me tell you that the confusion you are facing is perfectly normal and common when you are starting into programming.

I think that most of the difficulty here is understanding the “running code repeatedly” because you are not fully getting what it means.

Just remember that despite while and if sound and look very similar in code they are different things (remember the conditionals and loops boxes!). I am completely sure that soon, as you progress with programming, you will start using “loops” more and everything will make much more sense.

Hopefully this was a helpful for you. If that’s the case, click on the “like” button below so I know. If you have any further questions or comment fell free to reach out to me by email or on twitter.