I must also add:
Switch case statements can only be used to handle 1 type of variable, and crosscheck it's value to that of a non-variable. This is helpfule if that variable has pre-defined values assigned to it, but if there are multiple conditions which must be met, it is better to use an if,then,else. for example:
we want to print the time as am/pm, but we can only retrieve the raw time in military format (24 hours instead of 12.). Here is a little piece from my reminders program which can not possibly be written with a switch case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
cout<< "AM/PM: ";
while(ampm == "")
{
getline(cin, ampm);
}
if((ampm == "pm") || (ampm == "PM"))
{
if(hour != 12)
{
hour = (hour + 12);
while(hour > 24)
{
hour = (hour - 12);
}
}
}
if(((ampm == "am") || (ampm == "AM")) && (hour == 12))
{
hour = (hour + 12);
}
while(hour > 24)
{
hour = (hour - 12);
}
if((minute > 59) || (minute < 0))
{
minute = 0;
}
|
this was used to convert regular time into military time (which is returned when you get the system time) when the user inputs a reminder's time. We have to take the time he/she wants us to remind them, and convert it to military time.
there is more than 1 condition which (has to/can be) met before something is true, or false. If i type in am, and the hour is not 12, it is less than 24. but if i type in am, and the hour is exactly 12, then the hour is now 24. A switch case statement cannot be used here because: 1. some of the data is not an integer and 2. we HAVE to have 'and' and 'or' statements to allow for an accurate parse.
the "possibility of a false branch prediction" can be fixed with a little bit of modifications (trust me, when ur program screws up for no reason, you'll know what it is now) to the if/then statements.
This algorithm has worked perfectly for it's purpose, and my "Reminder" program has always reminded me at the times i set it (no later/earlier). So, yes, "possibility of a false branch prediction" is something to take into account, but keep in mind that sometimes, it is just better to use an if,then,else.