1. Is the "enum" keyword nothing more than a typedef of "const unsigned int"?
2. Is a switch statement nothing more than a cleaner-looking way to handle a longer list of if statements? What are the benefits of using an if statement instead of switch?
im not an experienced programmer to tell you a sure answer but ill try to help , this is my opinion :
>switch has some major advantages in some cases, like the implementation of duff's device which is a optimized version of a loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
send(to, from, count)
registershort *to, *from;
register count;
{
register n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
}
>as you might have guessed , the switch statement can jump at the middle of a loop then continuing the loop till n <= 0 which gives you some major advatages in some cases
>im not saying you cant implement duff's device with if statements but then it would lose its purpose because it would lose the optimization thing
>im sure enum has some special uses but as far as i know enumerations are just a list of constant expresions, i recommend using enum because of code readability and because it gives you a better structured code
Yeah, I'm getting the impression that Switch statements are just overall better than If statements and I've never really seen any clear argument as to which situations an If statement (on the flipside) would be better.
Also, the reason I'm asking about enums is because I was messing with typedefs myself. But when I think about it, enums would be closer to an array. I also got the impression that you're alluding to, that enums are just for readability and nothing more.
Enums are guaranteed to be unique and you can't mix different kinds of enums without an explicit cast. These are both handy properties. When you really get down to it, they are just a handy device to keep you from making mistakes in your code.
Conceptually, the big difference between a switch statement and an if/else if/else if sequence is that it lets you know that you're doing different things based on the value of a single expression. So when you see
1 2
if (x ==y) {
... lots of code
you don't know what the else might look like. Will it compare x to something? or y? Or neither? In contrast, when you see
1 2
switch(x) {
....
You immediately know a lot more about what it going to happen.
From a practical perspective, switches can be implemented very efficiently in machine code using a jump table or a binary search. In practice though, I suspect that most modern compilers can recognize an if/then/else sequence that can be converted to a jump table and will do it.
>if is better most of the times because you cannot have expressions in case labels, only constant values
>for example you have 3 arrays, you using a for loop to iterate through 100 numbers and you wanna asign numbers below 30 to first array, numbers between 30-59 in array two and rest of them in array 3,
>you can do this very easy with if statements, but try to do it with switch
>well as for enums i dont own the knowledge and experience to discuss about them , im just a highschool pleb, also a beginner in programming
// The following code is provided without any warranty, expressed or otherwise
enum eye_colors {BROWN, BLUE, GREEN};
void HowToFlirt(int eyeColor)
{
switch (eyeColor)
{
case BROWN:
std::cout << "Your eyes have such depth, I could lose myself in them" << std::endl;
break;
case BLUE:
std::cout << "When I look into your eyes, I see the clear serenity of a calm sea" << std::endl;
break;
case GREEN:
std::cout << "Looking into you eyes, I feel the warm nostalgia of gazing upon the green green grass of home" << std::endl;
break;
default:
std::cout << "Can I borrow you shades? Without them I fear that I shall be blinded by your radiance" << std::endl;
break;
}
}
A lot easier to understand than case 0:case 1: etc.
As for switch vs. ifelse ... well a switch statement can only evaluate based on a single variable. A sequence of ifelse conditions is more flexible: