Switch case declaration are an alternate for long if statements that evaluate a variable to numerous "integral" values ("integral" values are just values that can be expressed as an integer, for example the value of a char). The essential format for using switch case is outlined underneath. The value of the variable given into switch is judge against to the value subsequent to all of the cases, as well as when one value matches the value of the variable, the PC carry on executing the program from that end.
The condition of a switch declaration is a value. The case declare that if it has the value of anything is subsequent to that case then do anything follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, typically enclosed by braces, which it is in. In this case, break avoids the program from falling through as well as executing the code in all the additional case statements. An significant thing to note down regarding the switch statement is that the case values might only be steady essential expressions. Sadly, it isn't official to make use of case like this:Code:switch ( <variable> ) { case this-value: Code to execute if <variable> == this-value break; case that-value: Code to execute if <variable> == that-value break; ... default: Code to execute if <variable> does not equal the value following any of the cases break; }
The default case is non-compulsory, but it is sensible to contain it as it controls some unpredicted cases. Switch statements serves as an easy method to write long if statements when the necessities are met. Frequently it can be used to process input from a user.Code:int a = 10; int b = 10; int c = 20; switch ( a ) { case b: // Code break; case c: // Code break; default: // Code break; }
Below is an example program, in which not all of the correct functions are in fact stated, but which explain how one would make use of switch in a program.
This program will amass, but cannot be run until the indeterminate functions are given bodies, but it serves as a model (albeit simple) for dispensation input. If you do not comprehend this then attempt mentally putting in if statements for the case statements. Default just skips out of the switch case construction as well as enables the program to end logically. If you do not like that, then you can create a loop about the whole thing to have it wait for valid input. You could simply create a few tiny functions if you want to check the code.Code:#include <iostream> using namespace std; void playgame(); void loadgame(); void playmultiplayer(); int main() { int input; cout<<"1. Play game\n"; cout<<"2. Load game\n"; cout<<"3. Play multiplayer\n"; cout<<"4. Exit\n"; cout<<"Selection: "; cin>> input; switch ( input ) { case 1: // Note the colon, not a semicolon playgame(); break; case 2: // Note the colon, not a semicolon loadgame(); break; case 3: // Note the colon, not a semicolon playmultiplayer(); break; case 4: // Note the colon, not a semicolon cout<<"Thank you for playing!\n"; break; default: // Note the colon, not a semicolon cout<<"Error, bad input, quitting\n"; break; } cin.get(); }



Reply With Quote
Bookmarks