In C programming, decision-making is essential. The if else
structure allows you to execute different blocks of code based on conditions. Let’s dive into its components with definitions, examples, and practical usage.
What Is if else
in C?
The if else
statement lets you perform an action when a condition is true and another action when it is false.
if
Statement
The if
statement checks a condition. If the condition evaluates to true, the code inside the if
block is executed.
Syntax:
if (condition) {
// code to execute if condition is true
}
Example:
#include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("The number is positive.\n");
}
return 0;
}
else
Statement
The else
statement provides an alternative block of code when the if
condition is false.
Syntax:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
Example:
#include <stdio.h>
int main() {
int number = -5;
if (number > 0) {
printf("The number is positive.\n");
} else {
printf("The number is not positive.\n");
}
return 0;
}
else if
Statement
The else if
statement allows you to test multiple conditions sequentially.
Syntax:
if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if all conditions are false
}
Example:
#include <stdio.h>
int main() {
int number = 0;
if (number > 0) {
printf("The number is positive.\n");
} else if (number == 0) {
printf("The number is zero.\n");
} else {
printf("The number is negative.\n");
}
return 0;
}
Short Hand if
(Ternary Operator)
The ternary operator is a short way of writing an if...else
statement. It uses the syntax:
condition ? expression1 : expression2;
Explanation:
- If the condition is true,
expression1
is executed. - If the condition is false,
expression2
is executed.
Example:
#include <stdio.h>
int main() {
int number = 10;
// Using ternary operator
number > 0 ? printf("Positive\n") : printf("Non-positive\n");
return 0;
}
Real-Life Examples
Let’s apply these concepts in more practical scenarios.
Example 1: Grading System
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 75) {
printf("Grade: B\n");
} else if (marks >= 50) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
return 0;
}
Example 2: Even or Odd Check
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
Example 3: Login System
#include <stdio.h>
#include <string.h>
int main() {
char username[20];
char password[20];
printf("Enter username: ");
scanf("%s", username);
printf("Enter password: ");
scanf("%s", password);
if (strcmp(username, "admin") == 0 && strcmp(password, "1234") == 0) {
printf("Login successful!\n");
} else {
printf("Invalid username or password.\n");
}
return 0;
}
Why Use if...else
?
The if...else
structure is essential for:
- Making decisions in your program.
- Controlling program flow based on conditions.
- Enhancing program functionality by responding differently to various inputs.
Conclusion
The if...else
statement is a fundamental tool for decision-making in C. Whether you use the standard form, else if
, or the shorthand ternary operator, understanding these concepts will greatly enhance your programming skills. For more detailed tutorials and examples, visit Master Coding Science at mastercodingscience.com.