If else statement is very useful in any programming language. An if statement may also optionally contain a second statement, the “else" clause which is to be executed if the condition is not met.
Here is an example:
if(n > 0)
{
average = sum / n;
else {
printf("can't compute average\n");
average = 0;
}
The general syntax of an if statement in c:
if ( expression )
statement(s)
else
statement(s)
This feature is based in our daily life many times we think if we do or else not. eg: if i will reading carefully and properly i'll get good marks if i'll not reading i am not get good marks same procedure is if else.
> Here need to decide division of the student with the percentage
#include <stdio.h>
#include <conio.h>
main()
{
int percentage;
printf("Enter the percentage : ");
scanf("%d", &percentage);
if (percentage >= 70)
printf("Passed: Grade A \n");
else if ((percentage >=40) && (percentage <=70))
printf("Passed: Grade B \n");
else if ((percentage >=35) && (percentage <=40))
printf("Passed: Grade C \n");
else
printf("Failed\n");
}
#include <conio.h>
main()
{
int percentage;
printf("Enter the percentage : ");
scanf("%d", &percentage);
if (percentage >= 70)
printf("Passed: Grade A \n");
else if ((percentage >=40) && (percentage <=70))
printf("Passed: Grade B \n");
else if ((percentage >=35) && (percentage <=40))
printf("Passed: Grade C \n");
else
printf("Failed\n");
}
We have entered 60 and the result is Passed: Grade B.