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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-Else 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 this C# code?

C Sharp (C#)
int x = 10;
int y = 20;
if (x > y)
{
    Console.WriteLine("X is greater");
}
else if (x == y)
{
    Console.WriteLine("X equals Y");
}
else
{
    Console.WriteLine("Y is greater");
}
AX is greater
BX equals Y
CY is greater
DNo output
Attempts:
2 left
💡 Hint

Compare the values of x and y carefully.

Predict Output
intermediate
2:00remaining
If-else with boolean conditions

What will this program print?

C Sharp (C#)
bool isRaining = false;
bool haveUmbrella = true;
if (isRaining && haveUmbrella)
{
    Console.WriteLine("Go outside");
}
else
{
    Console.WriteLine("Stay inside");
}
AStay inside
BCompilation error
CGo outside
DNo output
Attempts:
2 left
💡 Hint

Both conditions must be true for the first block to run.

🔧 Debug
advanced
2:00remaining
Identify the error in if-else syntax

Which option contains a syntax error in the if-else structure?

C Sharp (C#)
int number = 5;
if (number > 0)
    Console.WriteLine("Positive");
else
    Console.WriteLine("Non-positive");
A
if (number > 0)
    Console.WriteLine("Positive")
else
    Console.WriteLine("Non-positive");
B
if (number > 0) {
    Console.WriteLine("Positive");
} else {
    Console.WriteLine("Non-positive");
}
C
if (number > 0)
    Console.WriteLine("Positive");
else
    Console.WriteLine("Non-positive");
D
if number > 0 {
    Console.WriteLine("Positive");
} else {
    Console.WriteLine("Non-positive");
}
Attempts:
2 left
💡 Hint

Check the syntax for the if statement in C#.

Predict Output
advanced
2:00remaining
Output of if-else with multiple conditions

What does this program print?

C Sharp (C#)
int score = 85;
if (score >= 90)
{
    Console.WriteLine("Grade A");
}
else if (score >= 80)
{
    Console.WriteLine("Grade B");
}
else if (score >= 70)
{
    Console.WriteLine("Grade C");
}
else
{
    Console.WriteLine("Grade F");
}
AGrade F
BGrade B
CGrade C
DGrade A
Attempts:
2 left
💡 Hint

Check which condition matches the score first.

🧠 Conceptual
expert
3:00remaining
Understanding if-else execution flow with variable changes

Consider this code snippet. What is the final value of result after execution?

C Sharp (C#)
int x = 5;
int result = 0;
if (x > 3)
{
    result = 10;
    if (x < 10)
    {
        result += 5;
    }
    else
    {
        result += 20;
    }
}
else
{
    result = -10;
}
A15
B10
C20
D-10
Attempts:
2 left
💡 Hint

Trace the nested if-else carefully and update result step by step.