Simple If Statement:
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are eligible to vote!\n");
}
return 0;
}
Here, we have a variable age set to 20.
The if statement checks if the age is greater than or equal to 18.
If the condition is true, it prints "You are eligible to vote!"
If-Else Statement:
#include <stdio.h>
int main() {
int marks = 75;
if (marks >= 60) {
printf("You passed!\n");
} else {
printf("You failed.\n");
}
return 0;
}
The program checks if the marks are greater than or equal to 60.
If true, it prints "You passed!".
If false, it prints "You failed.".
Nested If-Else Statements:
#include <stdio.h>
int main() {
int num = 0;
if (num > 0) {
printf("Positive number.\n");
} else {
if (num < 0) {
printf("Negative number.\n");
} else {
printf("Zero.\n");
}
}
return 0;
}
The program checks if num is greater than 0 in the outer if.
If true, it prints "Positive number."
If false, it goes to the else part and checks if num is less than 0.
If true, it prints "Negative number."
If false, it prints "Zero."
If Else-If Ladder:
#include <stdio.h>
int main() {
int score = 75;
if (score >= 90) {
printf("Grade A\n");
} else if (score >= 80) {
printf("Grade B\n");
} else if (score >= 70) {
printf("Grade C\n");
} else {
printf("Grade F\n");
}
return 0;
}
The program checks the score in a series of conditions.
If the score is greater than or equal to 90, it prints "Grade A."
If not, it checks the next condition (80), and so on.
If none of the conditions are true, it prints "Grade F."
0 Comments
If you have any doubts, Please let me know