Jump to content

Can someone please provide me with an example of a switch case and what switch case does?

 

switch (day)
{
case 1 : cout << "Sunday";
break;
case 2 : cout << "Monday";
break;
case 3 : cout << "Tuesday";
break;
case 4 : cout << "Wednesday";
break;
case 5 : cout << "Thursday";
break;
case 6 : cout << "Friday";
break;
case 7 : cout << "Saturday";
break;
default : cout << "Not an allowable day number";
break;
}

 

Is this one?

"My Profile Picture Says Everything You Need To Know About Life"

Link to comment
https://linustechtips.com/topic/255270-switch-case/
Share on other sites

Link to post
Share on other sites

Basically if the numeric data (an integer) in the variable day is equal to 1, it is going to display sunday, if it is 2, it will display monday, and so on. If a number other than 1-7 is entered, it defaults the the default statement which in this case displays a message. The breaks make it so only of the cases is executed. You do not want to execute more than one case because that would ruin the point. Also, you can do switches with string variables too.

Link to comment
https://linustechtips.com/topic/255270-switch-case/#findComment-3496911
Share on other sites

Link to post
Share on other sites

Here's a tutorial page (switch is at the bottom) http://www.cplusplus.com/doc/tutorial/control/

And another one http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm

 

A switch statement is kind of like another way to write an if statement.

switch (day){    case 1:        cout << "Sunday";        break;    case 2:        cout << "Monday";        break;    // ...    default:        cout << "Not an allowable day number";        break;}// basically the same asif (day == 1) {    cout << "Sunday";}else if (day == 2) {    cout << "Monday";}// ...else { // the default switch case    cout << "Not an allowable day number";}

The compiler may or may not treat them the same. Switch will be just as fast or faster than if statements though (source) however that doesn't mean you can use switch statements in every case.

Link to comment
https://linustechtips.com/topic/255270-switch-case/#findComment-3497513
Share on other sites

Link to post
Share on other sites

Well I know you can do it in C# which is what I thought this was in but have not seen what a switch looks like in other languages. 

The cout << is what indicates that it's C++. The C++ switch statement uses the same syntax as C# and Java (and I'm sure other languages as well)

Link to comment
https://linustechtips.com/topic/255270-switch-case/#findComment-3497597
Share on other sites

Link to post
Share on other sites

The breaks make it so only of the cases is executed. You do not want to execute more than one case because that would ruin the point.

 

Fall through in case statements can actually be useful.

If you leave out a break then it will run the code in the following case statement, and continue until a break is found.

Link to comment
https://linustechtips.com/topic/255270-switch-case/#findComment-3506338
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×