If-else execution flow in C Sharp (C#) - Time & Space Complexity
We want to understand how the time taken by an if-else decision changes as the input size changes.
Specifically, does choosing one path or another affect how long the program runs?
Analyze the time complexity of the following code snippet.
int CheckNumber(int n)
{
if (n > 0)
{
return 1;
}
else
{
return -1;
}
}
This code checks if a number is positive or not and returns a value accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else check.
- How many times: Exactly once per function call.
Whether the input is small or large, the program only does one check.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations stays the same no matter the input size.
Time Complexity: O(1)
This means the time to run this code does not grow with input size; it stays constant.
[X] Wrong: "If the input number is very large, the if-else will take longer to decide."
[OK] Correct: The if-else only checks one condition once, so size or value of the input does not affect the time.
Understanding that simple decisions take constant time helps you explain how your code behaves clearly and confidently.
"What if we added a loop that runs n times inside the if or else block? How would the time complexity change?"
Practice
else block do in an if-else statement in C#?Solution
Step 1: Understand the if-else structure
Theifblock runs when its condition is true.Step 2: Role of the else block
Theelseblock runs only when theifcondition is false.Final Answer:
Runs when theifcondition is false -> Option DQuick Check:
elseruns ififis false [OK]
- Thinking else runs when if is true
- Confusing else with else if
- Believing else runs before if
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 AQuick Check:
Parentheses and braces required in C# if-else [OK]
- Omitting parentheses around condition
- Using 'then' keyword incorrectly
- Missing curly braces
int x = 5;
if (x > 10) {
Console.WriteLine("Greater");
} else {
Console.WriteLine("Smaller or equal");
}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 CQuick Check:
5 > 10 is false, so else runs [OK]
- Assuming if block runs when condition is false
- Confusing output strings
- Thinking code has syntax errors
int num = 7;
if num > 5 {
Console.WriteLine("Big");
} else {
Console.WriteLine("Small");
}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 aroundnum > 5, causing a syntax error.Final Answer:
Missing parentheses around condition -> Option AQuick Check:
If condition must be in parentheses [OK]
- Forgetting parentheses around condition
- Adding semicolon after if line
- Misplacing curly braces
num. Which if-else structure correctly implements this?Solution
Step 1: Understand the conditions needed
We need to check ifnumis 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 BQuick Check:
Nested else-if covers all cases correctly [OK]
- Using overlapping conditions
- Missing zero case
- Repeating same condition twice
Ready to prove it?
Pass the test and this pattern moves unknown
