Switch In C Language

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

In C programming, the "switch" statement is used to perform different actions based on the value of a single expression. The syntax for a "switch" statement is as follows:

Copy code

switch (expression) {    case value1:        // code to be executed if expression == value1        break;    case value2:        // code to be executed if expression == value2        break;    ...    default:        // code to be executed if expression does not match any of the values }

The "expression" is the value that is being tested, and "value1", "value2", etc. are the different possible values that the expression can take. Each case is followed by a colon and the code to be executed if the expression matches that case's value. The break statement is used to exit the switch statement and continue with the next statement after the switch.

Here is an example of a program that uses a switch statement to determine the day of the week based on a number:

Copy code

#include <stdio.h> int main() {    int day;    printf("Enter a 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 input");    }    return 0; }

In this example, the program prompts the user to enter a number between 1 and 7. The switch statement then checks the value of the "day" variable against the different case values. If the value of "day" matches the value of a case, the code associated with that case is executed and the break statement is encountered, which exits the switch statement. If the value of "day" does not match any of the case values, the code associated with the default case is executed.

The switch statement is more efficient than a series of if-else statements when testing a single variable against many possible values because it directly compares the variable with the case values rather than using a series of relational operators.

5. Flow Control

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?