Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6899591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:26:26+00:00 2026-05-27T07:26:26+00:00

If the nested if-statement doesn’t fulfill a condition, how do I continue on to

  • 0

If the nested if-statement doesn’t fulfill a condition, how do I continue on to the outer if? Eg. I have this (very impractical) example:

a = 2

if( a > 1 ):
    if( a == 3 ):
        print "yes"
elif( a == 2 ):
    print "yes"

I want a == 2 to be checked next. How would I do this?

(I have multiple conditions in that nested if that I need to check so I’d rather not have a huge string of and/or statements in that one outer-if. I also have more than one elif statement so I don’t want to mash all the elifs together under that nested one.)

A deeper example:

b = 8

if( a > 1 ):
    if( b == 3 ):
        doSomething()
    elif( b == 4 ):
        doSomethingElse()
    elif( b == 5 ):
        more()
elif( -1 <= a <= 1 ):
    asd()
elif( a < -1 ):
    if( b == 7 ):
        asdfasdf()
    elif( b == 8 ):
        asasdf()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:26:27+00:00Added an answer on May 27, 2026 at 7:26 am

    How do you expect this to be clear to a computer?

    if a > 1:
      # ANYTHING
    elif a == 2:
      # ANYTHING
    

    by definition will never execute anything in the second block.

    I believe your intend might be more along this lines:

    def action():
      if a > 1:
        if b == 2:
          doSomething()
          return
        if b == 3:
          doSomethingElse()
          return
     if a == 2: # Note: NO elif!
       doDefaultForA2()
       return
     showError("Unknown command")
     return
    

    There are multiple ways to achieve this behaviour. I like the function+return pattern, because it structures code nicely. Another way is to use a handled flag:

    unhandled = True
    if a > 1:
      if b == 2:
        doSomething()
        unhandled = False
      if unhandled and b == 3:
        doSomethingElse()
        unhandled = False
    if unhandled and a == 2: # Note: NO elif!
      doDefaultForA2()
      unhandled = False
    if unhandled:
      showError("Unknown command")
    

    (You can obviously put in a number of shortcuts here by using elif. But using explicit “if unhandled” makes the code more verbose about the logic and easier to add new options.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure that can be very easily represented using a three-deep nested
I have two for loops nested like this: for(...) { for(...) { } }
Below I have a nested if statement which loads a certain CSS file depending
I have a nested switch statement inside a switch statement. I am wondering if
Is it possible to do a join to a nested select statement in SubSonic
Nested layouts do not work in Rails 3. After I hit this I tried
I have some nested tables that I want to hide/show upon a click on
How have you explained nested arrays to a programmer. I'm thinking someone that has
I have a nested function to show/hide paragraphs news-ticker-style. The problem is that when
I have a procedure SelectProc which contains a SELECT statement. I want to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.