Use an else if immediately after the body of an ifwhen you have more than one expression to evaluate. Expressions are evaluated in sequence (top to bottom) and the first evaluation that is true (non-zero) will be the one that executes the statements in its body. Any and all of the remaining expressions are ignored, even if they would have evaluated true.
In the following example, x, y and z are random integers that could be 0 or 1. We evaluate them one by one looking for the first that is non-zero. The comments show what we know to be true about X, Y, and Z at that point, based upon the evaluations that came before.
if( x=1 )
{
// X is definitely 1, but Y and Z might also be 1.
// do something..
}
else if( y=1 )
{
// X is not 1, but Y is definitely 1. Z might also be 1.
// do something else..
}
else if( z = 1 )
{
// Neither X nor Y is 1, but Z definitely is 1.
// do something else..
}
else
{
// X, Y and Z are all 0.
// when all else fails...
}
Copyright © 2026 eLLeNow.com All Rights Reserved.