This document contains code snippets and explanations related to the switch statement in C programming. It includes:
1) Examples of correct and incorrect switch statement syntax, explaining that only integer values can be used in cases, not expressions or floating values.
2) A program using nested switch statements to determine grace marks awarded to a student based on the number of subjects failed and student's class/division.
3) Explanations of concepts like only writing the integer value, not an expression, inside case statements.
1. Let us c(by yashwant kanetkar)
chapter 4 solution 5th
edition
Exercise:-
[A]
(a).
Heart
I thought one wears a suite
(b).
I am in case 3
(c).
Pure Simple Egghead!
(d).
Customers are dicey
Markets are pricey
Investers are moody
At least employees are good
(e).
Trapped
(f).
You entered a and b
2. (g).
Feeding fish
Weeding grass
mending roof
Just to servive
[B]
(a).
After switch semicolon ( ' ; ' ) is written which is wrong
incorrect statement:-switch(suite);
correct statement:-switch(suite)
(b).
In the case statement we never used the expression o
we only write the integer in the case statement.
(c).
We can not write floating value inside the switch and case statement we can only
write integer value.
(d).
We only write the integer value in the case statement not the expression of integer
constant or the expression of integer variable.
[D]
/*To find grace marks using switch*/
#include<stdio.h>
void main()
{
int sub,cls;
printf("Enter the no. of subjects in which a student has failedn");
scanf("%d",&sub);
3. printf("enter the class through which the student has passed(write 1 instead of
1st div.,write 2 instead of 2nd div.,write 3 instead of 3rd
div.n");
scanf("%d",&cls);
switch(cls)
{
case 1:
switch(sub)
{
case 1:
printf("grace marks=5n");
break;
case 2:
printf("grace marks=10n");
break;
case 3:
printf("grace marks=15n");
break;
default:
printf("No grace marksn");
}
break;
case 2:
switch(sub)
{
case 1:
printf("grace marks=4n");
break;
case 2:
printf("grace marks=8n");
break;