Bird
Raised Fist0
C Sharp (C#)programming~20 mins

While loop execution model in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
While Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple while loop
What is the output of this C# code snippet?
C Sharp (C#)
int i = 0;
while (i < 3)
{
    Console.Write(i);
    i++;
}
A012
B123
C0 1 2
D0123
Attempts:
2 left
💡 Hint
Remember the loop runs while i is less than 3 and increments i each time.
❓ Predict Output
intermediate
2:00remaining
While loop with break statement
What will be printed by this C# code?
C Sharp (C#)
int count = 0;
while (count < 5)
{
    if (count == 3) break;
    Console.Write(count);
    count++;
}
A01234
B0123
C012
D123
Attempts:
2 left
💡 Hint
The break stops the loop when count equals 3.
🔧 Debug
advanced
2:00remaining
Identify the infinite loop
Which option causes an infinite loop when run?
C Sharp (C#)
int x = 0;
while (x < 5)
{
    Console.Write(x);
    // missing increment
}
A
int x = 0;
while (x &lt; 5)
{
    Console.Write(x);
}
B
int x = 0;
while (x &lt; 5)
{
    Console.Write(x);
    x++;
}
C
int x = 0;
while (x &lt; 5)
{
    x++;
    Console.Write(x);
}
D
int x = 0;
while (x &lt;= 5)
{
    Console.Write(x);
    x++;
}
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop.
❓ Predict Output
advanced
2:00remaining
While loop with nested condition
What is the output of this code?
C Sharp (C#)
int i = 0;
while (i < 5)
{
    if (i % 2 == 0)
        Console.Write(i);
    i++;
}
A246
B135
C01234
D024
Attempts:
2 left
💡 Hint
Only print i when it is even.
❓ Predict Output
expert
2:00remaining
While loop with complex condition and post-loop value
What is the value of variable 'result' after this code runs?
C Sharp (C#)
int i = 1;
int result = 0;
while (i < 10)
{
    if (i % 3 == 0)
        result += i;
    i += 2;
}
A21
B12
C15
D18
Attempts:
2 left
💡 Hint
Add i to result only if i is divisible by 3, increment i by 2 each time.

Practice

(1/5)
1. What does a while loop do in C#?
easy
A. Stops the program immediately
B. Executes code only once regardless of condition
C. Repeats code a fixed number of times
D. Repeats a block of code as long as a condition is true

Solution

  1. Step 1: Understand the purpose of a while loop

    A while loop runs repeatedly while its condition remains true.
  2. Step 2: Compare with other options

    Options B, C, and D do not describe the behavior of a while loop correctly.
  3. Final Answer:

    Repeats a block of code as long as a condition is true -> Option D
  4. Quick Check:

    while loop = repeats while condition true [OK]
Hint: While loops run only if condition is true before each repeat [OK]
Common Mistakes:
  • Thinking while loops run a fixed number of times
  • Confusing while with do-while loops
  • Assuming code runs at least once without checking condition
2. Which of the following is the correct syntax for a while loop in C#?
easy
A. while (condition) { /* code to repeat */ }
B. while condition { /* code to repeat */ }
C. while: (condition) { /* code to repeat */ }
D. while (condition); { /* code to repeat */ }

Solution

  1. Step 1: Recall correct while loop syntax

    The correct syntax requires parentheses around the condition and braces for the code block.
  2. Step 2: Identify syntax errors in other options

    A has a colon after while, B misses parentheses around the condition, D has a semicolon after the condition making the loop body empty.
  3. Final Answer:

    while (condition) { /* code to repeat */ } -> Option A
  4. Quick Check:

    Correct syntax = while (condition) { /* code to repeat */ } [OK]
Hint: Always use parentheses for condition and braces for loop body [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Adding semicolon after while condition
  • Using colon instead of parentheses
3. What will be the output of this C# code?
int count = 3;
while (count > 0) {
    Console.Write(count + " ");
    count--;
}
medium
A. 0 1 2 3
B. 3 2 1
C. 3 2 1 0
D. 1 2 3

Solution

  1. Step 1: Trace the loop iterations

    count starts at 3, prints 3, then decrements to 2, prints 2, then 1, prints 1, then decrements to 0 and stops.
  2. Step 2: Confirm output sequence

    The printed numbers are "3 2 1 " exactly in that order.
  3. Final Answer:

    3 2 1 -> Option B
  4. Quick Check:

    Loop prints count down from 3 to 1 [OK]
Hint: Count down loops print from start to 1 before stopping [OK]
Common Mistakes:
  • Including 0 in output
  • Printing numbers in ascending order
  • Forgetting to decrement count
4. Identify the error in this code snippet:
int i = 0;
while (i < 5)
    Console.Write(i);
    i++;
medium
A. Missing braces causes only Console.Write(i) to loop, i++ runs once
B. Syntax error due to missing semicolon after while
C. Infinite loop because i is never incremented
D. No error, code runs correctly

Solution

  1. Step 1: Analyze loop body without braces

    Only the first statement after while loops; here, only Console.Write(i) is inside the loop.
  2. Step 2: Understand effect on i++

    i++ runs once after the loop finishes, so i never increments inside the loop, causing an infinite loop if braces were not considered.
  3. Final Answer:

    Missing braces causes only Console.Write(i) to loop, i++ runs once -> Option A
  4. Quick Check:

    Braces needed to include i++ in loop [OK]
Hint: Use braces to include multiple statements in while loop [OK]
Common Mistakes:
  • Assuming both statements are inside loop without braces
  • Thinking missing semicolon after while is error
  • Confusing infinite loop cause
5. You want to read user input repeatedly until they type "exit". Which while loop correctly implements this in C#?
hard
A. while (true) { string input = Console.ReadLine(); if (input != "exit") break; }
B. string input; do { input = Console.ReadLine(); } while (input != "exit");
C. string input = Console.ReadLine(); while (input != "exit") { input = Console.ReadLine(); }
D. string input; while (input != "exit") { input = Console.ReadLine(); }

Solution

  1. Step 1: Understand the need to read input before loop

    We must read input once before the while condition to avoid skipping the first input.
  2. Step 2: Check each option's logic

    string input = Console.ReadLine(); while (input != "exit") { input = Console.ReadLine(); } uses priming read pattern correctly. string input; do { input = Console.ReadLine(); } while (input != "exit"); is do-while, not while loop. while (true) { string input = Console.ReadLine(); if (input != "exit") break; } breaks incorrectly on non-exit input, stopping too early. string input; while (input != "exit") { input = Console.ReadLine(); } uses uninitialized input, compile error.
  3. Final Answer:

    string input = Console.ReadLine(); while (input != "exit") { input = Console.ReadLine(); } -> Option C
  4. Quick Check:

    Read input before while, then loop while not "exit" [OK]
Hint: Read input once before while to check condition correctly [OK]
Common Mistakes:
  • Not reading input before while loop
  • Using do-while instead of while
  • Using infinite loop with break instead of while condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard