Search

5.3 — Switch statements

Although it is possible to chain many if-else statements together, this is difficult to read. Consider the following program:

Because doing if-else chains on a single variable testing for equality is so common, C++ provides an alternative conditional branching operator called a switch. Here is the same program as above in switch form:

The overall idea behind switch statements is simple: the switch expression is evaluated to produce a value, and each case label is tested against this value for equality. If a case label matches, the statements after the case label are executed. If no case label matches the switch expression, the statements after the default label are executed (if it exists).

Because of the way they are implemented, switch statements are typically more efficient than if-else chains.

Let’s examine each of these concepts in more detail.

Starting a switch

We start a switch statement by using the switch keyword, followed by the expression that we would like to evaluate. Typically this expression is just a single variable, but it can be something more complex like nX + 2 or nX - nY. The one restriction on this expression is that it must evaluate to an integral type (that is, char, short, int, long, long long, or enum). Floating point variables and other non-integral types may not be used here.

Following the switch expression, we declare a block. Inside the block, we use labels to define all of the values we want to test for equality. There are two kinds of labels.

Case labels

The first kind of label is the case label, which is declared using the case keyword and followed by a constant expression. A constant expression is one that evaluates to a constant value -- in other words, either a literal (such as 5), an enum (such as COLOR_RED), or a constant variable (such as x, when x has been defined as a const int).

The constant expression following the case label is tested for equality against the expression following the switch keyword. If they match, the code under the case label is executed.

It is worth noting that all case label expressions must evaluate to a unique value. That is, you can not do this:

It is possible to have multiple case labels refer to the same statements. The following function uses multiple cases to test if the ‘c’ parameter is an ASCII digit.

In the case where c is an ASCII digit, the first statement after the matching case statement is executed, which is “return true”.

The default label

The second kind of label is the default label (often called the “default case”), which is declared using the default keyword. The code under this label gets executed if none of the cases match the switch expression. The default label is optional, and there can only be one default label per switch statement. It is also typically declared as the last label in the switch block, though this is not strictly necessary.

In the isDigit() example above, if c is not an ASCII digit, the default case executes and returns false.

Switch execution and fall-through

One of the trickiest things about case statements is the way in which execution proceeds when a case is matched. When a case is matched (or the default is executed), execution begins at the first statement following that label and continues until one of the following termination conditions is true:
1) The end of the switch block is reached
2) A return statement occurs
3) A goto statement occurs
4) A break statement occurs
5) Something else interrupts the normal flow of the program (e.g. a call to exit(), an exception occurs, the universe implodes, etc…)

Note that if none of these termination conditions are met, cases will overflow into subsequent cases! Consider the following snippet:

This snippet prints the result:

2
3
4
5

This is probably not what we wanted! When execution flows from one case into another case, this is called fall-through. Fall-through is almost never desired by the programmer, so in the rare case where it is, it is common practice to leave a comment stating that the fall-through is intentional.

Break statements

A break statement (declared using the break keyword) tells the compiler that we are done with this switch (or while, do while, or for loop). After a break statement is encountered, execution continues with the statement after the end of the switch block.

Let’s look at our last example with break statements properly inserted:

Now, when case 2 matches, the integer 2 will be output, and the break statement will cause the switch to terminate. The other cases are skipped.

Warning: Forgetting the break statement at the end of the case statements is one of the most common C++ mistakes made!

Multiple statements inside a switch block

With if statements, you can only have a single statement after the if-condition, and that statement is considered to be implicitly inside a block:

However, with switch statements, you are allowed to have multiple statements after a case label, and they are not considered to be inside an implicit block.

In the above example, the 4 statements between the case label and default label are part of case 1, but not considered to be inside an implicit block.

If this seems a bit inconsistent, it is.

Variable declaration and initialization inside case statements

You can declare (but not initialize) variables inside the switch, both before and after the case labels:

Note that although variable y was defined in case 1, it was used in case 2 as well. Because the statements under each case are not inside an implicit block, that means all statements inside the switch are part of the same scope. Thus, a variable defined in one case can be used in another case, even if the case in which the variable is defined is never executed!

