0
0
C Sharp (C#)programming~20 mins

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

Choose your learning style9 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.