English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about the if statement in C language programming with the help of examples (including if ... else and nested if..else).
The syntax of this statement in C language programming is:
if (test expression) { //The statements to be executed when the test expression is true }
The if statement evaluates the test expression inside the parentheses().
If the calculation result of the test expression is true, then the if will execute the statements inside the body.
If the calculation result of the test expression is false, the if will not execute the statements inside the body.
For more information on when to evaluate the test expression as true (non-zero value) and false (0), please checkrelationoperatorandLogical operators.
//The program displays a number if it is negative #include <stdio.h> int main() { int number; printf("Please enter an integer: "); scanf("%d", &number); //If the number is less than 0, it is true if (number < 0) { printf("You entered %d.\n", number); } printf("The if statement is very simple."); return 0; }
Output1
Enter an integer: -25 You entered -25. The if statement is very simple.
when the user enters-25When, the test expression number<0 will be evaluated as true. Therefore,You entered-25 will be displayed on the screen.
Output2
Enter an integer: 5 The if statement is very simple.
when the user enters5When, the calculation result of the test expression number<0 is false, and the if does not execute the statements inside the body
The if statement can have an optional else block. The syntax of if..else statement is:
if (test expression) { //The statements to be executed when the test expression is true } else { // If the test expression is false, the following statements will be executed }
If the calculation result of the test expression (test expression) is true,
The statements within if will be executed.
The statements in the else of the body will be skipped, that is, the statements in else will not be executed.
If the value of the test expression (test expression) is false,
The statements within else will be executed
The statements in the if body will be skipped, that is, they will not be executed.
// Check if an integer is odd or even #include <stdio.h> int main() { int number; printf("Please enter an integer: "); scanf("%d", &number); //If the remainder is 0, it is true if (number%2 == 0) { printf("%d is an even number.", number); } else { printf("%d is an odd number.", number); } return 0; }
Output result
Enter an integer: 23 23 it is an odd number.
when the user enters23when, (test expression) test expression number%2The evaluation result of ==0 is false. Therefore, the statements in the else body will be executed.
if...else statements execute two different pieces of code depending on whether the test expression (test expression) is true or false. It is suitable for situations where you must choose from more than two possibilities.
if...else if...else allows you to check multiple test expressions and execute different statements. It is suitable for situations where you need to choose from more than two possibilities.
if (test expression1) { // Statement } else if (test expression2) { // Statement } else if (test expression3) { // Statement } . . else { // Statement }
//The program uses =, > or < symbols to compare two integers #include <stdio.h> int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); //Check if two integers are equal if (number1 == number2) { printf("Result: %d = %d", number1, number2); } //Check number1Is greater than number2. else if (number1 > number2) { printf("Result: %d > %d", number1, number2); } //If both of the above test expressions are false, execute the following statements else { printf("Result: %d < %d", number1, number2); } return 0; }
Output result
Enter two integers: 12 23 Result: 12 < 23
Another if...else statement can be included in the body of another if...else statement.
The program below provides an example of using any if...else ladder involving two integers <, >, and =, but we will use nested if...else statements to solve this problem.
#include <stdio.h> int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); if (number1 >= number2) { if (number1 == number2) { printf("Result: %d = %d", number1, number2); } else { printf("Result: %d > %d", number1, number2); } } else { printf("Result: %d < %d", number1, number2); } return 0; }
If the body of an if...else statement contains only one statement, brackets {} can be omitted.
For example, the following code
if (a > b) { print("Hello"); } print("Hi");
Is Equivalent to
if (a > b) print("Hello"); print("Hi");