C If Statements
C programming provides conditional statements, which allows you to execute certain statements based on a condition. The most commonly used conditional statements in C programming are if
statements, if else
statements, if else if
statements, and nested if
statements.
If Statements
The if
statement is used to execute a statement or a block of statements if a condition is true. The syntax for the if
statement is as follows:
1if (condition) { 2 // statements to be executed if condition is true 3}
Here, the condition
is an expression that evaluates to a boolean value, either true or false. If the condition is true, the statements inside the if
statement are executed. If the condition is false, the statements inside the if
statement are skipped.
Example:
1#include <stdio.h> 2 3int main(void) { 4 int num; 5 printf("Enter a number: "); 6 scanf("%d", &num); 7 8 if (num > 0) { 9 printf("The number is positive.\n"); 10 } 11 return 0; 12}
In this example, the user is prompted to enter a number. The value of the num
variable is then checked against 0 using the if
statement. If num
is greater than 0, the message "The number is positive" is printed.
If else Statements
The if else
statement is used to execute a statement or a block of statements if a condition is true and another statement or block of statements if the condition is false. The syntax for the if else
statement is as follows:
1if (condition) { 2 // statements to be executed if condition is true 3} else { 4 // statements to be executed if condition is false 5}
Example:
1#include <stdio.h> 2 3int main(void) { 4 int num; 5 printf("Enter a number: "); 6 scanf("%d", &num); 7 8 if (num > 0) { 9 printf("The number is positive.\n"); 10 } else { 11 printf("The number is negative.\n"); 12 } 13 return 0; 14}
In this example, the user is prompted to enter a number. The value of the num
variable is then checked against 0 using the if else
statement. If num
is greater than 0, the message "The number is positive" is printed. If num
is not greater than 0, the message "The number is negative" is printed.
If else if Statements
The if else if
statement in C programming allows you to execute multiple conditions based on different inputs. It is an extension of the basic if
statement, but allows you to test for multiple conditions, rather than just one. The syntax for the if else if
statement is as follows:
1if (condition 1) { 2 // statements to be executed if condition 1 is true 3} else if (condition 2) { 4 // statements to be executed if condition 1 is false and condition 2 is true 5} else if (condition 3) { 6 // statements to be executed if condition 1 and 2 are false and condition 3 is true 7} ...
In the above syntax, condition 1
, condition 2
, and condition 3
are expressions that evaluate to either true
or false
. If condition 1
is true
, then the statements within the first block will be executed, and the program will skip all other conditions. If condition 1
is false
, the program will move on to check condition 2
, and so on. If none of the conditions are met, the program will move on to the next section of code.
Here's an example that demonstrates how the if else if
statement can be used:
1#include <stdio.h> 2 3int main(void) { 4 int grade; 5 printf("Enter your grade: "); 6 scanf("%d", &grade); 7 if (grade >= 90) { 8 printf("You received an A.\n"); 9 } else if (grade >= 80) { 10 printf("You received a B.\n"); 11 } else if (grade >= 70) { 12 printf("You received a C.\n"); 13 } else if (grade >= 60) { 14 printf("You received a D.\n"); 15 } else { 16 printf("You received an F.\n"); 17 } 18 return 0; 19}
In this example, the user is prompted to enter a grade. The value of the grade
variable is then checked against various ranges using the if else if
statement. If the grade is 90 or above, the message "You received an A" is printed. If the grade is between 80 and 89, the message "You received a B" is printed, and so on. If none of the conditions are met, the message "You received an F" is printed.
Nested If Statements
A if
statement can be nested within another if
statement. This means that an if
statement can be used inside the body of another if
statement. The syntax for a nested if
statement is as follows:
1if (condition 1) { 2 // statements to be executed if condition 1 is true 3 if (condition 2) { 4 // statements to be executed if condition 2 is true 5 } 6}
Example:
1#include <stdio.h> 2 3int main(void) { 4 int num1, num2; 5 printf("Enter two numbers: "); 6 scanf("%d %d", &num1, &num2); 7 8 if (num1 > num2) { 9 printf("The first number is greater than the second number.\n"); 10 if (num1 >= 100) { 11 printf("The first number is greater than or equal to 100.\n"); 12 } 13 } else { 14 printf("The second number is greater than or equal to the first number.\n"); 15 } 16 return 0; 17}
In this example, the user is prompted to enter two numbers. The value of the num1
and num2
variables are then checked against each other using the outer if
statement. If num1
is greater than num2
, the message "The first number is greater than the second number" is printed. Then, the inner if
statement checks if num1
is greater than or equal to 100. If num1
is greater than or equal to 100, the message "The first number is greater than or equal to 100" is printed. If num1
is not greater than num2
, the message "The second number is greater than or equal to the first number" is printed.