IF statement in C

Spread the love

A simple IF Statement in C consists of a Test – Expression followed by one or group of Statements which is to be evaluated and display the output.

Simple if in C Statements are used to control the flow of program execution with some conditions.

If Statement in C Syntax 1:

if(Test - Expression / boolean expression / Condition)
{
Statement(S);
}
Statement (optional);

How does the IF Statement in C Language work?

Whenever the test expression / boolean expression / condition is true then the statements which are inside the if block is executed.

 When the condition evaluates to false then the statement outside the if block is executed i.e. optional statement.

Here the statement outside the if statement is optional. If we are not using the optional statement then, when the boolean expression is false then there is nothing to display the output.

So we are using the Optional statement in the Simple If statement in C.

Flow Diagram of Simple IF Statement in C:

if statement in C
if statement in C

Here are the examples on IF Statement in C.

Evaluating a variable with a number if in C:

#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
if(a<=11)
{
printf("A is less than or equal to 11 %d",a);
}
printf("value is greater than 11 \n Given value is %d",a);
getch();
clrscr();
}

The condition that is mentioned inside the “if” is true then, the statement that is present inside the if block is executed. Otherwise the statements outside the if block is executed.

In this example, we have given the condition as (a<=10) and given a=15, so the condition is false and the output statement which is outside the if block will be executed. 

Output:

Value is greater than 10
Given value is 15

Evaluating one variable with another variable:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
a=10,b=11;
if(a<b)
{
printf("B is greater than A  \n");
}
printf("A value = %d, B value =%d \t\t",a,b);
getch();
clrscr();
}

Output:

B is greater than A
A value =10, B value =11

Multiple IF in C:

Syntax:

if(condition)
{
Statement 1;
}
if(condition 1)
{
Statement 2;
}

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=5,b=10,c=15;
if(a<b)
{
printf("B is big and A is small \n");
}
if(b<c)
{
printf("C is big and B is small \n");
}
if(c>a)
{
printf("C is big and A is small \n");
}
getch();
clrscr();
}

Output:

B is big and A is small
C is big and B is small
C is big and A is small

Evaluating the values entered by the user:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter A value");
scanf("%d",&a);
printf("Enter b value");
scanf("%d",&b);
if(a<b)
{
printf("B is big and A is small \n");
}
printf("End of the Program");
getch();
clrscr();
}

Output:

Here the values should be submitted by the user at the program runtime. As, based on the condition (a<b) is true i.e. a=10, b=12 then the output will be

B is big and A is small
End of the Program

When the user enters a value =15 and B value =10 then the output will be different.

Output:

End of the Program

Syntax 2: Without Optional Statement:

if(Condition)
{
Statement which is displayed when the Conditions is True i.e. non zero;
}

Example of IF in C without Optional Statement:

#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
if(a<10)
{
printf("A is small");
}
getch();
clrscr();
}

When the IF statement of the given condition is true i.e. non-zero then the output will be

Output:

A is Small

If the given condition is false then the output will not appear because the optional statement is absent in C.

1 thought on “IF statement in C”

  1. Pingback: Best C programming books for beginners 2021 - IT Examples

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top