Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
int n = 0;
bool ok = n > 0 && ++n > 0;
Console.WriteLine(ok);
Console.WriteLine(n);False 0
- Expecting
False 1:&&stops as soon as its left side isfalse, so++nnever runs andnstays0. - Expecting
True 1, as if++nran first: C# always evaluates the left side of&&first, andn > 0isfalse.
- What does the program print if
&&is replaced with&? - What does it print when the left side is
n >= 0instead?