Get startedGet started for free

Conditionals

1. Conditionals

In order to make our programs intelligent, we can use conditional expressions.

2. What are conditional expressions?

Conditional expressions tell our computer to run some code if a condition is met. This allows us to write code that can make its own decisions. Here is an example of a conditional expression in Julia. In the script, there is a variable called is_raining. We can check whether its value is true by writing if followed by the variable name. Julia will only run the next indented line if is_raining is true. In this case, it is true, so the message will print. We must follow the if-statement with the end keyword to tell Julia that the statement has finished.

3. What are conditional expressions?

We don't need to indent the code inside the if statement; it will still run if we don't indent it, though indenting makes our code more readable.

4. What are conditional expressions?

If the value is false, then the message will not be printed.

5. Multiple lines of code under the if statement

Inside the if-block, we can have many lines of code. Here, we have two print statements. Any code which comes after the end keyword will always be run, even if the condition isn't met. This time the condition is true, so all three messages are printed. If false, only the message outside the if-statement is printed.

6. Comparisons

Instead of defining a boolean variable in the script, we can use comparisons to generate true and false values. For example, if it's raining, a variable storing the amount of rain will be greater than zero. We can use the greater-than operator to check this and return true or false.

7. Comparisons

We can then use this boolean in our conditional expressions.

8. Comparisons

We don't need to assign the boolean to a variable. We can evaluate the comparison inside the if-statement like this.

9. Other comparisons

We can use other comparisons to build sophisticated if-statements. The equals-equals operator tells us if two values are equal. This can be used to compare numbers, strings, or data types.

10. Other comparisons

The exclamation-equals operator checks if two values are not equal.

11. Other comparisons

We've already used the greater-than operator, but the greater-than-or-equal-to operator returns true if a is greater than or equal to one.

12. Other comparisons

The less than, and less-than-or-equal-to operators work similarly.

13. When the condition is not met

In a conditional statement, we can use the else keyword to tell Julia what to do when the condition is false. Julia runs the code after the if-statement when the condition is true and the code after the else when the condition is false.

14. Additional conditions

We can create more complex conditional statements by using the elseif keyword. The elseif condition is checked only when the if condition is false. In this example, only the first message is printed, even though the amount of rain is less than one, and the elseif condition is true.

15. Additional conditions

Here the amount_of_rain is zero-point-five, so the first condition is false. The elseif condition is then checked, and it is true. Therefore, only the second message is printed.

16. Additional conditions

If neither condition is met, the code under the else statement is run. In this example, neither condition is met when the amount_of_rain is greater than one. So the final message is printed.

17. Multiple elseif's

We can add many elseif statements. Julia will work down this list of statements checking them in order. Each elseif will only be evaluated if all of the statements above it are false. The code in the else-block will run when none of the conditions are true.

18. Let's practice!

If you're ready, let's practice. Else, we'll see you later.