Keywords: if statement, conditional statement, else statement, elif statement
This chapter will cover the if statements in Python. The if statement is a conditional statement that enables us to control the logic of our programs. Since all programming is based on conditions, the if statement can be seen as one of the cornerstones of programming.
A if statement in Python determines whether the program should perform one or several operations, depending on whether one or more prespecified conditions are reached or not. The condition specified is a logical expression that is either true or false. In other words, we use a logical expression to decide if our programs should execute a certain code or not. Meaning, by using the if statement, you can specify what should happen if a condition is met or not. This creates conditional programming, which is the basis of all logic in a program.
In short, the if statement in Python is:
The if statement is used to decide whether or not to perform an operation. If the condition is true, then the operation is performed. If the condition is false, the operation is not performed. The if statement can be combined with several if statements, so-called else-if and else statements.
The if statement is used to decide whether or not to perform an operation
In this chapter, we will go through how to combine if-statements with else statements, and elif statements to create conditional programming. The flowchart illustrates whether or not an operation should be performed, depending on whether or not the condition in the if statement is met.
It might be a good idea to repeat and remind ourselves of the logical operators we saw in the previous chapter. These will be very useful when using the if statement.
Please leave feedback and help us continue to make our site better.
How useful was this article?
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?