Image

C Language - Control Statements - Simple if

Simple if

Syntax: if(condition)
                  {
                  Statement  1
                  ....
		  ....
                  Statement  n
                  }
Flowchart

EX: Program to check whether the entered number is positive negative or zero
             #include<stdio.h>
             #include<conio.h>	
             Void main()
               {
                int n;
		clrscr();
		printf(“\n Enterd any number=”);
		scanf(“%d” ,&n);
		if(n>o)
			printf(“\n %d is positive”,n);
		if(n<0)
			printf(“\n %d is negative”,n);
		if(n==0)
			printf(“\n %d is zero”,n)
		getch(); 
               }

b. if Else
Syntax:	if(condition)
		{
 Statement 1;
}
else
{
Statement 2;
}
Flowchart

Ex: program to check whether the entered number is even or odd.
		#include<stdio.h>
		#include<conio.h>
		Void main()
		{
		 int n;
		clrscr();
		printf(“Enter any number \n”);
		scanf(“%d”,&n);
		if(n%2==0)
		{
		printf(“\n  %d is even”,n);
		}
		else
		{
		Printf(“\n  %d is odd”,n);
		}
		getch();
		}

c. Nested else if
   Syntax:      if(condition  1)
		{
			if(condition  2)
				Statement  1;
			else
				Statement  2;
		}
		else
		{
			if(condition  3)
				Statement  3;
			else
				statement  4;
		}
Flowchart

EX: Program to check biggest number among three number.
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
 	int  a,b,c, big;
	Printf(“Enter three numbers :”);
	Scanf(“%d%d%d”, &a,&b,&c);
	if(a>b)
	{
		if(a>c)
			big=a;
		else
			big=c;
	}
	else
	{
		if(b>c)
			big=b;
		else
			big=c;
	}
	Printf(“Biggest number is %d \n”,big);
	getch();
}

d. Ladder else if
Syntax: 	if (Test Condition -1)
			Statement -1;
		else if ( Test Condition -2)
			Statement -2;
		else if ( Test Condition -3)
			Statement -3;
			:
			:
		else if ( Test Condition –n)
			Statement –n;
		else
		  default statement;

Rest of the Program Statements-X;
Flowchart

EX: Program to find out the grade of a student when the marks of 4 subject are given.
#include<stdio.h>
#include<conio.h>
main()
  {
       float m1,m2,m3,m4,total,per;
       char grade;
       clrscr();
       printf("Enter marks of subject:");
       scanf("%f%f%f%f",&m1,&m2,&m3,&m4);
       total = m1+m2+m3+m4;
       per = total/4.0;
       if(per>85)
		grade='A';
       else if(per>=75)
		grade='B';
       else if(per>=55)
		grade='C';
       else if(per>=40)
		grade='D';
       else if(per>=40)
		grade='E';
       printf("percentage of %f, Grade is %c, \n",per,grade);
       getch();
  }