Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python - if , if..else, Nested if, if-elif statements
Next article icon

For more complex decision trees,Pythonallows for nested if statements where one if statement is placed inside another. This article will explore the concept of nested if statements in Python, providing clarity on how to use them effectively.

Python Nested if Statement

A nested if statement in Python is an if statement located within another if or else clause. This nesting can continue with multiple layers, allowing programmers to evaluate multiple conditions sequentially. It's particularly useful in scenarios where multiple criteria need to be checked before taking an action.

Example of Nested If Statements

Let’s consider a practical example to understand how nested if statements work in Python.

Python
age=30member=Trueifage>18:ifmember:print("Ticket price is $12.")else:print("Ticket price is $20.")else:ifmember:print("Ticket price is $8.")else:print("Ticket price is $10.")

Output
Ticket price is $12.

In this example, the outer if checks the age, while the inner if checks the membership status to determine the ticket price.

Syntax of Nested If Statements in Python

The basic syntax of a nested if statement in Python is as follows:

if condition1:

# Code to execute if condition1 is true

if condition2:

# Code to execute if both condition1 and condition2 are true

Flowchart of Nested if Statement

The flowchart illustrates the concept of a nested if statement in programming.

Nested-statement
Flowchart

Here’s a summarized explanation of its structure:

  1. Initial Condition Check:
    • An if statement evaluates a primary condition.
    • If true, the flow proceeds to another if statement nested inside.
  2. Nested Condition Check:
    • This second if statement checks an additional condition.
    • If this nested condition is true, a specific block of code executes.
    • If false, a different block of code executes, placed immediately below.
  3. Else Clause:
    • If the initial if condition is false, the flow moves to the else block.
    • This else block contains its own set of operations that execute when the primary condition fails.

Nested if with else Condition

Nested if statements with else conditions are especially useful when decisions require more than one criterion to be evaluated in a sequence. This structure allows developers to refine the decision process at each level, managing specific actions based on various conditions.

Python
i=0;# if condition 1ifi!=0:# condition 1ifi>0:print("Positive")# condition 2ifi<0:print("Negative")else:print("Zero")

Output
Zero

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
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