MESSAGE
DATE | 2010-03-17 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop - Switches and Break
|
_______________________________________________________________________________________________________
Switch Statement:
In addition to the if/else construction, C++ has a switch statement which improves readablity of appropriate code. The basic format for switch looks like this:
switch(expression that returns an int){ case int1: statements; statements; statement; break; case int2: statements; statements; statement; break; case int3: statements; statements; statement; break; case int4: statements; statements; statement; break; default: statements; statements; statement; }
There is a couple of caveats with the switch syntax. First, it is really unlike any other C or C++ syntax. Be aware that the conditional statement when is in the switch() syntax must return some from of an integer, and on that note, recall that char are implicit integers. You can not use a string as a return type.
The break at the end of each case block is necessary to stop the switch, otherwise you code will check more than one case. You can have multiple cases share the same statement block by constructing your switch like this:
switch(expression that returns an int){ case int1: statements; statements; statement; break; case int2: case int3: case int4: statements; statements; statement; break; default: statements; statements; statement; }
With this structure, case int2, int3, and int4 all share the statement block listed on case int3.
--Next Functions -- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
? Copyright for the Digital Millennium
|
|