Quote:
Originally Posted by The Fifth Horseman
Do yourself a favor, and replace that ridiculous case with some else if 's.
|
I disagree, the use of a switch statement is precisely this. Its implementation is supposed to be optimized for comparing an expression to more than one constant expressions. On the other hand nested if/else if/else... may need many evaluations and processor instructions until they hit the target. Both switch and if have their uses.
Moreover, the reason why you have to put break statements, is because otherwise execution would fall through. BUT this can work in your favour, see this:
Code:
switch(cChar)
{
case '+':
cout << "The answer is: " << dfirstnumber << " + " << dsecondnumber << " = " << (dfirstnumber + dsecondnumber) << endl;
break;
case '-':
cout << "The answer is: " << dfirstnumber << " - " << dsecondnumber << " = " << (dfirstnumber - dsecondnumber) << endl;
break;
/*************/
case '*':
case 'x':
case 'X':
cout << "The answer is: " << dfirstnumber << " X " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
break;
/*************/
case '/':
if(dsecondnumber == 0)
cout << "that is an invalid operation" << endl;
else
cout << "The answer is: " << dfirstnumber << " / " << dsecondnumber << " = " << (dfirstnumber / dsecondnumber) << endl;
break;
default:
cout << "That is an invalid operation" << endl;
break;
}
This way you have one single piece of code for doing one single thing, as it should be. If you have to change that code in the future, and it's repeated in several places, you may introduce errors or discrepancies if you fail to update all of the instances in the same exact way.
By the way, you don't need to enclose each case group into brackets. (That defines a scope, which you can do anywhere, if you want variables declared within not to be accessible without.) switch() causes a jump to the matching case, and then execution continues, falling through any intermediate case and default labels, until a break (or return etc.) statement is found.
This is special about the switch statement. if (and for, while...) statements do need brackets, if you want more than one statement (line) into them. Above I disposed of the brackets in the if/else, but only because there's only one line for if and another for else. And again a pair of brackets defines a scope, so if you declare a variable within the brackets of an if (or for...) statement, it won't be defined once the program exits the closing bracket.
The C/C++ types are not the same across platforms, which is undesirable, but changing it now would break the standard. Depending on the platform, some types may have the same size, for example in 32-bit x86 int and long, or double and long double. Some non-standard extensions (Microsoft etc.) define custom types that are platform-invariant, such as those Horseman mentioned. But there are no subtypes, for example in 32-bit x86 __int32 is equivalent to both int and long; __int16 to short; and __int8 to char. The basic standard types are: char, short, int, long, double, long double. The integers (char, short, int and long) can be unsigned (which doubles their range in absolute value, since the bit usually reserved to keep the sign is saved for another significant binary digit).
I don't quite understand the final comments either, let alone the mention of Visual Basic. The name of a variable isn't restricted by its type--simply use self-documenting names that are immediate and intuitive to understand to you and anyone else. Some Visual Basic 6 and earlier coders did like to tag variable names with type prefixes, but that isn't required by the language, its purpose was only making the code more readable.