If Statement in C

-Advertisement-

General Introduction:

If statement is a powerful control statement and is used to control the flow of execution of statements in a program based on some conditions or test-expression.
There are three forms of if statement in C.
They are:
a) Simple if statement
b) if-else statement
c) if else if statement

Also Read: Function In C Programming

a) Simple if Statement:

It is the most basic form of the if statement and used to select or not select a statement based on the result of the condition.

Syntax of simple if statement:

if(test-expression)
{
	statement;
}

Flowchart of simple if statement:

If Statement in C 1

Semantic of simple if statement:

When an if statement is encountered, it first checks the test expression (condition). If the test expression is true, the control executes the statement and then transfer out of the if statement. However, if the test expression is false, the control moves out of the if statement without selecting the statement.

Example of implementation of simple if statement in C:

/* C program to check if a number is positive */
#include<stdio.h>
#include<conio.h>
int main()
{
	int number;
	printf("Enter a number to know if it is positive: ");
	scanf("%d",&number);
	
	if(number>0)
	{
		printf("\n %d is positive.",number);
	}
	
	getch();
	return(0);
}

Output:

If Statement in C 2

b) if else statement:

It is an extension of simple if statement. It is used to select a statement a select among two alternative statements based on the result of the condition.

Syntax of if else statement:

if(test expression)
{
      statement 1;
}
else
{
     statement 2;
}

Flowchart of if else statement:

If Statement in C 3

Semantic of if else statement:

When a if else statement is encountered it first checks the test expression. If it is true, then statement 1 is executed otherwise statement 2 is executed, not both at the same time.
After selecting and executing the statement, control transfers out of the if else statement.

Also Read: Recursion In C Programming

Example of Implementation of if else statement in C:

/* C program to check if a number is positive or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
	int number;
	printf("Enter a number to know if it is positive or not: ");
	scanf("%d",&number);
	
	if(number>0)
	{
		printf("\n %d is positive.",number);
	}
	
	else
	{
		printf("\n %d is not positive",number);
	}
	
	getch();
	return(0);
}

Outputs:

If Statement in C 4
If part
If Statement in C 5
else part

c) if else if statement:

It is also called if else if ladder. It is used to select an action among more than two possible actions, depending upon the result of the action.

Syntax of if else if statement:

if(condition 1)
{
     statement 1;
}
else if(condition 2)
{
     statement 2;
}
.
.
.
else if(condition n-1)
{
     statement n-1;
}
else
{
    statement n;
}

Flowchart of if else if statement:

If Statement in C 6
Flowchart of if else if statement

Semantic of if else if statement:

When an if else if ladder is encountered, it first checks the condition 1. If it is true then it’s corresponding statement 1 will be executed and then control transfer out of the ladder. However, if condition 1 is false, the condition 2 will be checked. If condition 2 is true then the corresponding statement 2 will be executed and control transfers out of the ladder. If condition 2 is false, then condition 3 will be checked and so on. If all conditions are false then else part will be executed and control transfers out of the ladder.

Example of Implementation of if else if statement in C:

/* C program to check if a number is positive, negative or zero*/
#include<stdio.h>
#include<conio.h>
int main()
{
	int number;
	printf("Enter a number to know if it is positive or not: ");
	scanf("%d",&number);
	
	if(number>0)
	{
		printf("\n %d is positive.",number);
	}
	
	else if(number<0)
	{
		printf("\n %d is negative",number);
	}
	
	else
	{
		printf("\nThe number is zero");
	}
	getch();
	return(0);
}

Outputs:

If Statement in C 7
if part
If Statement in C 8
else if part
If Statement in C 9
else part

So these were the three types of if conditions in C. Do comment if you have any queries.

-Advertisement-

LEAVE A REPLY

Please enter your comment!
Please enter your name here