Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Nesting Switch...Case and if/else

This topic is 6248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


switch...case is generally discouraged where an if...else statement can go instead because it jumps rather ungracefully around your code (like goto, but not as bad). The only thing to remember about using case statements is to terminate cases with 'break;'. That way you can do anything that you would usually do in a function in a case statement, and it will execute up to the next break command. You can use this for effect, such as:

switch( myInt )
case 5:
cout << "5...\n";
case 4:
cout << "4...\n";
case 3:
cout << "3...\n";
case 2:
cout << "2...\n";
case 1:
cout << "1...\n";
cout << "Liftoff!!!!\n";

but it could be quite bad to forget 'break' if:

switch( myChoice )
case 1:
cout << "You chose 1!";
case 2:
cout << "You chose 2!";
cout << "Please enter a choice!";

Happy coding,

EDIT: Forgive me if some of my syntax is up the spout, I haven't used switches for a couple of months!!!

Edited by - Obolus on August 11, 2001 2:47:51 PM

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!