top of page

## What are Conditionals?

Conditionals are the way computers make decisions.

## Why Conditionals?

In programming, you computer asks to check conditions by writing conditional statements. They also help programmers make decisions absed on the state of a situation.

## How do you Create Conditionals?

All conditionals begin with the word if. This declares the beginning of a conditional.

## if

Next, add the condition you are checking after the if.

## if x > 2

Then, add a colon after the condition you are checking.

## if x > 2:

Finally, add the code you want to execute based on the conditional by indenting the codeblock.

## if x > 2:   print(x)   z = x

There are two other types of conditionals that exist aside from "if". We will explain them as simply as possible here.

## Elif Conditionals

Let's start off with the example from above.

## if x > 2:   print(x)   z = x

Now let's say there was another specific condition you wanted to test alongside the first one. How would you do that? Well, it's actually very simple.

All you have to do is add another if statement, but replace the word if with elif. An easy way to memorize this is remembering that elif stands for else if.

## if x > 2:   print(x)   z = x elif x == 2:print(x)y = x

And that's all there is to conditionals! You can use them anywhere throughout your code, from functions to loops(which we will learn in the next lesson).

## Else Conditionals

Once again, let's use the example from above.

## if x > 2:   print(x)   z = x elif x == 2:print(x)y = x

Let's say that you have tested all the specified conditions you needed to and want to execute some code if all of the specified conditions are not met in any way. How could you do that?

It is very simple. All you have to do is type in the word else followed by a semicolon.

## if x > 2:    print(x)    z = x elif x == 2:print(x)y = x else:    print("Nothing")

Now if x is not less than or equal to 2, it will print "nothing" into the console.

And that's all there is to conditionals. In the next lesson, you will learn about loops and how to use conditionals inside of them. See you there!

bottom of page