12. The basics of if-else statement


The if-else statement is used to carry out a logical test and then take one of two possible actions, depending on the outcome of the test. Whether the outcome is true or false.
   The else potion of the if-else statement is optional. Thus in its simplest general form, the statement can be written as

                          if (expression) statement

When the if statement is true, then the else statement is not working. When the if statement is false then the else statement is working. We can easily use many if statement under one if statement. It is easy to use.  When you enter a expression under an if statement, the expression must be placed in parentheses, as shown. In this form, the statement will be executed only if the expression has a nonzero value  (i.e., if expression is true.). If the expression has a value of zero (the expression is false), then the statement will be ignored. 
  
For example: 


if(a==10)
printf("Yes")
else
printf("No")


Here if the 1st statement is true i mean if (a==10), then it will be print Yes. If  the if statement is false i mean if (a!=10) then it will be print No.


Here is a  complete C program for using if else.


#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int y;
printf("Enter the age : ");
scanf("%d",&y);
if(y>=145)
printf("Sorry he/she is death.");
else
if(y>=100)
printf("con......");
else
if(y>=80)
printf("Very old man");
else
if(y>=60)
printf("Old man");
else
if(y>40)
printf("Middle age man");
else
if(y>20)
printf("Young");
else
if(y>=15)
printf("Teens");
else
if(y>=8)
printf("Boy");
else
if(y>=5)
printf("Child");
else
if(y>=0)
printf("Baby");
else
if(y<0)
printf("Up coming");
getch();
}


(post by arnob)


No comments:

Post a Comment

free counters