Challenge - 5 Problems
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple for loop with increment
What is the output of the following C# code?
C Sharp (C#)
for (int i = 0; i < 3; i++) { Console.Write(i); }
Attempts:
2 left
💡 Hint
Remember the loop starts at 0 and runs while i is less than 3.
✗ Incorrect
The loop starts at 0 and runs while i is less than 3, printing i each time without spaces. So it prints 0, then 1, then 2, resulting in '012'.
❓ Predict Output
intermediate2:00remaining
For loop with break statement output
What will be printed by this C# code?
C Sharp (C#)
for (int i = 0; i < 5; i++) { if (i == 3) break; Console.Write(i); }
Attempts:
2 left
💡 Hint
The loop stops when i equals 3.
✗ Incorrect
The loop prints i starting from 0. When i reaches 3, the break stops the loop before printing 3. So output is '012'.
❓ Predict Output
advanced2:00remaining
For loop with multiple variables and complex condition
What is the output of this C# code snippet?
C Sharp (C#)
for (int i = 0, j = 5; i < j; i++, j--) { Console.Write(i + j); }
Attempts:
2 left
💡 Hint
Add i and j each loop iteration and print the sum.
✗ Incorrect
Loop runs while i < j. Iterations:
1) i=0, j=5 sum=5
2) i=1, j=4 sum=5
3) i=2, j=3 sum=5
After third iteration, i=3, j=2 stops loop. Output is '555'.
❓ Predict Output
advanced2:00remaining
For loop with continue statement effect
What will be the output of this C# code?
C Sharp (C#)
for (int i = 0; i < 5; i++) { if (i % 2 == 0) continue; Console.Write(i); }
Attempts:
2 left
💡 Hint
The continue skips even numbers.
✗ Incorrect
The loop skips printing when i is even (0,2,4). It prints only odd i values less than 5: 1 and 3. So output is '13'.
🧠 Conceptual
expert2:00remaining
Number of iterations in nested for loops
How many times will the innermost statement execute in this nested loop?
C Sharp (C#)
for (int i = 1; i <= 3; i++) { for (int j = i; j <= 3; j++) { Console.WriteLine(i * j); } }
Attempts:
2 left
💡 Hint
Count how many times the inner loop runs for each i.
✗ Incorrect
When i=1, j runs 1 to 3 → 3 times
When i=2, j runs 2 to 3 → 2 times
When i=3, j runs 3 to 3 → 1 time
Total = 3 + 2 + 1 = 6 times.