If Statement in C Language

If Statement in C is used to execute an instruction or sequence/block of instructions only if a condition is fulfilled. In if statements,

In if statements, an expression is evaluated first and then, depending on whether the value of the expression (relation or condition) is “true” or “false”, it transfers the control to a particular statement or a group of statements.

Syntax for If Statement

It is used to execute an instruction or block of instructions only if a condition is fulfilled.

If statement in C Language
If statement in C

The syntax is as follows:

if (condition) 
statement;

where condition is the expression that is to be evaluated. If this condition is true,

If this condition is true, the statement is executed. If it is false, statement is ignored (not executed) and the program continues on the next instruction after the conditional statement.

If we want more than one statement to be executed, then we can specify a block of statements within the curly braces { }.

The syntax is as follows:

if (condition) 

block of statements; 
}

An Example Program on if statement in C

Write a program to calculate the net salary of an employee, if a tax of 10% is levied on his gross-salary if it exceeds Rs. 20,000/- per month.

Program using If statement

/*Program to calculate the net salary of an employee */

#include <stdio.h>

main( )

{

float gross_salary, net_salary;

printf(“Enter gross salary of an employee\n”);

scanf(“%f ”,&gross_salary );

if (gross_salary <20000)

net_salary= gross_salary;

if (gross_salary >= 20000)

net_salary = gross_salary- 0.1*gross_salary;

printf(“\nNet salary is Rs.%.2f\n”, net_salary);

}

OUTPUT

Enter gross salary of an employee
15000
Net salary is Rs.15000.00
Enter gross salary of any employee
25000
Net salary is Rs. 22500.00

Different forms of implementation if-statement are:

  1. Simple if statement
  2. If-else statement
  3. Nested if-else statement
  4. Else if statement

 

One thought on “If Statement in C Language

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: