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

If statement execution flow in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If Statement Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if-else statements
What is the output of the following C# code?
C Sharp (C#)
int x = 10;
int y = 20;
if (x > 5)
{
    if (y < 15)
        Console.WriteLine("A");
    else
        Console.WriteLine("B");
}
else
{
    Console.WriteLine("C");
}
AA
BNo output
CC
DB
Attempts:
2 left
💡 Hint
Check the conditions inside both if statements carefully.
Predict Output
intermediate
2:00remaining
If-else if-else chain output
What will be printed when this code runs?
C Sharp (C#)
int score = 75;
if (score >= 90)
    Console.WriteLine("Excellent");
else if (score >= 70)
    Console.WriteLine("Good");
else
    Console.WriteLine("Try again");
ATry again
BGood
CExcellent
DNo output
Attempts:
2 left
💡 Hint
Check which condition matches the score value first.
Predict Output
advanced
2:00remaining
If statement with multiple conditions
What is the output of this code snippet?
C Sharp (C#)
int a = 5;
int b = 10;
if (a > 0 && b < 5)
    Console.WriteLine("X");
else if (a > 0 || b < 5)
    Console.WriteLine("Y");
else
    Console.WriteLine("Z");
AY
BNo output
CZ
DX
Attempts:
2 left
💡 Hint
Evaluate each condition carefully using logical AND and OR.
Predict Output
advanced
2:00remaining
If statement with assignment inside condition
What will be printed by this code?
C Sharp (C#)
int x = 0;
if ((x = 5) > 3)
    Console.WriteLine(x);
else
    Console.WriteLine("No");
A5
B0
CNo
DCompilation error
Attempts:
2 left
💡 Hint
Remember that assignment inside if condition changes the variable value.
🧠 Conceptual
expert
3:00remaining
If statement execution flow with multiple conditions and side effects
Consider the following code. What is the final value of variable result after execution?
C Sharp (C#)
int a = 2;
int b = 3;
int result = 0;
if (a++ > 2 && ++b > 3)
    result = a + b;
else
    result = a - b;
Aresult = 6
Bresult = 3
Cresult = 0
Dresult = 5
Attempts:
2 left
💡 Hint
Remember how post-increment and pre-increment operators work and short-circuit evaluation.