Dev C++ Switch Case Example
Posted By admin On 11.01.21จะเห็นว่าค่าตัวแปรที่ใช้ในการเงื่อนไข จะอยู่หลัง switch และเงื่อนไขจะอยู่หลัง case ส่วนสิ่งที่จะทำหากผลของเงื่อนไขเป็นจริง.
This article covers the switch
statement. For information on the switch
expression (introduced in C# 8.0), see the article on switch
expressions in the expressions and operators section.
The switch statement body consists of a series of case labels and an optional default label. No two constant expressions in case statements can evaluate to the same value. The default label can appear only once. The labeled statements are not syntactic requirements, but the switch statement is meaningless without them. C templates have often been compared to fancy macros and can often replace traditional macros for some applications, but they aren't macro preprocessors. Also, since there's no observable behavior from your switch statements, the examples you posted probably aren't great examples of what you might really be looking to achieve. You might want. Oct 27, 2008 Nvm, I found that switch case statements only handle int and char values. After doing that it uses the switch case statement, to check if the variable called name2 is 1 or 2 or 3 or 4. After that its adds 1 into counter and shows me the result. The problem currently is that it is not showing anything other then all values = 0. จากผังงานรูปที่ 12-2 แสดงการทำงานของคำสั่ง switch ที่ไม่มีคำสั่ง break ทำให้ทราบว่า การทำงานหลังจากตรวจสอบว่าเงื่อนไขที่ 1เป็นจริง จะทำกิจกรรมที่ 1. Jan 30, 2017 Give a basic example on how to use if-else and switch statement in C. How to use if else and switch in C CS.Math.Educator. Using Range in the Case Values of Switch Statement C.
switch
is a selection statement that chooses a single switch section to execute from a list of candidates based on a pattern match with the match expression.
The switch
statement is often used as an alternative to an if-else construct if a single expression is tested against three or more conditions. For example, the following switch
statement determines whether a variable of type Color
has one of three values:
It's equivalent to the following example that uses an if
-else
construct.
The match expression
The match expression provides the value to match against the patterns in case
labels. Its syntax is:
In C# 6 and earlier, the match expression must be an expression that returns a value of the following types:
C Code Switch Case
- a char.
- a string.
- a bool.
- an integral value, such as an
int
or along
. - an enum value.
Starting with C# 7.0, the match expression can be any non-null expression.
The switch section
A switch
statement includes one or more switch sections. Each switch section contains one or more case labels (either a case or default label) followed by one or more statements. The switch
statement may include at most one default label placed in any switch section. The following example shows a simple switch
statement that has three switch sections, each containing two statements. The second switch section contains the case 2:
and case 3:
labels.
A switch
statement can include any number of switch sections, and each section can have one or more case labels, as shown in the following example. However, no two case labels may contain the same expression.
Only one switch section in a switch statement executes. C# doesn't allow execution to continue from one switch section to the next. Because of this, the following code generates a compiler error, CS0163: 'Control cannot fall through from one case label (<case label>) to another.'
This requirement is usually met by explicitly exiting the switch section by using a break, goto, or return statement. However, the following code is also valid, because it ensures that program control can't fall through to the default
switch section.
Execution of the statement list in the switch section with a case label that matches the match expression begins with the first statement and proceeds through the statement list, typically until a jump statement, such as a break
, goto case
, goto label
, return
, or throw
, is reached. At that point, control is transferred outside the switch
statement or to another case label. A goto
statement, if it's used, must transfer control to a constant label. This restriction is necessary, since attempting to transfer control to a non-constant label can have undesirable side-effects, such transferring control to an unintended location in code or creating an endless loop.
Case labels
Each case label specifies a pattern to compare to the match expression (the caseSwitch
variable in the previous examples). If they match, control is transferred to the switch section that contains the first matching case label. If no case label pattern matches the match expression, control is transferred to the section with the default
case label, if there's one. If there's no default
case, no statements in any switch section are executed, and control is transferred outside the switch
statement.
For information on the switch
statement and pattern matching, see the Pattern matching with the switch
statement section.
Because C# 6 supports only the constant pattern and doesn't allow the repetition of constant values, case labels define mutually exclusive values, and only one pattern can match the match expression. As a result, the order in which case
statements appear is unimportant.
In C# 7.0, however, because other patterns are supported, case labels need not define mutually exclusive values, and multiple patterns can match the match expression. Because only the statements in the first switch section that contains the matching pattern are executed, the order in which case
statements appear is now important. If C# detects a switch section whose case statement or statements are equivalent to or are subsets of previous statements, it generates a compiler error, CS8120, 'The switch case has already been handled by a previous case.'
The following example illustrates a switch
statement that uses a variety of non-mutually exclusive patterns. If you move the case 0:
switch section so that it's no longer the first section in the switch
statement, C# generates a compiler error because an integer whose value is zero is a subset of all integers, which is the pattern defined by the case int val
statement.
You can correct this issue and eliminate the compiler warning in one of two ways:
By changing the order of the switch sections.
By using a when clause in the
case
label.
The default
case
The default
case specifies the switch section to execute if the match expression doesn't match any other case
label. If a default
case is not present and the match expression doesn't match any other case
label, program flow falls through the switch
statement.
The default
case can appear in any order in the switch
statement. Regardless of its order in the source code, it's always evaluated last, after all case
labels have been evaluated.
Pattern matching with the switch
statement
Each case
statement defines a pattern that, if it matches the match expression, causes its containing switch section to be executed. All versions of C# support the constant pattern. The remaining patterns are supported beginning with C# 7.0.
Constant pattern
The constant pattern tests whether the match expression equals a specified constant. Its syntax is:
where constant is the value to test for. constant can be any of the following constant expressions:
- A bool literal: either
true
orfalse
. - Any integral constant, such as an
int
, along
, or abyte
. - The name of a declared
const
variable. - An enumeration constant.
- A char literal.
- A string literal.
The constant expression is evaluated as follows:
If expr and constant are integral types, the C# equality operator determines whether the expression returns
true
(that is, whetherexpr constant
).Otherwise, the value of the expression is determined by a call to the static Object.Equals(expr, constant) method.
The following example uses the constant pattern to determine whether a particular date is a weekend, the first day of the work week, the last day of the work week, or the middle of the work week. It evaluates the DateTime.DayOfWeek property of the current day against the members of the DayOfWeek enumeration.
The following example uses the constant pattern to handle user input in a console application that simulates an automatic coffee machine.
Type pattern
The type pattern enables concise type evaluation and conversion. When used with the switch
statement to perform pattern matching, it tests whether an expression can be converted to a specified type and, if it can be, casts it to a variable of that type. Its syntax is:
where type is the name of the type to which the result of expr is to be converted, and varname is the object to which the result of expr is converted if the match succeeds. The compile-time type of expr may be a generic type parameter, starting with C# 7.1.
The case
expression is true
if any of the following is true:
expr is an instance of the same type as type.
expr is an instance of a type that derives from type. In other words, the result of expr can be upcast to an instance of type.
expr has a compile-time type that is a base class of type, and expr has a runtime type that is type or is derived from type. The compile-time type of a variable is the variable's type as defined in its type declaration. The runtime type of a variable is the type of the instance that is assigned to that variable.
expr is an instance of a type that implements the type interface.
If the case expression is true, varname is definitely assigned and has local scope within the switch section only.
Note that null
doesn't match a type. To match a null
, you use the following case
label:
The following example uses the type pattern to provide information about various kinds of collection types.
Instead of object
, you could make a generic method, using the type of the collection as the type parameter, as shown in the following code:
The generic version is different than the first sample in two ways. First, you can't use the null
case. You can't use any constant case because the compiler can't convert any arbitrary type T
to any type other than object
. What had been the default
case now tests for a non-null object
. That means the default
case tests only for null
.
Without pattern matching, this code might be written as follows. The use of type pattern matching produces more compact, readable code by eliminating the need to test whether the result of a conversion is a null
or to perform repeated casts.
The case
statement and the when
clause
Starting with C# 7.0, because case statements need not be mutually exclusive, you can add a when
clause to specify an additional condition that must be satisfied for the case statement to evaluate to true. The when
clause can be any expression that returns a Boolean value.
The following example defines a base Shape
class, a Rectangle
class that derives from Shape
, and a Square
class that derives from Rectangle
. It uses the when
clause to ensure that the ShowShapeInfo
treats a Rectangle
object that has been assigned equal lengths and widths as a Square
even if it hasn't been instantiated as a Square
object. The method doesn't attempt to display information either about an object that is null
or a shape whose area is zero.
C# Switch Case Default
Note that the when
clause in the example that attempts to test whether a Shape
object is null
doesn't execute. The correct type pattern to test for a null
is case null:
.
C# language specification
C# Switch Case String
For more information, see The switch statement in the C# Language Specification. The language specification is the definitive source for C# syntax and usage.