What is the purpose of an if-else statement in C#?
An if-else statement lets the program choose between two paths based on a condition. If the condition is true, it runs one block of code; if false, it runs another.
Click to reveal answer
beginner
What happens if the condition in an if statement is false and there is no else block?
If the condition is false and there is no else block, the program skips the if block and continues with the next code after the if statement.
Click to reveal answer
beginner
Explain the flow of execution in this code snippet:
If the score is 50 or more, the program prints "Pass". Otherwise, it prints "Fail". Only one message is printed based on the score.
Click to reveal answer
beginner
Can an if-else statement have multiple else blocks?
No, an if-else statement can have only one else block. To check multiple conditions, use else if blocks between if and else.
Click to reveal answer
beginner
What is the difference between if and else if in C#?
An if starts a condition check. Else if checks another condition only if previous if or else if conditions were false. Else runs if all previous conditions are false.
Since 10 is not greater than 10, the else block runs and prints "Not Greater".
Which keyword is used to check multiple conditions after an if in C#?
Aelse
Belseif
Celse if
Delif
✗ Incorrect
C# uses "else if" (two words) to check additional conditions after an if.
What happens if the if condition is true and there is an else block?
AOnly the if block runs
BOnly the else block runs
CBoth if and else blocks run
DNeither block runs
✗ Incorrect
Only the if block runs when the condition is true; the else block is skipped.
Can an if statement exist without an else block in C#?
ANo
BYes
COnly if inside a loop
DOnly with a switch
✗ Incorrect
An if statement can stand alone without an else block.
What is the output of this code if number = 0?
if (number > 0) { Console.WriteLine("Positive"); } else if (number < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); }
APositive
BNegative
CNo output
DZero
✗ Incorrect
Since number is 0, both if and else if conditions are false, so else block runs and prints "Zero".
Describe how an if-else statement controls the flow of a program.
Think about how the program decides which code to run.
You got /4 concepts.
Explain the difference between if, else if, and else blocks in C#.
Consider the order and exclusivity of these blocks.
You got /3 concepts.
Practice
(1/5)
1. What does the else block do in an if-else statement in C#?
easy
A. Runs before the if block
B. Runs when the if condition is true
C. Runs only if there is no if block
D. Runs when the if condition is false
Solution
Step 1: Understand the if-else structure
The if block runs when its condition is true.
Step 2: Role of the else block
The else block runs only when the if condition is false.
Final Answer:
Runs when the if condition is false -> Option D
Quick Check:
else runs if if is false [OK]
Hint: Else runs only if if-condition is false [OK]
Common Mistakes:
Thinking else runs when if is true
Confusing else with else if
Believing else runs before if
2. Which of the following is the correct syntax for an if-else statement in C#?
easy
A. if (condition) { } else { }
B. if condition { } else { }
C. if (condition) then { } else { }
D. if condition then { } else { }
Solution
Step 1: Check C# syntax rules
C# requires parentheses around the condition in an if statement.
Step 2: Identify correct syntax
Only if (condition) { } else { } uses parentheses and curly braces correctly.
Final Answer:
if (condition) { } else { } -> Option A
Quick Check:
Parentheses and braces required in C# if-else [OK]
Hint: Use parentheses around condition and braces for blocks [OK]
Common Mistakes:
Omitting parentheses around condition
Using 'then' keyword incorrectly
Missing curly braces
3. What is the output of this C# code?
int x = 5;
if (x > 10) {
Console.WriteLine("Greater");
} else {
Console.WriteLine("Smaller or equal");
}
medium
A. No output
B. Greater
C. Smaller or equal
D. Compilation error
Solution
Step 1: Evaluate the condition x > 10
Since x is 5, 5 > 10 is false.
Step 2: Determine which block runs
The else block runs because the if condition is false, printing "Smaller or equal".
Final Answer:
Smaller or equal -> Option C
Quick Check:
5 > 10 is false, so else runs [OK]
Hint: Check if condition is true or false to pick output [OK]
Common Mistakes:
Assuming if block runs when condition is false
Confusing output strings
Thinking code has syntax errors
4. Identify the error in this C# code snippet:
int num = 7;
if num > 5 {
Console.WriteLine("Big");
} else {
Console.WriteLine("Small");
}
medium
A. Missing parentheses around condition
B. Missing semicolon after if statement
C. Missing curly braces around else block
D. Incorrect variable type
Solution
Step 1: Check if statement syntax
C# requires parentheses around the condition in an if statement.
Step 2: Identify the error
The code lacks parentheses around num > 5, causing a syntax error.
Final Answer:
Missing parentheses around condition -> Option A
Quick Check:
If condition must be in parentheses [OK]
Hint: Always put condition inside parentheses in if [OK]
Common Mistakes:
Forgetting parentheses around condition
Adding semicolon after if line
Misplacing curly braces
5. You want to print "Positive", "Negative", or "Zero" based on an integer variable num. Which if-else structure correctly implements this?
hard
A. if (num > 0) { Console.WriteLine("Positive"); } else if (num <= 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); }
B. if (num > 0) { Console.WriteLine("Positive"); } else { if (num < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } }
C. if (num >= 0) { Console.WriteLine("Positive"); } else if (num <= 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); }
D. if (num > 0) { Console.WriteLine("Positive"); } else if (num > 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); }
Solution
Step 1: Understand the conditions needed
We need to check if num is positive, negative, or zero.
Step 2: Analyze each option's logic
if (num > 0) { Console.WriteLine("Positive"); } else { if (num < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } } correctly nests the else-if logic to handle all three cases without overlap or error.
Final Answer:
if (num > 0) { Console.WriteLine("Positive"); } else { if (num < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } } -> Option B
Quick Check:
Nested else-if covers all cases correctly [OK]
Hint: Use nested else if or else if for multiple conditions [OK]
Common Mistakes:
Using overlapping conditions
Missing zero case
Repeating same condition twice
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.