Back to questions
JuniorTheoryCommon
What does the program print?
int x = 7;
if (x > 3)
{
Console.Write("A");
}
else if (x > 5)
{
Console.Write("B");
}
Console.Write("C");AC
- Expecting
BC: the first true condition from the top picks the branch, not the strictest one, so withx = 7thex > 3branch runs. - Expecting
ABC: anelse ifis checked only when every condition above it is false, so two true conditions never run two branches.
- How would you reorder the branches so that the program prints
BCforx = 7? - What does the program print when
xis2?