BACK

Intro to Conditional Statements

Intro to Conditional Statements

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.

Examples:

  • If there is a new invoice on the X drive (a shared folder), then Nacy will pay it.
    • The condition in this example is “a new invoice on the X drive”
      • If a new invoice shows up, then Nacy will pay it
      • If a new invoice does not show up, no action (per this conditional statement) would take place as no alternative is defined.
  • If the customer ID is read from this transfer form and it matches an existing customer, then we can process the transfer.
    • The condition in this example is checking for 2 conditions:
      • 1. The customer ID exists/is read from the form
      • 2. The customer ID matches an existing customer
    • If both of those conditions are true, then a transfer can be processed.
    • If either of those conditions is false, then the transfer cannot be processed…and again with this example, no alternative action is defined.
  • If I have a meeting at 9 am, I will join it, else I will work on the expense reporting that is due.
    • The condition in this example is the presence of a committed-to 9 am meeting
      • If the meeting exists (meaning our conditional statement is true), the person defining this statement would join the meeting
      • If the meeting does not exist (meaning our conditional statement is false), the person defining this statement will work on the expense report.
    • In this example, notice the language of else – it defines what should happen should the conditional statement not resolve to true.

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.

Conclusion

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!