Types of Conditional Statements
C provides several ways to control the flow of a program based on conditions. These statements allow you to decide which block should run depending on whether a condition evaluates to true or false.- The if Statement: The if statement executes a block of code only if the condition is true.
- The if-else Statement: It provides two paths, one if a condition is true and another if it is false.
- Nested if-else Statement: One if-else block can contain another if-else.
- Else-if ladder: This has a chain of conditions where each is tested one after another.
- The switch statement: It allows multiple possible execution paths based on the value of a single variable.
- Conditional/Ternary Operator (?:): It is the shorthand for a simple if-else statement.
The if Statement
The if statement is the simplest conditional statement in C. It allows to execute a block of code only when a specified condition is true. If the condition is false, the block is skipped, and the program continues with the next statement.When to use:
The if statement is best used for simple checks where you only need to run code when a single condition is satisfied.
Syntax:
Here,if (condition) {
// code to execute if condition is true
}
- condition: Any expression that evaluates to true (non-zero) or false (zero).
- If the condition is true, the code inside the block runs.
- If the condition is false, the block is ignored.
// C program to show if statement
#include stdio.h
// Main function
int main()
{
int age = 20;
if (age = 18)
{
printf("You are eligible to vote.\n");
}
printf("Program finished.\n");
return 0;
}
Output:
Explanation:You are eligible to vote.
Program finished.
In this program, the message about voting eligibility is printed only because the condition age = 18 is true.
The if-else Statement
The if-else statement is used when you want the program to choose between two paths. If the condition is true, one block of code runs; if it is false, the other block runs.When to use:
Use if-else statement when there is a need to clearly define two alternatives, one if the condition is true and another if it is not.
Syntax:
Here,if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
- condition: Any logical expression that evaluates to true (non-zero) or false (zero).
- If the condition is true, the if block executes.
- If the condition is false, the else block executes.
// C program to show if-else statement
#include stdio.h
// Main function
int main()
{
int number = 7;
if (number % 2 == 0)
{
printf("The number is even.\n");
}
else
{
printf("The number is odd.\n");
}
return 0;
}
Output:
Explanation:The number is odd.
Here, the program checks if the number is even. Since the condition numer %2 == 0 is false, the else block runs instead.
The switch Statement
The switch statement is used when one option needs to be chosen from many based on the value of a single variable or expression.When to use:
When you need to compare the same variable against multiple values.
When there are many conditions, it can make the else-if ladder long.
When dealing with menus or a fixed set of choices.
Syntax:
Here,switch (expression) {
case value1:
// code to execute if expression == value1
break;
case value2:
// code to execute if expression == value2
break;
...
default:
// code to execute if no case matches
}
- expression: Must evaluate to an integer or character type.
- case value: Each case is compared with the expression. If they match, that block of code runs.
- break: Stops the switch block after a case executes, without it, execution “falls through” to the next case.
- default: Optional block that runs if none of the cases match.
// C program to show switch statement
#include stdio.h
// Main function
int main()
{
int day = 3;
switch(day)
{
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Output:
Wednesday
The Conditional/ Ternary Operator (?:)
The conditional operator is also known as ternary operator as it takes three operands, a codntion, a value if the condition is true, and a value if the condition is false. It is a shorthand way of writing simple if-else statements.When to use:
- It is used for simple decisions.
- When you want to make code concise.
- Avoid it for complex logics as it can reduce readability
Here,condition ? expression1 : expression2;
- condition: A logical expression that evaluates to true (non-zero) or false (zero).
- If the condition is true, expression1 is executed.
- If the condition is false, expression2 is executed.
// C program to show ternary operator
#include stdio.h
// Main function
int main()
{
int number = 10;
(number % 2 == 0) ? printf("Even\n") : printf("Odd\n");
return 0;
}
Output:
Even
Common Pitfalls in Conditional Statements
Here are some common small mistakes that can lead to unexpected results:- Using = instead of ==: One of the most common errors is using the assignment operator (=) instead of the equality operator (==). The equality operator is used for comparison.
- Missing break in switch: Forgetting the break in switch causes fall-through, where multiple cases are executed unintentionally.
- Deeply Nested if-else: Writing too many nested if-else blocks makes code hard to read and debug.
- Overusing Ternary Operator: The ternary operator (?:) is concise, but it can become confusing if nested or used for complex logic.
- Ignoring Boundary Cases: Sometimes the conditions don’t handle the boundary values like = vs . This can cause problems in grading systems, ranges, or limit checks.
Conclusion
In conclusion, conditional statements in C enable dynamic behaviour instead of fixed sequences by allowing programs to make decisions and execute code selectively based on given conditions. With if, if-else, else-if, and switch, different scenarios can be handled effectively.Frequently Asked Questions
1. What are conditional statements in C?2. What is the difference between 'if' and 'if-else'?They are used to make decisions and execute code based on conditions.
3. When should we use 'switch' instead of 'if-else'?if runs code only when a condition is true, while if-else provides two execution paths.
4. What is the ternary operator in C?Use switch when checking a single variable against multiple fixed values.
5. What happens if a break is missing in a switch?It is a shorthand for if-else written as condition ? value1 : value2.
Execution continues to the next case, causing unintended behaviour.
0 Comments