This may seem a bit counter-intuitive, so let’s examine why. When you define a local variable like “int y;”, the variable isn’t created at that point -- it’s actually created at the start of the block it’s declared in. However, it is not visible (in scope) until the point of declaration. The declaration statement doesn’t need to execute -- it just tells the compiler that the variable can be used past that point. So with that in mind, it’s a little less weird that a variable declared in one case statement can be used in another cases statement, even if the case statement that declares the variable is never executed.

However, initialization of variables directly underneath a case label is disallowed and will cause a compile error. This is because initializing a variable does require execution, and the case statement containing the initialization may not be executed!

If a case needs to define and/or initialize a new variable, best practice is to do so inside a block underneath the case statement:

Rule: If defining variables used in a case statement, do so in a block inside the case (or before the switch if appropriate)

Quiz

1) Write a function called calculate() that takes two integers and a char representing one of the following mathematical operations: +, -, *, /, or % (modulus). Use a switch statement to perform the appropriate mathematical operation on the integers, and return the result. If an invalid operator is passed into the function, the function should print an error. For the division operator, do an integer division.

2) Define an enum (or enum class, if using a C++11 capable compiler) named Animal that contains the following animals: pig, chicken, goat, cat, dog, ostrich. Write a function named getAnimalName() that takes an Animal parameter and uses a switch statement to return the name for that animal as a std::string. Write another function named printNumberOfLegs() that uses a switch statement to print the number of legs each animal walks on. Make sure both functions have a default case that prints an error message. Call printNumberOfLegs() from main() with a cat and a chicken. Your output should look like this:

A cat has 4 legs.
A chicken has 2 legs.

Quiz answers

1) Show Solution

2) Show Solution

5.4 -- Goto statements
Index
5.2 -- If statements

