Write a C program to find the grace marks for a student using switch. The user should enter the class obtained by the student and the number of subjects he has failed in. Use the following logic:
– If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. Otherwise the grace is of 5 marks per subject.
– If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. Otherwise the grace is of 4 marks per subject.
– If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. Otherwise the grace is of 5 marks per subject.
#include<stdio.h>
int main()
{
char choice;
printf("Enter your choice\n");
scanf("%c", &choice);
switch(choice)
{
case 'a':
case 'A': printf("You typed A or a\n");
break;
case 'b':
case 'B': {
printf("You typed B or b\n");
break;
}
default: printf("Your selection is wrong!\n");
}
return 0;
}
Output 1 Enter your choice B You typed B or b
Output 2 Enter your choice b You typed B or b
Here we’ve not specified keyword break after case ‘a’ and case ‘b’, that means if user enters a or A, for both whatever is present in case ‘A’ gets executed and then control exits switch case when it encounters break statement. Similarly for ‘b’ and ‘B’.
Also note that curly braces are optional here. Infact its unnecessary to use curly braces in switch case statements.
#include<stdio.h>
int main()
{
int choice;
printf("Enter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("This is first statement\n");
case 2: printf("This is second statement\n");
default: printf("Your selection is wrong!\n");
}
return 0;
}
Output 1 Enter your choice 1 This is first statement This is second statement Your selection is wrong!
Output 2 Enter your choice 2 This is second statement Your selection is wrong!
Here we’re not using break after any case, so it’s printing statements present in all the cases.
#include<stdio.h>
int main()
{
int choice;
printf("Enter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("This is first statement\n");
break;
case 2: printf("This is second statement\n");
break;
default: printf("Your selection is wrong!\n");
}
return 0;
}
Output 1 Enter your choice 1 This is first statement
Output 2 Enter your choice 2 This is second statement
Output 3 Enter your choice a Your selection is wrong!
#include<stdio.h>
int main()
{
int choice;
printf("Enter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 2: printf("Apple\n");
break;
case 1: printf("IBM\n");
break;
default: printf("Your selection is wrong!\n");
}
return 0;
}
Output 1 Enter your choice 1 IBM
Here order doesn’t matter. As you can see from above example, case 2 comes before case 1.
#include<stdio.h>
int main()
{
int choice;
printf("Enter your choice\n");
scanf("%d", &choice);
switch(choice)
{
printf("List of companies\n");
case 2: printf("Apple\n");
break;
case 1: printf("IBM\n");
break;
default: printf("Your selection is wrong!\n");
}
return 0;
}
Output 1 Enter your choice 1 IBM
All the code must go inside any of the cases orelse those code will not have any effect. In above program the printf statement with “List of companies” doesn’t get printed and it doesn’t even throw any error.
Note: 1. default case is optional. 2. Value of cases in switch must always be unique. 3. switch can not be used with float, double. 4. Switch, case, default are all builtin keywords. 5. continue does not take the control back to the start of switch.