Question

I have the following:

string compararcom610b;
//Variables.Gcombinadofavdesf is defined in another class

if (something)
{
}
else if (Variables.Gcombinadofavdesf == 1)
{
    //do stuff_1
}
else if (compararcom610b == "1" && Variables.Gcombinadofavdesf == 1)
{
    //do stuff_2
}

Assuming that in this right moment compararcom610b = 1 and Variables.Gcombinadofavdef = 1, will the stuff_1 be done as well as stuff_2?

Or will only stuff_1 be done?

Was it helpful?

Solution

Since you use else if as soon as it enter in a block it will skip the others.

it will only execute stuff 1.




If you want both to be executed use this :

if (something)
{
}
else if (Variables.Gcombinadofavdesf == 1)
{
    //do stuff_1
}

if (compararcom610b == "1" && Variables.Gcombinadofavdesf == 1)
{
    //do stuff_2
}

OTHER TIPS

If Variables.Gcombinadofavdesf == 1, then do stuff_1.

Otherwise [else] if compararcom610b == "1" and Variables.Gcombinadofavdesf == 1, do stuff_2.

Does that make more sense?

The second condition on the second else if, the one that checks if Variables.Gcombinadofavdesf == 1 again, is redundant. If Variables.Gcombinadofavdesf == 1 the first check for its value will result in the second else-if being skipped. If it's false, the second check will be false as well.

In an if else-if else-if ... else statement combo, only one will ever be executed. You can think of each extra else-if as being a backup plan if the if before it fails.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top