4) If-Then Statements

If-Then statements are an extremely important part of any program. They provide a quick and easy way to filter out information.

If-Then statements are comparative statements that will run certain code if a condition is true. They can compare any type of basic information including strings, numbers, and Boolean values. Below is a program comparing Boolean values.

If-then statements are a lot like locks. If the keys are right, then the following code will run. In the example above, is_hairy is the only True variable. Since only one key works, the code will not print anything out.

The above method is not the only way to create If-then statements. The example below will always print something.

Sometimes, programmers need more than two results for their code. The ‘elif’ command was made exactly for that purpose. else + if = elif

Don’t worry, I didn’t forget about numbers and strings. String and number comparisons are very similar to each other. As demonstrated below, comparing the two requires the use of ‘==’, ‘>=’, or ‘<=’.

Leave a Reply