Simplied Form of Else If
Possible only we have only if block directly in each else block.
//Example of else if
void main()
{
int x;
printf(“enter any number 2,4,6 or 8 only:”);
scanf(“%d”,&x);
if(x==2)
{
printf(“TWO”);
}
else if(x==4)
{
printf(“FOUR”);
}
else if(x==6)
{
printf(“SIX”);
}
else if(x==8)
{
printf(“EIGHT”);
else
{
printf(“Invalid Input”);
}
printf(“\nRest of the program”);
}
Practic Questions)
1) write C program to determine the grade of the student based on the entered marks.
Criteria for Grading:
Marks obtained Grade
>=0 and <35 Fail
>=35 and <55 Grade C
>=55 and <75 Grade B
>=75 and <95 Grade A
>=95 and <=100 Grade A+
2. Write C program to determine big out of 3 numbers using else…if
>>>Switch…case
it is used when we want to check assignment of different values to a single variable and execute corresponding code.
sometimes it works as alternative to multiple if or else…if.
break transfers the program control to outside of the current block.
//Example of switch…case
void main()
{
int x;
printf(“Enter any number from 2,4,6,8:”);
scanf(“%d”,&x);
switch(x)
{
case 2: printf(“TWO”);
break;
case 4: printf(“FOUR”); break;
case 6: printf(“SIX”); break;
case 8: printf(“EIGHT”); break;
default: printf(“Invalid Input”);
}
printf(“\nRest of the program”);
}
Practice)
Write C Program to check whether user entered English Alphabet is vowel or consonant (using switch…case)