Movatterモバイル変換


[0]ホーム

URL:


Open In App

In Python, conditional statements help control the flow of a program by executing different blocks of code based on whether a condition is true or false. These statements allow decision-making in code. The main types of conditional statements are:

Let’s go through each of them with examples.

if Statement

An if statement is used when a block of code needs to be executed only if a specific condition evaluates to True.

Syntax

if condition:
# Statements to execute if condition is true

Below is the flowchart by which we can understand how to use if statement:

if-statement-in-Python

Example: In this example,if statement checks if 10 is greater than 5. If true, it prints "10 greater than 5"; regardless, it then prints "Program ended" as the next statement, indicating the program flow.

Python
if10>5:print("10 greater than 5")print("Program ended")

Output
10 greater than 5Program ended

Indentation (white space) is used to delimit the block of code. As shown in the above example, it is mandatory to use indentation in Python3.

if else Statement

In conditional if Statement the additional block of code is merged as else statement which is performed when if condition is false. 

Syntax  

if (condition):
# Executes this block if condition is true
else:
# Executes this block if condition is false

Below is the flowchart by which we can understand how to use if-else statement:

if-else-statement-in-Python

Example 1: In this example, code assigns value 3 to variable x and uses an if-else statement to check if x is equal to 4. If true, it prints "Yes"; otherwise, it prints "No," demonstrating a conditional branching structure.

Python
x=3ifx==4:print("Yes")else:print("No")

Output
No

Example 2: In this example, code uses a nested if-else chain to check value of the variable letter. It prints a corresponding message based on whether letter is "B," "C," "A," or none of specified values, illustrating a hierarchical conditional structure.

Python
letter="A"ifletter=="B":print("letter is B")else:ifletter=="C":print("letter is C")else:ifletter=="A":print("letter is A")else:print("letter isn't A, B and C")

Output
letter is A

Nested if Statement

if statement can also be checked inside other if statement. This conditional statement is called a nested if statement. This means inner if condition will only be checked if the outer if condition is True.

Syntax

if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true

Below is the flowchart by which we can understand how to use nested if statement:

nested-if-in-Python

Example: In this example, code uses a nested if statement to check if variable num is greater than 5. If true, it further checks if num is less than or equal to 15, printing "Bigger than 5" and "Between 5 and 15" accordingly, showcasing a hierarchical condition for refined control flow.

Python
a=10ifa>5:print("Bigger than 5")ifa<=15:print("Between 5 and 15")

Output
Bigger than 5Between 5 and 15

if-elif Statement

The if-elif statement is a shortcut for chaining multiple if-else conditions. While using if-elif statement at the end else block is added which is performed if none of the above if-elif statement is true.

Syntax

if (condition):
statement
elif (condition):
statement
else:
statement

Below is the flowchart by which we can understand how to use elif:

if-else-if-ladder-in-Python

Sequential Evaluation with if-elif-else Structure

Example: In this example, code uses an if-elif-else statement to evaluate value of the variable letter. It prints a corresponding message based on whether letter is "B," "C," "A," or none of the specified values, demonstrating a sequential evaluation of conditions for controlled branching.

Python
letter="A"ifletter=="B":print("letter is B")elifletter=="C":print("letter is C")elifletter=="A":print("letter is A")else:print("letter isn't A, B or C")

Output
letter is A

Using elif Inside Nested if Statements

Using elif within nested if statements in Python allows for more complex decision structures within a branch of another decision.

Example:In this example, outer if checks whether x is greater than 5. Inside it, a nested if-elif-else structure evaluates value of y to give more refined control.

Python
x=10y=5ifx>5:ify>5:print("x is greater than 5")elify==5:print("x is greater than 5 and y is 5")else:print("x is greater than 5 and y is less than 5")

Output
x is greater than 5 and y is 5

The structure of the above code provides conditional checks within another conditional check, enhancing the decision-making capabilities of your code.


if, else and elif in Python
Visit Courseexplore course icon
Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp