Write a program to read the data and determine the following: (a) Total marks obtained by each student. (b) The highest marks in each subject and the Roll No. Of the student who secured it. (c) The student who obtained the highest total marks. C Program: Print Details of 50 students using Structure.
A switch statement allows a variable or value of an expression to be tested for equality against a list of possible case values and when match is found, the block of code associated with that case is executed.
Syntax of Switch Statement
switch(expression) {
case constant1 :
/* Code to be executed when value of expression equals constant1 */
statement;
break;
case constant2 :
/* Code to be executed when value of expression equals constant2 */
statement;
break;
case constant3 :
/* Code to be executed when value of expression equals constant3 */
statement;
break;
default : /* Optional */
statement;
}
case constant1 :
/* Code to be executed when value of expression equals constant1 */
statement;
break;
case constant2 :
/* Code to be executed when value of expression equals constant2 */
statement;
break;
case constant3 :
/* Code to be executed when value of expression equals constant3 */
statement;
break;
default : /* Optional */
statement;
}
Important Points about Switch Statement
- Switch case performs equality check of the value of expression/variable against the list of case values.
- The expression in switch case must evaluates to return an integer, character or enumerated type.
- You can use any number of case statements within a switch. The expression value is compared with the constant after case.
- The data type of the value of expression/variable must be same as the data type of case constants.
- The break statement is optional.The break statement at the end of each case cause switch statement to exit. If break statement is not used, all statements below that case statement are also executed until it found a break statement.
- The default code block gets executed when none of the case matches with expression. default case is optional and doesn't require a break statement.
C Program to print grade of a student using Switch case statement
Above program check whether a student passed or failed in examination using if statement.OutputAdvantage of Switch Statement
- The complexity of the program increases as the number of constants to compare increases. Switch statement provides a way to write easy to manage and readable code.
- Switch statement provides a default case handler when no case matches.
- We can nest switch statements like if else statements.
![Write A Program To Print Student Rank As Per The Grade Using AND Operator Write A Program To Print Student Rank As Per The Grade Using AND Operator](/uploads/1/2/6/5/126544622/637652697.jpg)
![C program to find grade of a student using function C program to find grade of a student using function](/uploads/1/2/6/5/126544622/549108683.png)
- Switch statements can only be used to perform equality comparison, we cannot perform conditional checks using relational operators like >, <, > etc.
C program to check whether a character is vowel or consonant using switch statement |
C program to print name of days in week using switch statement |
C program to check whether a number is odd or even using switch statement |
C program to find maximum of two numbers using switch statement |