Switch / Enum Part 2 (Enum)
enum
in C++Permalink
RecallPermalink
- In an early discussion, we were trying to give driving direction, and there was a finite set of actions:
- turn right
- turn left
- continue straight
- arrive
switch
provided a convenient way to choose among specific, finite valuesif...else
also works, butswitch
is often more convenient for finite values
SwitchPermalink
int action = ...;
switch (action) {
case 0: //turn right
Serial.println("Turn right (you can turn on red)");
break;
case 1: //turn left
Serial.println("Turn left (wait for arrow)");
break;
case 2: //continue
Serial.println("Keep going straight");
break;
case 3: //arrived
Serial.println("You have arrived!");
break;
default:
Serial.println("Error!");
}
Another improvementPermalink
- This was cleaning and easier to read
- However, the actions (or states) are still a little confusing
- Ex: We need to remember that case 2 means continue straight
case 2: //continue
Serial.println("Keep going straight");
- These is another improvement we can make
enum
Permalink
enum
is a user-defined data type- This means we get to decide what its values will be
enum
that allows us to give labels to literal (constant) values- It can make our code much more self-explanatory and logical
enum
SyntaxPermalink
enum <<enum_name>> { <<value1>>, <<value2>>, ...}
enum
is the type keyword<<enum_Name>>
is a name we define (it will become likeint
orString
)<<value1>>
…<<value2>>
are the allowed value we will let our variable take- By default, C++ treats each of these values as starting at
0
and increasing by 1
RecallPermalink
int action = ...;
switch (action) {
case 0: //turn right
Serial.println("Turn right (you can turn on red)");
break;
case 1: //turn left
Serial.println("Turn left (wait for arrow)");
break;
case 2: //continue
Serial.println("Keep going straight");
break;
case 3: //arrived
Serial.println("You have arrived!");
break;
default:
Serial.println("Error!");
}
enum
ExamplePermalink
- There are four actions or states we need to represent
- Turn left, Turn right, Go straight, arrive
- Let’s define our
enum
and call itOperation
enum Operation {left, right, straight, arrive};
Operation
value ofleft
is equivalent to0
Operation
value ofright
is equivalent to1
Operation
value ofstraight
is equivalent to2
Operation
value ofarrive
is equivalent to3
enum
ExamplePermalink
- Now we can create a
Operation
variable
Operation action;
action
would be given a value from some function we write- You could also assign a literal value to
action
like this
Operation action = straight; //equivalent to int value of 2
- We can now use our
Operation
value to control theswitch
enum
ExamplePermalink
Operation action = ...;
switch (action) {
case right:
Serial.println("Turn right (you can turn on red)");
break;
case left:
Serial.println("Turn left (wait for arrow)");
break;
case straight:
Serial.println("Keep going straight");
break;
case arrive:
Serial.println("You have arrived!");
break;
default:
Serial.println("Error!");
}
Tip: Converting String
to enum
Permalink
- If you have an
int
stored as aString
but want to useenum
for your aswitch
statement, first covert to anint
withtoInt()
and then cast toenum
enum Operation {left, right, straight, arrive};
String opStr = "0"; //this often comes from an event handler
int opInt = opStr.toInt(); //int 0
Operation op = (Operation) opInt; //Operation LEFT