An if statement helps your program decide what to do based on a condition. It lets your program choose different paths, like making a choice in real life.
When you want to check if a number is positive or negative and do something different for each.
When you want to check if a user entered the correct password before allowing access.
When you want to decide what message to show based on the time of day.
When you want to perform an action only if a button is clicked.
When you want to check if a list is empty before processing it.
Syntax
C Sharp (C#)
if (condition)
{
// code to run if condition is true
}
else
{
// code to run if condition is false
}
The condition is a statement that is either true or false.
The else part is optional and runs only if the if condition is false.
Examples
This checks if number is greater than zero and prints a message if true.
C Sharp (C#)
int number = 10;
if (number > 0)
{
Console.WriteLine("Number is positive");
}
This prints "Not positive" because the number is less than zero.
C Sharp (C#)
int number = -5;
if (number > 0)
{
Console.WriteLine("Positive");
}
else
{
Console.WriteLine("Not positive");
}
Checks a true/false condition and prints a message accordingly.
C Sharp (C#)
bool isRaining = true;
if (isRaining)
{
Console.WriteLine("Take an umbrella");
}
else
{
Console.WriteLine("No umbrella needed");
}
Sample Program
This program checks the temperature and prints if it is warm or cold.
C Sharp (C#)
using System;
class Program
{
staticvoid Main()
{
int temperature = 15;
if (temperature > 20)
{
Console.WriteLine("It's warm outside.");
}
else
{
Console.WriteLine("It's cold outside.");
}
}
}
OutputSuccess
Important Notes
Only one block of code runs: either the if block or the else block.
You can have multiple if statements to check different conditions one after another.
Summary
An if statement lets your program choose between two paths.
The condition inside if must be true or false.
The else block runs only if the if condition is false.
Practice
(1/5)
1. What does an if statement do in C#?
easy
A. It defines a new variable.
B. It repeats code multiple times.
C. It chooses which code to run based on a condition.
D. It stops the program immediately.
Solution
Step 1: Understand the purpose of if
An if statement checks a condition to decide which code runs next.
Step 2: Compare options
Only It chooses which code to run based on a condition. describes choosing code based on a condition, which matches the if statement's role.
Final Answer:
It chooses which code to run based on a condition. -> Option C
Quick Check:
if chooses code path = C [OK]
Hint: If checks true/false to pick code path quickly [OK]
Common Mistakes:
Confusing if with loops
Thinking if defines variables
Believing if stops program
2. Which of these is the correct syntax for an if statement in C#?
easy
A. if (x > 5) Console.WriteLine("Yes") end
B. if x > 5 then Console.WriteLine("Yes")
C. if x > 5: Console.WriteLine("Yes")
D. if (x > 5) { Console.WriteLine("Yes"); }
Solution
Step 1: Review C# if syntax
C# requires parentheses around the condition and braces for the block.
Step 2: Check each option
if (x > 5) { Console.WriteLine("Yes"); } uses parentheses and braces correctly; others use wrong keywords or punctuation.
Final Answer:
if (x > 5) { Console.WriteLine("Yes"); } -> Option D
Quick Check:
Correct C# if syntax = D [OK]
Hint: Remember: if(condition) { code } in C# [OK]
Common Mistakes:
Omitting parentheses around condition
Using Python or other language syntax
Missing braces for code block
3. What will this code print?
int x = 10;
if (x < 5) {
Console.WriteLine("Less");
} else {
Console.WriteLine("More or equal");
}
medium
A. Less
B. More or equal
C. Nothing
D. Error
Solution
Step 1: Evaluate the condition
The condition x < 5 is false because x is 10.
Step 2: Determine which block runs
Since the condition is false, the else block runs, printing "More or equal".
Final Answer:
More or equal -> Option B
Quick Check:
Condition false -> else runs = A [OK]
Hint: If false, else block runs [OK]
Common Mistakes:
Thinking if block runs when false
Ignoring else block
Confusing < with >
4. Find the error in this code:
int num = 7;
if num > 5 {
Console.WriteLine("Big");
} else {
Console.WriteLine("Small");
}
medium
A. Missing parentheses around the condition
B. Missing semicolon after if
C. Wrong keyword else
D. Variable num not declared
Solution
Step 1: Check if statement syntax
C# requires parentheses around the condition in an if statement.
Step 2: Identify the error
The code has if num > 5 { without parentheses, causing a syntax error.
Final Answer:
Missing parentheses around the condition -> Option A
Quick Check:
Parentheses needed in if condition = A [OK]
Hint: Always put condition inside parentheses in if [OK]
Common Mistakes:
Forgetting parentheses in if
Adding semicolon after if line
Misusing else keyword
5. You want to print "Positive", "Negative", or "Zero" based on an integer n. Which code correctly uses if statements to do this?
hard
A. if (n > 0) { Console.WriteLine("Positive"); } else if (n < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); }
B. if (n > 0) Console.WriteLine("Positive"); if (n < 0) Console.WriteLine("Negative"); else Console.WriteLine("Zero");
C. if (n > 0) { Console.WriteLine("Positive"); } else { if (n > 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } }
D. if (n > 0) { Console.WriteLine("Positive"); } else if (n <= 0) { Console.WriteLine("Zero"); } else { Console.WriteLine("Negative"); }
Solution
Step 1: Understand the logic needed
We need to check if n is greater than 0, less than 0, or exactly 0.
Step 2: Analyze each option
if (n > 0) { Console.WriteLine("Positive"); } else if (n < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } correctly uses if, else if, and else to cover all cases without overlap or missing conditions.
Final Answer:
if (n > 0) { Console.WriteLine("Positive"); } else if (n < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } -> Option A
Quick Check:
Proper if-else chain covers all cases = B [OK]
Hint: Use if-else if-else chain for multiple conditions [OK]
Common Mistakes:
Using separate ifs causing multiple prints
Wrong order of conditions
Incorrect else condition logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.