Back to questions
JuniorTheoryCommon
What does the program print?
int sum = 0;
for (int i = 2; i <= 6; i += 2)
{
sum += i;
}
Console.WriteLine(sum);12
- Expecting
10: the stepi += 2runs after the body, so the first iteration hasi = 2, and the sum is2 + 4 + 6. - Expecting
20: before an iteration withi = 8, the condition8 <= 6is false, so the loop ends without adding8.
- What does the program print if the condition becomes
i < 6? - How many times is the condition
i <= 6checked while the loop runs?