Detailed Explanation of If-Else Statements in C Language


In C language, the if-else statement is used to perform actions based on specific conditions. If the given condition is true, the operation specified in the if code block will be executed.

There are several variants of if statements in C language:
If statement: Contains only the if part; if the condition is true, the operations in the if code block are executed.
If-else statement: Contains both if and else parts; if the condition is true, the operations in the if code block are executed; otherwise, the operations in the else code block are executed.
If else-if statement: Contains multiple else-if parts to check multiple conditions in sequence and perform corresponding operations based on the condition results.
Nested if statement: Contains another if statement inside the if or else code block to perform different operations in more complex conditions.

👇 Click to receive 👇
👉 C Language Knowledge Resource Collection

The if statement is used to check a given condition and perform corresponding operations based on the correctness of the condition. It is mainly used in scenarios where different operations need to be performed based on different conditions. The syntax of the if statement is as follows:
if(expression){  // Code to execute}

Let’s look at a simple example of an if statement in C language:
#include <stdio.h>
int main() {   int number = 0;   printf("Enter a number: ");   scanf("%d", &number);
   if (number % 2 == 0) {       printf("%d is an even number", number);  }
   return 0;}

Output:
Enter a number: 44 is an even number

In this example, the program first asks the user to input a number. Then, it uses the if statement to check whether the number is even (by evaluating number % 2 == 0). If the condition is true, that is, the number is even, the program will print the corresponding message "X is an even number", where X is the number entered by the user. If the condition is false, the operations in the if code block will be skipped, and the program will continue executing the subsequent code.

The if-else statement is used to perform two operations on a single condition. The if-else statement is an extension of the if statement, allowing us to perform two different operations based on whether the condition is true or false. It is important to note that the if and else code blocks cannot be executed simultaneously. It is always recommended to use the if-else statement, as it always triggers an else case when the if condition is not met.
The syntax of the if-else statement is as follows:
if (condition) {   // Code to execute if the condition is true} else {   // Code to execute if the condition is false}

Let’s look at a simple example using the if-else statement in C language to determine whether a number is even or odd:
#include<stdio.h>
int main() {   int number = 0;   printf("Enter a number: ");   scanf("%d", &number);
   if (number % 2 == 0) {       printf("%d is an even number", number);  } else {       printf("%d is an odd number", number);  }
   return 0;}

Output:
Enter a number: 44 is an even numberEnter a number: 55 is an odd number

In this example, the program asks the user to input a number. Then, it uses the if-else statement to check whether the number is even (by evaluating number % 2 == 0). If the condition is true, that is, the number is even, the program will print the corresponding message "X is an even number", where X is the number entered by the user. If the condition is false, the operations in the else code block will be executed, printing "X is an odd number".

The if else-if statement is an extension of the if-else statement. It is used in scenarios where multiple operations need to be performed based on different conditions. In the if else-if statement, if condition 1 is true, the statements defined in the if code block are executed; otherwise, if condition 2 is true, the statements defined in the else-if code block are executed; if all conditions are false, the statements defined in the else code block are executed. There can be multiple else-if code blocks. It is similar to the switch case statement; if there are no matching cases, the default case will be executed instead of the else code block.
if (condition1) {   // Code to execute if condition1 is true} else if (condition2) {   // Code to execute if condition2 is true} else if (condition3) {   // Code to execute if condition3 is true}...else {   // Code to execute if all conditions are false}

Here is an example of the if-else-if statement in C language:
#include<stdio.h>
int main() {   int number = 0;   printf("Enter a number: ");   scanf("%d", &number);
   if (number == 10) {       printf("Number is equal to 10");  } else if (number == 50) {       printf("Number is equal to 50");  } else if (number == 100) {       printf("Number is equal to 100");  } else {       printf("Number is not equal to 10, 50, or 100");  }
   return 0;}

Output:
Enter a number: 4Number is not equal to 10, 50, or 100Enter a number: 50Number is equal to 50

In this example, the program asks the user to input a number. Then, it uses a series of if-else-if statements to check whether the number is equal to specific values (such as 10, 50, 100) in sequence. If the number equals a specific value, the corresponding code block is executed, and the corresponding message is printed. If the number does not equal these specific values, the operations in the else code block are executed, printing "Number is not equal to 10, 50, or 100".

Programmer Technical Exchange Group

Scan the code to join the group and remember to note: city, nickname, and technical direction.




Leave a Comment