

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Code:- #include #include int main() { char grade; clrscr(); printf("Enter your grade (A/B/C/D): "); scanf("%c", &grade); switch(grade) { case 'A': printf("Excellent!"); break; case 'B': printf("Good!"); break; case 'C': printf("Average!"); break; case 'D': printf("Needs Improvement!"); break; default: printf("Invalid Grade!"); } getch(); return 0; } Output:- Enter your grade (A/B/C/D): B Good!
In conclusion, the else-if statement is one of the most useful control structures in C programming. It provides a clean and efficient way to perform multi-way decision- making in a program. By checking conditions in sequence and executing only the matching block, it makes the program logical, simple, and easy to debug. The else-if ladder therefore plays a key role in writing programs that can choose between several options based on specific criteria.
Example of switch Statement: #include #include int main() { int day; clrscr(); printf("Enter day number (1–7): "); scanf("%d", &day); switch(day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; case 5: printf("Friday"); break; case 6: printf("Saturday"); break; case 7: printf("Sunday"); break; default: printf("Invalid day"); } getch(); return 0; } Output Example: Enter day number (1–7): 4 Thursday Explanation: The value of day is compared with each case label, and only the matching case executes. This structure is cleaner than multiple if-else statements when checking fixed values.
Output Example: Enter a year: 2024 2024 is a Leap Year Explanation: The program checks the leap year condition using nested if-else statements.