I have an if - else if - else condition but I want to pass through all if's until the end to check all conditions. Currently it stops if the first if is true. How do I do that?
I have an if - else if - else condition but I want to pass through all if's until the end to check all conditions. Currently it stops if the first if is true. How do I do that?
Know the answer? Post it. Somebody with the same question will find it here.



In C#, if ? else if ? else is exclusive.
That means:
If the first if is true ? all other conditions are skipped.
else if runs only when previous conditions are false.
else ifUse multiple independent if statements:
if (condition1)
{
// do something
}
if (condition2)
{
// do something
}
if (condition3)
{
// do something
}Now all conditions will be evaluated, even if the first one is true.
if (condition1)
{
...
}
else if (condition2)
{
...
}
else if (condition3)
{
...
}if (condition1)
{
Console.WriteLine("C1 true");
}
if (condition2)
{
Console.WriteLine("C2 true");
}
if (condition3)
{
Console.WriteLine("C3 true");
}
else
{
Console.WriteLine("C3 false");
}else ifUse else if only when conditions are mutually exclusive
(only ONE should run).
Example — checking multiple validation errors:
List errors = new();
if (age Console.WriteLine(e));
}Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ganesan CRank #369 · 6.8K reputationPosted Sep 7, 2021, 12:41 PM EDT
David MccarterRank #10 · 63.8K reputationPosted Sep 7, 2021, 12:08 PM EDT
Marius VasileRank #785 · 2.9K reputationPosted Sep 7, 2021, 12:22 PM EDT