A switch statement on enum value - C++ Statement

C++ examples for Statement:switch

Description

A switch statement on enum value

Demo Code

#include <iostream> 
  
using namespace std; 
  
int main(int argc, const char * argv[]) 
{ 
        enum class Colors 
        { //from  ww w  . ja  v  a  2 s. co  m
            Red, 
            Green, 
            Blue 
        }; 
        Colors color = Colors::Red; 
        switch (color) 
        { 
            case Colors::Red: 
                { 
                    cout << "The color is Red!"; 
                } 
                break; 
              
            case Colors::Green: 
                { 
                    cout << "The color is Green"; 
                } 
                break; 

            default: 
                { 
                    cout << "Unknown color!"; 
                } 
                break; 
        } 
      
        return 0; 
}

Result


Related Tutorials