Dev C++ Switch Example
- Dev C++ Switch Case
- Visual C++ Switch Case Example
- Dev C++ Switch Statement Examples
- Dev C++ Switch Case Example
The switch case statement is used when we have multiple options and we need to perform a different task for each option. C – Switch Case Statement. Before we see how a switch case statement works in a C program, let’s checkout the syntax of it. C: Switch Statements Sometimes when creating a C program, you run into a situation in which you want to compare one thing to a number of other things. Let's say, for example, that you took a character from the user and wanted to compare this to a number of characters to perform different actions. Jun 08, 2014 Searches related to c tutorial switch case how to use switch case in c switch case c beispiel c switch case string c switch case range c switch case enum switch case c. C Tutorial – Learn C Programming with examples By Chaitanya Singh Filed Under: Learn C C language is a direct descendant of C programming language with additional features such as type checking, object oriented programming, exception handling etc.
Language | ||||
Standard Library Headers | ||||
Freestanding and hosted implementations | ||||
Named requirements | ||||
Language support library | ||||
Concepts library(C++20) | ||||
Diagnostics library | ||||
Utilities library | ||||
Strings library | ||||
Containers library | ||||
Iterators library | ||||
Ranges library(C++20) | ||||
Algorithms library | ||||
Numerics library | ||||
Input/output library | ||||
Localizations library | ||||
Regular expressions library(C++11) | ||||
Atomic operations library(C++11) | ||||
Thread support library(C++11) | ||||
Filesystem library(C++17) | ||||
Technical Specifications |
Labels | ||||
label : statement | ||||
Expression statements | ||||
expression ; | ||||
Compound statements | ||||
{ statement... } | ||||
Selection statements | ||||
if | ||||
switch | ||||
Iteration statements | ||||
while | ||||
do-while | ||||
for | ||||
range for(C++11) | ||||
Jump statements | ||||
break | ||||
continue | ||||
return | ||||
goto | ||||
Declaration statements | ||||
declaration ; | ||||
Try blocks | ||||
try compound-statementhandler-sequence | ||||
Transactional memory | ||||
synchronized , atomic_commit , etc(TM TS) |
Transfers control to one of the several statements, depending on the value of a condition.
[edit]Syntax
attr(optional)switch ( condition) statement | (until C++17) |
attr(optional)switch ( init-statement(optional)condition) statement | (since C++17) |
attr(C++11) | - | any number of attributes |
condition | - | any expression of integral or enumeration type, or of a class type contextually implicitly convertible to an integral or enumeration type, or a declaration of a single non-array variable of such type with a brace-or-equals initializer. |
init-statement(C++17) | - | either
|
statement | - | any statement (typically a compound statement). case: and default: labels are permitted in statement and break; statement has special meaning. |
attr(optional)case constant_expression: statement | (1) |
attr(optional)default : statement | (2) |
constant_expression | - | a constant expression of the same type as the type of condition after conversions and integral promotions |
[edit]Explanation
The body of a switch statement may have an arbitrary number of case:
labels, as long as the values of all constant_expressions are unique (after conversions/promotions). At most one default:
label may be present (although nested switch statements may use their own default:
labels or have case:
labels whose constants are identical to the ones used in the enclosing switch)
If condition evaluates to the value that is equal to the value of one of constant_expressions, then control is transferred to the statement that is labeled with that constant_expression.
If condition evaluates to the value that doesn't match any of the case:
labels, and the default:
label is present, control is transferred to the statement labeled with the default:
label.
The break statement, when encountered in statement exits the switch statement:
Compilers may issue warnings on fallthrough (reaching the next case label without a break) unless the attribute If init-statement is used, the switch statement is equivalent to
Except that names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if condition is a declaration) are in the same scope, which is also the scope of statement. | (since C++17) |
Because transfer of control is not permitted to enter the scope of a variable, if a declaration statement is encountered inside the statement, it has to be scoped in its own compound statement:
[edit]Keywords
switch,case,default
[edit]Example
The following code shows several usage cases of the switch statement
Dev C++ Switch Case
Output:
[edit]See also
Visual C++ Switch Case Example
C documentation for switch |
Sometimes when creating a C++ program, you run into a situation in which you want to compare one thing to a number of other things. Let's say, for example, that you took a character from the user and wanted to compare this to a number of characters to perform different actions. For now, let's just say that you have three commands which operate from the keys: 'h', 'e', and 'q'.
We can use an if-statement to check equality to 'h'
, 'e'
, and 'q'
- remember that we use single quotes because we're dealing with chars, not strings. So let's just write the code in the way we're used to (and let's wrap a while loop around it so it keeps getting a character and acting upon what it was, because that makes our program slightly better and easier to test), using if-statements, like the following:
This program should be something you're comfortable with creating by this point, however your programmer instincts should also be kicking in and telling you that you shouldn't be repeating so much code here. One of the core principles of object orientated programming is DRY: Don't Repeat Yourself. In this program we're having to repeat a lot of code for the 'else if's including using ch
every time we're doing a comparison.
Dev C++ Switch Statement Examples
The solution to this 'problem' is what this tutorial is all about: switch statements. These are essentially just a really nice way to compare one expression to a bunch of different things. They are started via the switch
keyword, and from there comparisons are made using a case:
syntax. It isn't easily described in words, so take a look at the syntax below:
The different case
s essentially act as many if/else-ifs in a chain, and an 'else' type clause can be specified by using default
:
Dev C++ Switch Case Example
This type of functionality should be reasonably easy to understand with if-statements securely under your belt, and so if you're feeling brave - try porting the basic program we created at the start of this tutorial to use if-statements. If you're not quite that brave, the cleaner and neater switch
version of the code is below: