Index
Conditional Statement
A conditional statement in C is used to make decisions in your program. It lets you execute certain parts of your code only if a specific condition is true or false.
There are three main types of conditional statements in C:
- if statement
- if-else statement
- else if ladder
if Statement
The if
statement evaluates a condition, and if that condition is true, it executes the block of code inside the if
statement. If the condition is false, the code block is skipped.
Syntax:
if (condition) {
// Code to be executed if the condition is true
}
Example:
#include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
}
return 0;
}
Output
x is greater than 5
- Explanation:
- The condition
x > 5
is true becausex
is 10. So, the program will printx is greater than 5
.
- The condition
if-else Statement
else if
: Provides an additional condition if the previous if
(or else if
) was false..
Syntax:
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example:
#include <stdio.h>
int main() {
int x = 3;
if (x > 5) {
printf("x is greater than 5\n");
} else {
printf("x is less than or equal to 5\n");
}
return 0;
}
Output
x is less than or equal to 5
- Explanation:
- The condition
x > 5
is false becausex
is 3, so theelse
block will execute, printingx is less than or equal to 5
.
- The condition
else if Ladder
The else if
ladder allows you to check multiple conditions sequentially. It executes the first block of code where the condition is true and skips the rest. If none of the conditions are true, it executes the else
block.
Syntax:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if all conditions are false
}
Example:
#include <stdio.h>
int main() {
int x = 10;
if (x > 15) {
printf("x is greater than 15\n");
} else if (x == 10) {
printf("x is equal to 10\n");
} else {
printf("x is less than 10\n");
}
return 0;
}
Output
x is equal to 10
- Explanation:
- The condition
x > 15
is false, butx == 10
is true, so the program will printx is equal to 10
. It does not check further once a true condition is found.
- The condition
Here’s a C program that demonstrates all types of conditional statements (if
, if-else
, else if
, and the ternary operator) in a real-world example. Let’s imagine a simple restaurant order system that checks the user’s age and preferences to offer different menu options and discounts.
Restaurant Menu Code
In this program:
- We check the user’s age to apply a discount if they’re a child, adult, or senior.
- We take the user’s preference (vegetarian or non-vegetarian) to suggest a menu item.
- Finally, we use the ternary operator to add a message about the special of the day.
#include <stdio.h>
int main() {
int age;
char preference;
int discount;
char* menuItem;
printf("Welcome to the Restaurant!\n");
printf("Please enter your age: ");
scanf("%d", &age);
// Applying age-based discounts
if (age <= 12) {
discount = 50; // 50% discount for children
printf("You're eligible for a 50%% discount!\n");
} else if (age >= 60) {
discount = 30; // 30% discount for seniors
printf("You're eligible for a 30%% discount!\n");
} else {
discount = 10; // 10% discount for all other adults
printf("You're eligible for a 10%% discount!\n");
}
// Taking preference input
printf("Do you prefer (v)egetarian or (n)on-vegetarian? ");
scanf(" %c", &preference); // Added a space to ignore any leading newline character
// Suggesting a menu item based on preference
if (preference == 'v' || preference == 'V') {
menuItem = "Grilled Veggie Platter";
} else if (preference == 'n' || preference == 'N') {
menuItem = "Grilled Chicken with Herbs";
} else {
printf("Invalid choice! Defaulting to a vegetarian menu.\n");
menuItem = "Mixed Veg Salad";
}
// Displaying the final order details
printf("\nThank you for your order!\n");
printf("Your menu item: %s\n", menuItem);
printf("Discount applied: %d%%\n", discount);
// Ternary operator to display a message about the special of the day
char* specialMessage = (age <= 25) ? "You get a free dessert today!" : "Enjoy our special soup with your meal!";
printf("%s\n", specialMessage);
return 0;
}
Explanation
- If-Else Statements:
- We first use
if-else
to check the user’s age and apply a discount. - The first
if
checks if the age is less than or equal to 12, applying a 50% discount. - The
else if
checks if the age is 60 or older for a 30% discount. - If neither condition is met, the
else
applies a 10% discount.
- We first use
- Nested If Statements:
- After gathering the user’s age, we ask for their meal preference (
vegetarian
ornon-vegetarian
). - We use
if
statements to assign a menu item based on their choice, using||
(logical OR) to accept both uppercase and lowercase inputs forv
andn
. - If an invalid input is given, we default to a vegetarian option.
- After gathering the user’s age, we ask for their meal preference (
- Ternary Operator:
- The ternary operator is used to display a special message based on the user’s age.
- If the age is less than or equal to 25, the user receives a free dessert, otherwise a complimentary soup.
Output Example
For a sample user, the output might look like this:
Welcome to the Restaurant!
Please enter your age: 27
You're eligible for a 10% discount!
Do you prefer (v)egetarian or (n)on-vegetarian? n
Thank you for your order!
Your menu item: Grilled Chicken with Herbs
Discount applied: 10%
Enjoy our special soup with your meal!