HOWTO · Python

How to Do Nothing Inside an if Statement in Python

Learn how Python's pass statement leaves a conditional branch empty while keeping the block valid.

On this page

Python requires an indented statement after an if, elif, or else header. If a branch has no action yet, the pass statement lets you leave it empty while keeping the conditional syntactically complete. It is useful as a deliberate placeholder, but it does not skip the condition or implement behavior for you.

What pass does in an if block

pass is a no-operation statement: when Python reaches it, it performs no action. Execution then continues according to the surrounding control flow. In an if branch, Python proceeds after the conditional when that branch finishes; in a loop body, control may begin the next iteration instead of moving beyond the loop. The condition is still evaluated normally.

A suite cannot be left blank. For example, writing if ready: followed immediately by another statement at the same indentation level is not a valid way to leave the branch empty. Put pass in the branch when there is intentionally nothing to do. The same rule applies to other compound statements such as loops, functions, and class definitions: their suites need a statement, and pass can fill that role.

Use pass in a conditional branch

A branch containing pass remains part of the conditional. Python evaluates the condition, enters the matching branch, and then proceeds after the conditional once that branch is complete. An elif branch is considered when the preceding conditions are false, and its own condition must be true. An else branch has no condition; it runs only when all preceding if and elif conditions are false. pass does not cause another branch to run.

Run this example:

x = 10

if x > 5:
    pass
else:
    print("x is not greater than 5")

print("Continue with the rest of the program")
Continue with the rest of the program

With x set to 10, the condition is true, so Python enters the first branch and executes pass. The else body is skipped, and the final print runs. The supplied run completed successfully with exit code 0 and printed the output shown above. If you change x to 5 or a smaller value, the else body runs before the final statement.

Understand the example’s output

The example uses pass only in the branch where x > 5. It does not print anything from that branch, but it also does not stop the program. The later print remains outside the conditional, so it is reached after either branch finishes. When x is 5 or less, the else branch adds its message before that later statement.

This distinction matters when reading output: a no-op in one branch does not suppress statements after the conditional. Likewise, the branch that does not match is simply not selected. To change what the program displays, add the intended operation to the appropriate branch rather than expecting pass to alter the condition or affect other statements.

When to use pass and when to choose another statement

Use pass when a branch must exist but its implementation is intentionally deferred, such as while outlining a function or deciding how a particular input should be handled. Replace the placeholder with real behavior when that behavior is known. Until then, the branch silently does nothing, so a reader should be able to tell from surrounding names or a concise comment why it is present.

pass is not a way to temporarily disable working code during testing or debugging. It does not preserve the disabled statements, and it does not make a branch act as though its condition were false. To control execution while debugging, use a deliberate condition or debugger; to preserve code temporarily, use an appropriate version-control change or comment with care.

Choose a different statement when the program needs a real response. Use return to leave a function with a result (or explicitly with no value), continue to move to the next loop iteration, or break to leave a loop. These statements change control flow; pass does not. If no branch-specific action is needed at all, consider whether the conditional can be simplified instead of keeping an empty branch.