C Programming Tutorial - ITDeveloper

ITDeveloper : C Programming

C Programming

Step by Step Tutorials


Share with a Friend

if Statement In C

An if statement consists of a Boolean expression followed by one or more statements.

Syntax

The syntax of an 'if' statement in C programming language is −

if(boolean_expression) {
   /* statement(s) will execute if the boolean expression is true */
}

If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the 'if' statement (after the closing curly brace) will be executed.

C programming language assumes any non-zero and non-null values as true and if it is either zero or null, then it is assumed as false value.

Flow Diagram

C if statement

Example

#include <stdio.h>

int main() {

int n1, n2, n3;

printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);

// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the largest
if (n2 >= n1 && n2 >= n3)
printf("%d is the largest number.", n2);

// if n3 is greater than both n1 and n2, n3 is the largest
if (n3 >= n1 && n3 >= n2)
printf("%d is the largest number.", n3);

return 0;
}

 

OUTPUT:

Enter three different numbers: 5 15 10
15 is the largest number.