Python if…else Statement
Decision making in any programming language is used to execute a block of code if certain conditions are met. They are helpful for developers to make decision-based programs.
With the help of the control structure, the computer makes a decision by evaluating the logical expression. It allows our program to choose a different path of execution based upon the outcome of an expression or the state of the variable.
There are four kinds of decision making statements in Python:-
- if statement
- if..else statements
- if…elif…else statements
- nested if statements
Contents
What is if statement in Python?
The test expression is evaluated and if the condition results in True, then only the block of code is executed.
The syntax of the if statement is:-
Flowchart for if statement:-
Example code for Python if statement to check if a number is positive:
number = int(input("Enter the value of a: ")) if number >= 0: print("The number is positive") print("The program continues")
The output of the above program is:-
The number is positive
The program continues
What is the if…else statement in Python?
The if…else statement consists of an if statement followed by a statement or block of statements, followed by else keyword which is again followed by another statement or block of statement. In an if…else statement, the condition is evaluated first. If the condition is true, the statement in the immediate block is executed. If the condition is false, the statement in the else block is executed. This is used to decide whether to do something at a special point or to decide between two courses of action.
The syntax of the if statement is:-
Flowchart for if…else statement:-
Example code for Python if…else statement to check if a number is positive or negative:
number = int(input("Enter the value of a: ")) if number >= 0: print("The number is positive") else: print("The number is negative") print("The program continues")
The output of the above program is:-
The number is negative
The program continues
What is the if…elif…else statement in Python?
Sometimes we wish to make a multi-way decision based on several conditions. The most general way of doing this is by using the else if variant on the if statement. This works by cascading several comparisons. As soon as one of these gives a true result, the following statement or block is executed, and no further comparisons are performed. The elif is used for else if statement in Python.
The syntax of the if statement is:-
Flowchart for if…else statement:-
Example code for Python if…elif…else statement to check if a number is positive or negative or zero:
number = int(input("Enter the value of a: ")) if number > 0: print("The number is positive") elif number == 0: print("The number is zero") else: print("The number is negative") print("The program continues")
The output of the above program is:-
The number is zero
The program continues
What is the nested if statement in Python?
Nested ifs are very common in programming. Nested if is a structure which has another if…else body within its body of the structure. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block. In Python, indentation is the only way to separate the block of statements.
The syntax of the if statement is:-
Flowchart for if…else statement:-
Example code for Python nested statement to check if a number is positive or negative or zero:
number = int(input("Enter the value of a: ")) if number >= 0: if number == 0: print("The number is zero") else: print("The number is positive") else: print("The number is negative") print("The program continues")
The output of the above program is:-
The number is zero
The program continues