Wrap syntax example in ```cpp (#33287)
This commit is contained in:
committed by
Niraj Nandish
parent
f0b29bcb28
commit
26bb9b6585
@ -5,6 +5,7 @@ title: Switch Statement
|
|||||||
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
|
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
|
||||||
|
|
||||||
Syntax:
|
Syntax:
|
||||||
|
```cpp
|
||||||
switch(expression) {
|
switch(expression) {
|
||||||
case constant-expression :
|
case constant-expression :
|
||||||
statement(s);
|
statement(s);
|
||||||
@ -17,7 +18,7 @@ switch(expression) {
|
|||||||
default : //Optional
|
default : //Optional
|
||||||
statement(s);
|
statement(s);
|
||||||
}
|
}
|
||||||
|
```
|
||||||
The following rules apply to a switch statement −
|
The following rules apply to a switch statement −
|
||||||
|
|
||||||
The expression used in a switch statement must have an integral or enumerated type(int,char,enum), or be of a class type in which the class has a single conversion function to an integral or enumerated type.
|
The expression used in a switch statement must have an integral or enumerated type(int,char,enum), or be of a class type in which the class has a single conversion function to an integral or enumerated type.
|
||||||
@ -72,10 +73,13 @@ int main () {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
Output:
|
Output:
|
||||||
You passed
|
You passed
|
||||||
Your grade is D
|
Your grade is D
|
||||||
|
```
|
||||||
|
|
||||||
###Sources
|
### Sources
|
||||||
https://www.tutorialspoint.com
|
https://www.tutorialspoint.com
|
||||||
|
Reference in New Issue
Block a user