[Solved] how will the code execution be done inside asp.net c# (IF, else IF , IF ,IF)


Putting a bit of indentation to your code will clear your problem

// Test values, change them to change the output
int c1 = 1;
int c2 = 2;
int c3 = 3;
int c4 = 4;

if(c1 == 1)
    Console.WriteLine("Condition1 is true");
else if (c2 == 2)
    if(c3 == 3)
        if(c4 == 4)
            Console.WriteLine("Condition2,3 and 4 are true");
        else
            Console.WriteLine("Condition4 is false but 3 and 2 are true");
    else
        Console.WriteLine("Condition3 is false but 2 is true");
else
    Console.WriteLine("Condition1 and 2 are false");

In your example there are no curly braces to delimit blocks of statements to be executed if the conditions are true, so the ifs end at the first semicolon.

When condition1 is true nothing in the else chain will be evaluated because both ifs on condition3 and 4 depends on the condition1 being false and condition2 being true.

If condition1 is false then condition2 is evaluated and, if true, the code goes to check condition3 and goes to check condition4 if condition3 is true,

Of course, depending on what actions you need to perform on the input values this could be written simply as

if(c1 == 1)
    Console.WriteLine("Condition1 is true");
else if (c2 == 2 && c3 == 3 && c4 == 4)
    Console.WriteLine("Condition2,3, and 4 are true");

EDIT

After adding the braces now the code behavior is totally different

if(condition1)
{
    // enters here if condition1 is true, thus the condition2 is not evaluated
}
else if (condition 2)
{
    // enters here if condition1 is evaluated and is false, 
    // but the condition2 is true 
}


if(condition3)
{
   // enters here if condition3 is true, indipendently from the
   // result of the evaluation of condition1 and 2 
}
if(condition4)
{
   // enters here if condition3 is true, indipendently from the
   // result of the evaluation of condition1 and 2 
}

1

solved how will the code execution be done inside asp.net c# (IF, else IF , IF ,IF)