A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/nested-if-statement-in-python/ below:

Nested-if statement in Python - GeeksforGeeks

Nested-if statement in Python

Last Updated : 12 Jul, 2025

For more complex decision trees, Python allows 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 = 30
member = True

if age > 18:
    if member:
        print("Ticket price is $12.")
    else:
        print("Ticket price is $20.")
else:
    if member:
        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.

Flowchart

Here’s a summarized explanation of its structure:

  1. Initial Condition Check:
  2. Nested Condition Check:
  3. Else Clause:
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 1
if i != 0:
    # condition 1
    if i > 0:
        print("Positive")
        
    # condition 2
    if i < 0:
        print("Negative")
else:
    print("Zero")
    


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4