Animated Gif of Switch case statement
C++ Switch Case
Switch case also know as Decision making statement because it's depend on given condition.
Every condition depends on values of variables and we use switch statement to match exact value to execute the specific part of code.
Syntax of Switch case statement is very simple
Switch( condition = 2 ){
we use different case under switch statement. if any case matches with switch provided value it'll execute only matched case and then break the flow of control toward normal execution. if none of the case matches then default execute. in default we don't need to use break statement.
Syntax of Switch case statement is very simple
Switch( condition = 2 ){
case 1:
//case 1 body
break;
case 2:
//case 2 body
break;
default:
// default message
}
//case 1 body
break;
case 2:
//case 2 body
break;
default:
// default message
}
Example
Switch
case
- #include <iostream>
- using namespace std;
- int main( ){
- int x = 4;
- switch( x ){
- case 1:
- cout << " value is : " << x << endl ;
- break ;
- case 2:
- cout << " value is : " << x << endl ;
- break ;
- case 3:
- cout << " value is : " << x << endl ;
- break ;
- case 4:
- cout << " value is : " << x << endl ;
- break ;
- default:
- cout << " value not exist " << endl ;
- }
- return 0;
- }
Comments
Post a Comment