0

I have a section of code with several nested if statements that must be in a certain order. However, one of those ifs is optional. That is, if we do test condition1, we must do it first, but we can skip it if the user prefers. One can do this the following way:

option = True #user can toggle this

if option:
    if condition1:
        if condition2:
            if condition3:
                print("Hello World")
else:
     if condition2:
         if condition3:
            print("Hello World")

However, this reuses a lot of text. What is the best way to do this?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
aoristone
  • 21
  • 2
  • 3
    `if not option or condition1:`? – deceze Aug 01 '22 at 05:30
  • Are you concerned about more complex cases, like an else clause for `if condition2`? – tdelaney Aug 01 '22 at 05:40
  • @deceze condition1 is actually a memory-heavy function which I would prefer not to use unless necessary. Does python execute both sides of the or, or does it just output True as soon as it finds the first one is True? – aoristone Aug 01 '22 at 05:41
  • 2
    `or` is short-circuiting, meaning it stops at the first truthy value. – deceze Aug 01 '22 at 05:43
  • 1
    The latter is true - [Does Python support short-circuiting?](https://stackoverflow.com/q/2580136/6045800) – Tomerikoo Aug 01 '22 at 05:43
  • @tdelaney - yes, I have made it as minimal as possible. There are some elses in the nesting and occasional extra things it does before making the next if check. – aoristone Aug 01 '22 at 05:44

1 Answers1

3

The effect of this:

if condition1:
    if condition2:
        if condition3:
            print("Hello World")

Is the same as:

if condition1 and condition2 and condition3:
    print("Hello World")

Similarly, to replace your entire example:

if option:
    if condition1:
        if condition2:
            if condition3:
                print("Hello World")
else:
     if condition2:
         if condition3:
            print("Hello World")

This makes more sense:

if ((not option) or condition1) and condition2 and condition3:
    print("Hello World")

Since you want the same to happen in either case, which is to print that message.

Have a search for "Python boolean operators" to learn more about how not, and and or work.

Note that the parentheses are technically not required in this case, since and takes precedence over or, and not takes precedence over either - but since you'd have to remember that and there's no real impact from the parentheses, I figured putting them in and leaving them in makes sense.

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • OP wants more complex if/else clauses than what was shown in the example so the `if condition1 and condition2 and condition3:` part isn't going to work. – tdelaney Aug 01 '22 at 05:47
  • As @tdelaney said, I want it to perform more complex if/else behaviour, so turning it into chained ands is not suitable for my purposes. However, the ((not option) or condition1) in combination with the information above about short-circuiting solves my problem, so I have marked this comment as solving my question – aoristone Aug 01 '22 at 05:52