Recall & Review
beginner
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:<br>
if (score >= 50) { Console.WriteLine("Pass"); } else { Console.WriteLine("Fail"); }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.
Click to reveal answer
What will this code print if x = 10?<br>
if (x > 10) { Console.WriteLine("Greater"); } else { Console.WriteLine("Not Greater"); }✗ Incorrect
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#?
✗ 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?
✗ 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#?
✗ Incorrect
An if statement can stand alone without an else block.
What is the output of this code if number = 0?<br>
if (number > 0) { Console.WriteLine("Positive"); } else if (number < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); }✗ 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.