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

If-else execution flow in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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"); }
ANot Greater
BGreater
CNothing
DError
Which keyword is used to check multiple conditions after an if in C#?
Aelse
Belseif
Celse if
Delif
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
Can an if statement exist without an else block in C#?
ANo
BYes
COnly if inside a loop
DOnly with a switch
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"); }
APositive
BNegative
CNo output
DZero
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.