In our everyday lives we are faced with having to make many different types of choices.
For each step we take in a certain direction you can bet that there are numerous forks in the road or doors waiting on either side of that chosen path, forcing us to walk down one path or open one door, but never both at the same time.
This is much like the structure of Conditional Statements in programming, as they revolve around the exact same concept, that being choices. You present a scenario, and for that scenario a door is in returned opened, giving back to you a result on the other side.
We could translate this over to the infamous weather example that is usually used when you first begin learning conditionals. The program is supplied with an answer to a question, a choice as to how a person needs to react, in this case towards something such as the weather. We could be given three different doors but which does the user open? With a conditional we are lucky enough to not be blind on our way to making a choice, because this is a situation that can gain an answer by the coder or user simply deciding to look out the window.
Doing so answers the question of “What is the weather like?” And, depending on the weather we are given a door to open. If it’s sunny, the first condition is met and we are told “The weather is great today! Don’t forget sunscreen!” The path has been chosen and that door has been opened. Else, if it’s not sunny, and instead when we look outside all we see is buckets of rain splashing onto the sidewalk, maybe we should take an umbrella, causing the second condition to be met, and the second door to open. You can view my full example in the image below.
The point of this is, that by using Conditionals we create our paths and open our doors, allowing the program to head in a specific direction. Because much like real life, there are various instances in coding when you need to perform different actions, depending on different choices.
If you’re not new to programming I’m sure you’re well and truly familiar with this concept, but I figured this post would be a good opportunity to expand understanding for those who might be new to programming or even conditional statements in general. Below you can also find a Flowchart, breaking down this process further.
Either way I hope you guys enjoyed this little post, if you have ideas of topics you’d like me to write on, leave me the suggestions in the comments down below ^_^.