Switch case statement evaluates a given expression và based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used khổng lồ perform different actions based on different conditions(cases).

Bạn đang xem: Switch

Switch case statements follow a selection-control mechanism và allow a value to change control of execution.The switch statement is a multiway branch statement. It provides an easy way to lớn dispatch execution lớn different parts of code based on the value of the expression.

In C, the switch case statement is used for executing one condition from multiple conditions. It is similar to lớn an if-else-if ladder.


The switch statement consists of conditional-based cases và a default case.

Syntax of switch Statement in C

switch(expression)case value1: statement_1; break;case value2: statement_2; break;...case value_n: statement_n; break;default: default_statement;

How to use switch case Statement in C?

Before using the switch case in our program, we need to lớn know about some rules of the switch statement.


Rules of the switch case statement

Following are some of the rules that we need khổng lồ follow while using the switch statement:

In a switch statement, the “case value” must be of “char” & “int” type.There can be one or N number of cases.The values in the case must be unique.Each statement of the case can have a break statement. It is optional.The mặc định Statement is also optional.

Example


Step 1: The switch variable is evaluated.Step 2: The evaluated value is matched against all the present cases.Step 3A: If the matching case value is found, the associated code is executed.Step 3B: If the matching code is not found, then the default case is executed if present.Step 4A: If the break keywords is present in the case, then program control breaks out of the switch statement.Step 4B: If the break keywords is not present, then all the cases after the matching case are executed.Step 5: Statements after the switch statement are executed.

We can also understand the working of the switch statement in C using the flowchart.

Flowchart of Switch Statement


switch case in c

Flowchart of switch statement in C


Break in switch case

This keyword is used to lớn stop the execution inside a switch block. It helps to lớn terminate the switch block & break out of it. When a break statement is reached, the switch terminates, and the flow of control jumps khổng lồ the next line following the switch statement.

The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall through lớn subsequent cases until a break is reached.

Example of switch case without break


Default in switch case

The default keyword is used to lớn specify the set of statements to execute if there is no case match.

It is optional lớn use the default keyword in a switch case. Even if the switch case statement does not have a default statement,it would run without any problem.


Important Points About Switch Case Statements

1. Switch expression should result in a constant value

If the expression provided in the switch statement does not result in a constant value, it would not be valid.Some valid expressions for switch case will be,

// Constant expressions allowedswitch(1+2+23)switch(1*2+3%4)// Variable expression are allowed provided// they are assigned with fixed valuesswitch(a*b+c*d)switch(a+b+c)

2. Expression value should be only of int or char type.

The switch statement can only evaluate the integer or character value. So the switch expression should return the values of type int or char only.

3. Case Values must be Unique

In the C switch statement, duplicate case values are not allowed.

3. Nesting of switch Statements

Nesting of switch statements is allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes the program more complex & less readable.

Examples of switch Statement in C

Example 1: C Program khổng lồ print the day of the week using a switch case.


Switch Statement

Instead of writing many if..else statements, you can use the switch statement.

The switch statement selects one of many code blocks khổng lồ be executed:


switch(expression) case x: // code block break; case y: // code block break; default: // code block

This is how it works:

The switch expression is evaluated once
The value of the expression is compared with the values of each case
If there is a match, the associated block of code is executed
The break statement breaks out of the switch block and stops the execution The default statement is optional, and specifies some code khổng lồ run if there is no case match

The example below uses the weekday number khổng lồ calculate the weekday name:


int day = 4;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;// Outputs "Thursday" (day 4)

The break Keyword

When C reaches a break keyword, it breaks out of the switch block.

This will stop the execution of more code và case testing inside the block.

When a match is found, và the job is done, it"s time for a break. There is no need for more testing.


A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in the switch block.

Xem thêm: Mở & Kết Bài Ai Đã Đặt Tên Cho Dòng Sông Chọn Lọc Hay Nhất, Kết Bài Ai Đã Đặt Tên Cho Dòng Sông (41 Mẫu)


int day = 4;switch (day) case 6: printf("Today is Saturday"); break; case 7: printf("Today is Sunday"); break; default: printf("Looking forward lớn the Weekend"); // Outputs "Looking forward khổng lồ the Weekend"

Note: The mặc định keyword must be used as the last statement in the switch, & it does not need a break.


*

COLOR PICKER
*

*

×

Report Error

If you want to lớn report an error, or if you want khổng lồ make a suggestion, vị not hesitate lớn send us an e-mail:

help
Top Tutorials
HTML Tutorial
CSS Tutorial
Java
Script Tutorial
How khổng lồ Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorialj
Query Tutorial
Top References
HTML Reference
CSS Reference
Java
Script Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Referencej
Query Reference
Top Examples
HTML Examples
CSS Examples
Java
Script Examples
How to Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examplesj
Query Examples
Get Certified
HTML Certificate
CSS Certificate
Java
Script Certificate
Front over Certificate
SQL Certificate
Python Certificate
PHP Certificatej
Query Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate