If statements allow you to create more interesting programs as they provide additional code flows. Learn how to define and use them in this video.
- In the real world, we make decisions based on current conditions all of the time. If it's raining, I'll take my umbrella. If I'm sleepy, I'll grab a cup of coffee and so forth. The most common way for a computer to make decisions is by means of an if statement. We've seen an example of an if statement earlier in this course, but let's dive deeper to understand the ins and outs. An if statement has this general structure. It starts with the word if, that's where it gets its name from, then it checks some condition, and if that condition is true, then it will do the work that you've placed inside of it. To visualize this, think of it as a flow chart with only one check, if it's true, then the program will proceed to execute the statements. If not, then it just ignores those statements as if they don't exist. Here's a simple if statement in Python. It starts with the word if, then we have our condition, or boolean expression, five less than six. Next we have a colon, this is part of the syntax, or rules, of the Python if statement. And on the next line we have an indented line of code. This contains a print statement that will run if our condition is true. The indentation in Python is very important because that's how the interpreter knows which statements belong to the if. In fact, we have a name for statements that are grouped together this way in programming. It's called a block. In this example, there's only one statement in our block, but we could easily extend it like this. And now we have two statements in our block. In Python, the block is ended as soon as we come to a line that's not indented anymore. This new print statement isn't indented. It's not part of the previous if statement's block. Let's move over to VS Code to take a look at an example in the 04_02 exercise file. This code should be familiar to you. This is our code that we used before that includes an if statement. Let's go ahead and run it and check out the output. So we'll come down to run Python file in terminal. First, it asks what's your name? Let's go with Frank. And then, are you enjoying the course? We'll choose yes. And we can an output, that's good to hear, and then the final statement, but let's see what happens if we choose something other than yes. We'll close this down and run this code again. Once again for what's your name, I'll go with Frank. And then are you enjoying the course? I'm going to type dance. And as our output, we only get the final statement. Did you see what happened? I only got the final statement because the line of code inside the if was ignored. That condition answer equals equals yes evaluated to false. The only thing our if knows how to handle is an answer of yes, and dance certainly doesn't equal yes. Recall what I mentioned earlier about the importance of proper indentation. Let's see what would happen if we failed to properly indent our code for the if statement. We'll change that here on line number nine. So first let's close this down, come on line number nine right in front of our print statement, and we're going to delete it. Let's save and run our file again. This time we get an error, an indentation error. That's because Python requires the indentation so that it knows what code belongs to the if. It's standard in Python to use four spaces for doing some indenting. If we come back here to our code, we can close this down, we can use the tab button to add four spaces to our code. When we click tab, the IDE automatically converts this to spaces for us, which is pretty handy. If statements allow a program to take different paths while it's running. So far, we've just gone from top to bottom processing every single line only one path, but that's boring and it doesn't give us much opportunity to interact with the user. Now, we know how to switch things up based on some condition and we can start to write much more interesting programs.
- Writing source code
- Basic statements and expressions
- Working with numbers and strings
- Writing conditional code
- Increasing your efficiency with functions
- Working with comments
- Making decisions in code
- Troubleshooting errors
- Learning about other languages