Home > Blockchain >  nested if else condition
nested if else condition

Time:11-26

Hi I have a question regarding the following nested condition (python):

If (A meet B):
     condition 1;
     If(A meet B):
          condition2;

does it mean that if A meet B perform condition 1, and if A meet B again, perform condition 2?

CodePudding user response:

Yes, that's how it works. Here is an example:

program:

a = 10
b = 10
if a == b:
    print("a is equals to b")
    if a == b:
        print("a is again equals to b")

output:

a is equals to b
a is again equals to b

CodePudding user response:

The first if loop is applied for every condition you set, so both outputs will show you Condition1 and Condition2. Here is a simple example to understand it:

x = 2
if x == 2:
    print("hello")
    if x == 2:
        print("goodbye")

Output: hello goodbye

But if the first if loop modifies the initial condition, the second if loop would not work:

x = 2
if x == 2:
    x  = 1
    if x == 2:
        x -= 4
print(x)

Output: x = 3

  • Related