“if”, “else”, and “else if” are called decision control instructions. In programming it may happen sometime that it have to make decision. This decision types are true of false. For example if condition is true then flow of control goes one way if false then goes to another way. It is important that coder write the instructions in the way that it will works perfectly. Now using “if”, “else” we can execute single statement or a group of statements.
Let’s see the example
Now let’s see the output for above program
Let’s see the example
#include<stdio.h>
void main()
{
int a,b;
printf("Enter The Value of a\n");
scanf("%d",&a);
printf("Enter The Value of b\n");
scanf("%d",&b);
if(a>b)
{
printf("a is bigger than b");
}
else if(b>a)
{
printf("b is bigger than a");
}
else
{
printf("a and b is equal");
}
}
In above example we ask for two value form user and want to see which one is bigger. Here first “if” statement check whether value inside variable “a” is bigger than value inside “b” or not. If condition is true then it will print “a is bigger than b”. If condition become false then it goes to “else if” statement. Here it will check the condition whether value inside variable “a” is smaller than value inside “b” or not. If condition is true then it will print "b is bigger than a". If this condition is false too then control goes to last “else” statement. Now here coder pass the default statement, as here if a>b and b>a become false then the last condition left that is a=b. for this we can write “else if (a==b)” but as we know that it is the last condition left we pass it through default condition statement that is “else”. Note that we do not write any condition in “else” statement.void main()
{
int a,b;
printf("Enter The Value of a\n");
scanf("%d",&a);
printf("Enter The Value of b\n");
scanf("%d",&b);
if(a>b)
{
printf("a is bigger than b");
}
else if(b>a)
{
printf("b is bigger than a");
}
else
{
printf("a and b is equal");
}
}
Now let’s see the output for above program
Enter The Value of a
20
Enter The Value of b
56
b is bigger than a
20
Enter The Value of b
56
b is bigger than a


Thanks..
ReplyDelete