C Program to Calculate Grade of Student Using Switch Case In this C program, we will calculate the grade of a student based on the total marks obtained by the student in five subjects. Here we will use a switch case statement, however you can write same program using if-else ladder statement also.
Switch case statements are a substitute for long if statements that compare avariable to several 'integral' values ('integral' values are simply valuesthat can be expressed as an integer, such as the value of a char). The basic format for using switch case isoutlined below. The value of the variable given into switch is compared tothe value following each of the cases, and when one value matches the value ofthe variable, the computer continues executing the program from that point.
Below is a sample program, in which not all of the proper functions areactually declared, but which shows how one would use switch in a program. This program will compile, but cannot be run until the undefined functions aregiven bodies, but it serves as a model (albeit simple) for processing input.If you do not understand this then try mentally putting in if statements forthe case statements. Default simply skips out of the switch case constructionand allows the program to terminate naturally. If you do not like that, thenyou can make a loop around the whole thing to have it wait for valid input.You could easily make a few small functions if you wish to test the code. Quiz yourself Switch Case In Dev C++Previous: Functions How To Use Switch Statement In Dev C PdfNext: PointersBack to C Tutorial Index
Advertising | Privacy policy |Copyright © 2019 Cprogramming.com | Contact | About
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
Syntax
The syntax for a switch statement in C programming language is as follows −
The following rules apply to a switch statement −
![]()
Flow DiagramExample
When the above code is compiled and executed, it produces the following result −
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |