• Normally in c programming language statement are execute in a sequenced manner.
  • Sometime we need to execute some statement based on some specific condition.
  • Depending on the condition either we have to execute or skip some statement.
  • In such situation we need to take decision to transfer control flow on the require statement.
  • C-programming language provides the following decision making statement:

(1) If statement         (2) Switch statement       (3) Conditional statement           (4) Go to statement

  1. If statement:

  • When we want to execute some statement based on a given condition that time we can use if statement.
  • If statement is a two way decision making statement.
  • It provides two alternative paths to be followed according to the condition.
  • The following are different from of if statement.

(1) Simple if statement  (2) if …else statement   (3) else…if ladder   (4) Nested if

(1) Simple if statement:

  • It is a simple form of if statement in which some statement are either executed or skipped according to the given condition.
  • It has the following form:
  • It first checks the condition or expression given inside if.
  • If condition becomes true, control enters into if block and statements inside the block are executes.
  • If condition becomes false then statements inside if block are skipped and control transfers to the statement following by if statement.
  • When if statement has a single statement then braces are not required but it is necessary when if statement contain two or more statement

flow of if statementSyntax:

if (condition)
{
   Statement(s);
}

Example:

if (ctegory =="sport")
{
    mark = mark + bonus_mark;
}
  • It first checks the condition or expression given inside if.
  • If condition becomes true, control enters into if block and statements inside the block are executes.
  • If condition becomes false then statements inside if block are skipped and control transfers to the statement following by if statement.
  • When if statement has a single statement then braces are not required but it is necessary when if statement contain two or more statement
  • The following program demonstrates the use of if…else statement to find out maximum number.
// program to find out maximum number
#include "stdio.h";
#include "conio.h";

void main()
{
  int a,b;
  clrscr();
  printf("enter two numbers");
  scanf("\n %d \t %d",&a,&b);
  if (a>b)
    printf("A is maximum");
  else
    printf("B is maximum");
  getch();
}

Leave a Reply

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