C if...else Statement
In this tutorial, you will learn about if statement (including if...else and nested if..else) in C programming with the help of examples.
C if Statement
The syntax of the
if statement in C programming is:
if (test expression){// statements to be executed if the test expression is true}
How if statement works?
The
if statement evaluates the test expression inside the parenthesis ().- If the test expression is evaluated to true, statements inside the body of
ifare executed. - If the test expression is evaluated to false, statements inside the body of
ifare not executed.

To learn more about when test expression is evaluated to true (non-zero value) and false (0), check relational and logical operators.
Example 1: if statement
// Program to display a number if it is negative#include <stdio.h>int main(){int number;printf("Enter an integer: ");scanf("%d", &number);// true if number is less than 0if (number < 0){printf("You entered %d.\n", number);}printf("The if statement is easy.");return 0;}
Output 1
Enter an integer: -2 You entered -2. The if statement is easy.
When the user enters -2, the test expression
number<0 is evaluated to true. Hence, You entered -2 is displayed on the screen.
Comments
Post a Comment