In this post, we’ll look at one of the three tenets of problem-solving: Selection.
Selection is the process of taking action (or not) based on the evaluation of a condition(s). If the condition resolves to true, the corresponding actions are taken. If the condition does not resolve to true, then alternate actions (or no actions at all) may be taken.
There are plenty of examples in our day to day business operations where conditional statements come into play…likely without us really naming them as such. As you read through each statement below, think about what action takes place when the condition is true vs the action (if any) that takes place if the condition is not true. Additionally, see if you can identify what part of the statement is the condition vs the corresponding action.
To further reiterate this point on clearly defined conditions and their corresponding actions, let’s look at the anatomy of a conditional statement.
In the conditional statement provided, the condition we’re checking for is the weather outside after work. It’s the specific criteria we’re checking for before taking any corresponding action. The action in our sample statement is going for a run. What happens if it’s raining outside after work? Per this conditional statement, no action is defined, so no action would be taken. With conditional statements, the logic that comes immediately after is only executed if the condition itself resolves to true.
If you paid close attention, you’ve noticed that we went back and forth between the language of “selection” and “conditional” in this post. Thats because the two phrases mean the same thing, however in computer programming the concept of selection statements is typically referred to as “conditional statements” or “if statements” (because in almost all programming languages, such statements always begin with “if”).
Take a look at the video below to understand how these conditional/if statements can be used within a bot. The video also throws another wrinkle into the mix – else-if statements.
Hopefully, by now you feel like you have a better grasp on selection/conditional/if statements. Conditional statements are very commonly used in all programming – including bots. Knowing how to use conditional statements as a bot builder allows you to do logical checks for things like valid data, the presence of files, a window being open, etc…and allows you to instruct the bot on how it should react accordingly.
Know that when you hear other developers talking about if statements, they really just mean selection/conditional statements that can be used to check for certain criteria to be true before taking subsequent actions. With all of that in mind, consider including conditional statements in your next bot build. Go build better bots, and go be great!