265 comments to 5.3 — Switch statements

  • Happilicious

    In section "Variable declaration and initialization inside case statements", stated
    However, initialization of variables directly underneath a case label is disallowed, and will cause a compile error. This is because initializing a variable does require execution, and the case statement containing the initialization may not be executed!

    is default value disallowed too? I've tried initialize variable underneath a default label and compiler error pops up.

    • Alex

      Initialization is the way we normally give "default values" to variables. Not sure whether you're referring to something else.

      • Happilicious

        Sorry, i meant default label. And I've cleared up my question, as i was confused with the sentence "initialization of variables directly underneath a case label is disallowed", if i stated anything wrong, please correct me.
        When i tried to run this code without default label,

        Seems like the code work if there's no default or case label underneath initialization of variable z even if variable z is initialized underneath case 3. I guess it's better to have default label in every switch statement than it being left out.

        • Alex

          Yes, you have it correct. It does look like compilers will let you initialize a variable directly if there's no subsequent case, since there's no way to skip over that case. However, I would not recommend doing this, as it's bad practice, and will lead to trouble if you add additional cases later. Generally speaking, if you need to define a new variable inside a case, use a block. It'll save you headaches.

  • Sembiyan

    Question :
    In the below program why there is no syntax error for case 1: which is written inside case 0's {}, something like NO matching switch found

    Information :
    Also when input1 is given as 1 it prints one.
    seems like C++ won't care about braces in case statements! (the assembly code generates a table)

    • Alex

      Switch/case statements are essentially a form of goto, and you can definitely goto the middle of a block, so although I was initially surprised that this worked, upon reflection I guess it makes sense.

      That said, you should avoid actually doing this on purpose, as it's pretty unclear what the behavior will actually be.

  • Milos

    Under STARTING SWITCH header u typed, the expression must evaluate to integral type, didn't u mean "integer", thank you for this tutorial u provide really awesome content!

  • Mar Kos

    Hi,
    thx for the great tutorial so far! 🙂

    I need some help with the following error (MS VS Community 2015):
    "(38): error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)"

    I even tried to switch my printNumberOfLegs() function with yours from the solution, but had the very same error.
    This error message doesn´t make any sense to me! :/

  • Avijit Pandey

    here is what i wrote

    Although i am using a C++11 capable compiler(GNU CC Compiler),but if i use enum class instead of enum, my compiler says that all the animals (PIG,CHICKEN,...) are not defined in this scope in getAnimalName() and printNumberOFLegs() functions.
    so why did you advice to use the enum class?
    or am i doing something wrong?

  • J

    ? Im doing a java course which by the way is a breeze thanks to what I learned on here so Thanks for the awesome tut. My ? is in java switch statement conditional expression can also evaluate to a string is this not true in C++ also ?

    • Alex

      In C++, a switch must be an integer or enumeration type (or a class that is convertable to such). So basically, no, you can't switch on a string in C++.

      • J

        Thanks for the reply maybe they will add that ability later it would be nice

        • Alex

          I think that's unlikely to ever happen in C++ (it would be quite inefficient).

          For now, you can use a sequence of if/else statements. If you want something maybe more efficient, you can set up a std::map to map strings to enums, and then switch on the enums.

  • M Hassan Tahir

    In the quiz exercise (2), i want to get the animal name input from the user but i cant find a way to do so.

    Error: no suitable conversion function from "std::string" to "Animal" exists.

    Need some help in this case plz. Thanks!

    • Alex

      Yes, the problem here is that x is a std::string, and you are passing x as an argument to function getAnimalName(), but getAnimalName() has a parameter of type Animal. The compiler doesn't know how to convert a string to an animal.

      So you'll need to do this conversion yourself. The easiest way to do this is to write a function to do the conversion:

      • sambhav beniwal

        how to take a input off data type 'Animal' directly ?
        example:

        here, since it is giving an error.is there a possble way to take input of animal directly from user ?

        • Alex

          Read the user's input as a std::string, then write a function to convert the std::string to an Animal.

          e.g.

      • sambhav beniwal

        [code]
            #include<iostream>
            #include<stdlib.h>
            using namespace std;
            enum Animal
            {
                pig,
                chicken,
                goat,
                cat,
                dog,
                ostrich
            };

            void getAnimalName(Animal animal)
            {
                switch (animal)
                {
                    case pig:
                    cout<<"pig";
                    break;
                    case chicken:
                    cout<<"chicken";
                    break;
                    case goat:
                    cout<<"goat";
                    break;
                    case cat:
                    cout<<"cat";
                    break;
                    case dog:
                    cout<<"dog";
                    break;
                    case ostrich:
                    cout<<"ostrich";
                    break;
                    default:
                    cout<<"invalid choice";
                    break;
                }
            }
            int printNumberOfLegs(Animal animal)
            {
                switch(animal)
                {
                    case pig:
                    return 4;
                    case chicken:
                    return 2;
                    case goat:
                    return 4;
                    case cat:
                    return 4;
                    case dog:
                    return 4;
                    case ostrich:
                    return 2;
                    default:
                    cout<<"\tinvalid choice\t";
                    return 0;
                }
            }
            int main()
            {
                system("cls");
                int x;
                x=printNumberOfLegs(pig);
                cout<<"A\t"<<(getAnimalName(pig))<<"\thas\t"<<x<<"\tlegs.";
            }

        [\code]
        here, cout<<"A\t"<<(getAnimalName(pig))<<"\thas\t"<<x<<"\tlegs.";
        why isn't cout possible this way ??

        • Alex

          Your getAnimalName() returns a void, and you can't send a void value to std::cout.

          If you make getAnimalName() return a std::string instead of void, then this should work.

  • Georges Theodosiou

    Mr Alex, good afternoon (I live in France),

    Please accept my many thanks for fixing. Now it functions perfect.

    With regards and friendship
    Georges Theodosiou
    The Straw Man

  • Georges Theodosiou

    Mr Alex, good morning,

    Please let me report you my problem. I use compiler coliru (http://coliru.stacked-crooked.com/) and output is in quotes:

    quote
    main.cpp: In function 'int main()':

    main.cpp:30:28: error: 'printColors' was not declared in this scope

         printColors(COLOR_GREEN);

                                ^
    quote

    With regards and friendship
    Georges Theodosiou
    The Straw Man

  • Matt

    I think I remember you teaching us that variables declared inside of if/else blocks are not visible outside of the block. Does the same thing go for variables declared within a switch?

    If not, then what about declaring a variable within a block, within a case statement.... will that variable be visible outside of that block and outside of the switch statement?

    • Alex

      Variable declared in a block are only visible in the block in which they are declared, or in any sub-blocks.

      So for variables declared in a switch block or case block, those will only be visible in those blocks.

  • Kenneth

    Quiz 1 solution, compiles and seems to work just fine, except that the console prints "Invalid operator selected!" before "Results:".  Just curious why this happens.  Fantastic tutorial by the way!  Loving it!

    • Alex

      When I compiled this with Visual Studio 2015 and tried a couple of inputs (5 3 + and 5 3 -) it worked fine. I don't see anything wrong (though the break statements in DoMath() are extraneous and can be removed).

      • Kenneth

        I should have clarified that better, I was referring to if I put in something other than one of the four basic operators
        (5 3 g for example).

        • Alex

          Function calls have higher precedence than operator<<, so DoMath() is evaluated before the other statements are printed. One way to fix this would be to have DoMath() return the result as a std::string instead of printing the value itself.

  • Omar Farrag

  • Hugh O'Brien

    For some reason this won't compile. Its telling me there's an unexpected end of file when searching for precompiled header?

    Love the tutorials by the way 🙂

    • Alex

      Compiles fine for me. Sounds like maybe your stdafx.h is corrupt somehow. Try recreating your project.

      • Hugh O'Brien

        Ya thanks, I figured it out. It was just that I was using an old file the I'd previously used and in microsoft visual there was a two tabs (one being source tab). When I recreated the code in a new file there was no source tab and it all worked fine. Unfortunately I don't yet understand the UI of microsoft visual because I'm a newby. In time... Thanks for you reply 🙂

  • Hello Alex am learning a lot with your site  but please could you show an example with the example you have illustrated in your tutorials. How do i start such an example from the the start then i continue from where you started in your example thats dealing with colors.

  • subh

    I wanted to modify question-2 so that it will only print the animal entered by the user.  I tried to modify the main(), but I am not able to write proper switch() or if else statement. Can you help?

    int main()
    {
    std::string x;
    cout<<"enter the name of an animal\n";
    cin>> x;
        if ( x == 'cat')
         {
        printNumberofLegs(Animal::cat);
         }
        else if (x == 'chicken')
         {
        printNumberofLegs(Animal::chicken);
         }
        return 0;
    }

    • Alex

      Your if statement is fine, but "cat" and "chicken" need to be in double quotes. Double quotes are for strings, single quotes for single chars.

  • Nyap

    Whats a jump table

    • Nyap

      also, I don't get how this works:
      [quote]Note that although variable y was defined in case 1, it was used in case 2 as well. All cases are considered part of the same scope, so a declaration in one case can be used in subsequent cases.[/quote]
      But doesn't the declaration statement have to executed for the variable to be accessable?

      • Alex

        I just answered this in the above comment. Seems to be a popular question this week. 🙂 When you define a variable like “int y;”, the variable isn’t created at that point -- it’s actually created at the start of the block it’s in. However, it is not visible (in scope) until the declaration. So with that in mind, it’s a little less weird that a variable defined in one switch case can still be used in later switch cases, even if the upper switch case doesn’t execute.

        I'll add a little more about this to the lesson itself.

    • Alex

      Abstractly, a jump table is a table (array) of instructions that "jump" the point of execution to a different place in the code (using some other value to select which branch to take).

      In C++, a switch statement acts as a form of jump table itself. Your switch provides the value to jump to, and the point of execution jumps directly to the case statement that matches. This is often more efficient than using a series of if/else statement.

      An array of function pointers also acts like a jump table.

  • J3ANP3T3R

    "All cases are considered part of the same scope, so a declaration in one case can be used in subsequent cases"

    sometimes i find that C++ does not make any sense. given that case will execute the next statement(s) if case evaluated to true until it encounters break, goto etc. that said "int y;" should not have executed and y should not have been declared.

    QUESTION : if the variable is declared inside a block will it still be visible in the rest of the cases ?

    • Nyap

      >QUESTION : if the variable is declared inside a block will it still be visible in the rest of the cases ?
      no unless you use keyword "static" - the variable will be destroyed at the end of the block

      >sometimes i find that C++ does not make any sense. given that case will execute the next statement(s) if case evaluated to true until it encounters break, goto etc. that said "int y;" should not have executed and y should not have been declared.
      I'm confused about that too :c

      • Nyap

        I've been chatting a bit on forums, and got this:

        So duration is what really matters. When the compiler compiles this:

        int y is created on line 2 and destroyed when the blocks ends. It can be used anywhere in that block. Execution path doesn't matter

        • J3ANP3T3R

          oh i see... so it gathers all the variable declarations within the entire switch statement and sort of creates the variable at the start of the block ?

          • Nyap

            @J3ANP3T3R actually I made a mistake

            it can only be used anywhere under the declaration, however this is just a limitation set by the compiler. If it weren't for scope, then you could just put it anywhere, and it would still compile and run perfectly. At least thats what I have gathered

            Forum post is here: https://gbatemp.net/threads/c-switch-statements-variable-scope-question.427258/#post-6364165

        • Alex

          Yup!

          Although in the above example, case 3 is not allowed, because initialization DOES require execution, and if case 3 never executes, what value would variable z have if the default path were selected? It would be undefined.

      • Alex

        Where a variable is visible (scope) and where it is created/destroyed (duration) are two different things. The static keyword affects duration, not scope. I made an update to this lesson to try to make how this works a little clearer. Have a re-read and let me know if you're still confused.

    • Alex

      Yeah, switch statements are a bit weird. That's why I try not to use them for anything other than trivial lookups (where the body for each case is simply a return statement).

      That said, when you define a variable like "int y;", the variable isn't created at that point -- it's actually created at the start of the block it's in. However, it is not visible (in scope) until the declaration. So with that in mind, it's a little less weird that a variable defined in one switch case can still be used in later switch cases, even if the upper switch case doesn't execute.

      As for the block question, anything declared inside a block is not visible to anything outside the block.

  • Akhil

    Please help,i am unable to figure what's wrong with this code

    • Alex

      consoleapplication1.cpp(90): error C4700: uninitialized local variable 'cat' used

      It looks like you've declared two animals (cat and chicken) but never initialized them. Should be:

  • Edward Osei-Nyarko

    This code gives me errors when i compile;
    Error message:

    ||=== Build: Debug in Learncpp5.3 (compiler: GNU GCC Compiler) ===|
    C:\Users\Gloria.owner\Documents\Eddiemania\Learncpp5.3\main.cpp||In function 'double Calculate(char, double, double)':|
    C:\Users\Gloria.owner\Documents\Eddiemania\Learncpp5.3\main.cpp|15|error: case label ''-'' not within a switch statement|
    C:\Users\Gloria.owner\Documents\Eddiemania\Learncpp5.3\main.cpp|18|error: case label ''*'' not within a switch statement|
    C:\Users\Gloria.owner\Documents\Eddiemania\Learncpp5.3\main.cpp|21|error: case label ''/'' not within a switch statement|
    C:\Users\Gloria.owner\Documents\Eddiemania\Learncpp5.3\main.cpp|24|error: case label ''%'' not within a switch statement|
    C:\Users\Gloria.owner\Documents\Eddiemania\Learncpp5.3\main.cpp|25|error: invalid operands of types 'double' and 'double' to binary 'operator%'|
    C:\Users\Gloria.owner\Documents\Eddiemania\Learncpp5.3\main.cpp|27|error: case label not within a switch statement|
    ||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 5 second(s)) ===|

  • Patrick

    I wrote much more code than Alex provided in his answer. Definitely an opportunity for me to write code more efficiently. This is why I like quizzes so much.

    I changed the return type value for a division to double as part of the first quiz answer. If first integer is smaller than the second integer then without type casting I get 0 returned because precision is dropped.

    From:

    to:

  • Joshua Richards

    Very Small thing I noticed.

    In the very first example in this lesson you wrote an enum with a comma at the end of the last enumerator. I know this is not a big deal at all, and it would still compile, I just wanted to bear notice that I remembered that this is not proper conention when writing an enum. This is just proof of how great your tutorials are to remember such as minut detail thanks Alex!

  • Nhav

    At quiz 2
    How should you even call the printNumberOfLegs() function to get the default case?

    Also if the default case would be picked wouldn’t you get the result : printNumberOfLegs(): Unhandled enumerator legs.  ?:))

    Cuz you put cout << "legs" after default case.

    • Alex

      As written, there's no way to get the default case. The default case here is meant to ensure that if we ever add another enumerator and forget to update the function to account for the new enumerator, the output gives us some clue that we now have a case that isn't handled properly. Yes, the output is funny in that case, but at least we _know_ it's wrong, can look into it, and then fix the function.

  • Dyablo

    For some reason,

    gives me integer division, whereas,

    gives me floating point division. Can't figure why that is

    • Alex

      The return type of the function is set to an int, so it's implicitly converting the double result back to an integer before returning the value to the caller.

      • Shiva

        Alex, it seems like Mr. Dyablo copied your code, because the function you wrote too is set to return an int, not a double. (Todd-mode ON. B) )

        Variables declared inside a switch statement follow the normal scope and duration rules, don't they? So a variable declared inside a case statement is destroyed at the end of the switch. Also it can be used inside other cases. Similarly, a variable declared inside a block within a case statement is destroyed at the end of the block, and is not accessible inside other case statements. Correct?

  • saeid

    in quiz1 in your code :

    i think its better to use static_cast :

    what u think ?

  • Lokesh

    @Alex
    In the solution to Quiz Q.1, you have used

    in the default case instead of a break. Since, this could be a valid answer, isn't it error prone?
    Also, if I use break statement in the default case instead of a return statement, I get the following warning:
    In function 'int calculate(int, int, char)':
    test_45_switch_case.cpp:20:1: warning: control reaches end of non-void function [-Wreturn-type]
    }
    ^
    Why is it so? Isn't the control returning to the main() after the switch block is exited using break statement?

    • Alex

      > in the default case instead of a break. Since, this could be a valid answer, isn’t it error prone?

      Yes, absolutely. But we haven't really talked about error handling yet, so I'm keeping things simple for now.

      > Isn’t the control returning to the main() after the switch block is exited using break statement?

      If you replace the returns with a break, the break exits the switch block (not the function), and then the function continues to run to the end. Once the function ends, control returns to main. So in that case you've reached the end of the function without providing a return statement, so what value would the function return in that case? I'm surprised it only gave you a warning and not an error.

  • Lokesh

    @Alex
    You do not need to use

    explicitly in the first example code inside printColor(), since you have used using directive

  • Osman Zakir

    Why does Visual Studio keep putting a red line under operator<< after '"A "' here?

    I know it means there's an error, but I don't get why it's showing an error there.  Operator<< should be able to handle outputting strings.

    Edit: I overloaded operator<< to accept strings, but it ended up causing the program to crash when I run it. Please help.

  • Hridayesh Sharma

    hey alex in this code i have declared a variable in one of the case statemnts and it is running fine.

    • Alex

      Yep, I made a mistake here. You are allowed to declare variables inside the cases and assign values to them, but not initialize them. I've updated the lesson to reflect this.

      • Hridayesh Sharma

        but Alex what is the use of restricting the initialization of a variable when  i can use the variable by first declaring it and then assiging a value to it. Or what specifically is the purpose of restrictiong the initialization of variables in switch case statements.
        Thanks btw for your quick response.

        • Alex

          It has to do with the way switch statements are implemented (they are essentially goto statement in disguise, and goto statements are not allowed to jump past initialized values in C++).

          In many cases you can simply declare and assign a value to the variable rather than provide an initializer, but there are other cases where this is not possible, such as const values or references, which require initialization.

          But really, it's irrelevant, because declaring variables in a case outside of a block is something that should be avoided. Just put them in a block and be done with it.

  • Jazz

    Hi Alex,

    My question is about C++11 syntax usage in the second quiz. How can 'Animal parameter' gets into the function getAnimalName(???), in case if i'm using enum class, istead of plain enum? The same question related to switch(???) statement.

    • Alex

  • Jim

    Alex,
    I'm still having problems understanding your different uses of the word color.  In the first code above you have a function printColor it has (Colors color) after it as the argument.

    Can you please tell me if I have this right? There is one argument. Colors is the (Enum)type here and color is the variable used in the function printColor. Since there is never an assignment made to color (only comparisons) with the if/else statements below it the function just prints out: "unknown" (No Quotes).

    Now in the second code you have:
    switch (color) and none of the case statements are ever true so the default unknown is again printed out. Is this also right?  Thanks

    • Alex

      > There is one argument. Colors is the (Enum)type here and color is the variable used in the function printColor.

      True.

      > Since there is never an assignment made to color (only comparisons) with the if/else statements below it the function just prints out: "unknown" (No Quotes).

      False. Because this is a function parameter, the value of this variable will be set by the caller.

      For example, the caller may do something like this:

      When printColor is called, the color variable (of type Color) will be initialized with the enum value COLOR_BLACK.

      This isn't any different to how function parameters work with fundamental data types, like integers or doubles.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]