Switch / Enum Part 2 (Enum)
enum in C++
Recall
- 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
switchprovided a convenient way to choose among specific, finite valuesif...elsealso works, butswitchis often more convenient for finite values
Switch
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 improvement
- 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
enumis a user-defined data type- This means we get to decide what its values will be
enumthat allows us to give labels to literal (constant) values- It can make our code much more self-explanatory and logical
enum Syntax
enum <<enum_name>> { <<value1>>, <<value2>>, ...}
enumis the type keyword<<enum_Name>>is a name we define (it will become likeintorString)<<value1>>…<<value2>>are the allowed value we will let our variable take- By default, C++ treats each of these values as starting at
0and increasing by 1
Recall
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 Example
- There are four actions or states we need to represent
- Turn left, Turn right, Go straight, arrive
- Let’s define our
enumand call itOperation
enum Operation {left, right, straight, arrive};
Operationvalue ofleftis equivalent to0Operationvalue ofrightis equivalent to1Operationvalue ofstraightis equivalent to2Operationvalue ofarriveis equivalent to3
enum Example
- Now we can create a
Operationvariable
Operation action;
actionwould be given a value from some function we write- You could also assign a literal value to
actionlike this
Operation action = straight; //equivalent to int value of 2
- We can now use our
Operationvalue to control theswitch
enum Example
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
- If you have an
intstored as aStringbut want to useenumfor your aswitchstatement, first covert to anintwithtoInt()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