Hello and welcome.
Let's continue our discussion on data types and talk about a couple more.
In this lecture, we're going to talk about the enum data type and the char care data type.
So enemies items are a data type that allows a programmer to define a variable and specify the valid
values that could be stored into that variable.
So an enum is very powerful.
It's actually specifying a data type.
It is the keyword enum, but you can basically have your own data type that includes certain values
only.
So for example, you could create a variable named my color, and if it's a certain enum type, you
can set it so that it only includes or only can be assigned values of primary colors such as red, yellow
or blue, and then no other values can be assigned to it.
The compiler will give you an error.
And so it's sort of like a data type that you can specify that you can create, but so you're going
to have to define the data type and then you're going to have to create variables of that type.
So an enum is the keyword.
So the first thing you have to do if you want to use this enum data type is you have to define that
enum type and give it a name, just an identifier, just like a variable name.
And the way that you do it is you're going to use the enum keyword and then after the enum keyword you're
going to provide a name like an identifier.
And then after the identifier, you're going to put all of the the list of valid values inside curly
braces.
And you're basically saying that this enum can only be these values.
And then when you want to use the enum later, you declare you create a variable of that type enum.
So let's see this in action.
Here, I'm defining an enum type called primary color.
So I have the enum keyword and then I have my name of my enum, which is primary colour, and then I'm
defining all the value values for that type.
And you'll see that they're enclosed inside curly braces, the left curly brace and the right curly
brace.
In this case, we have a type of enum named Primary Color that can only hold values of red, yellow
and blue.
And it can't store anything else if you try to assign a value to primary color, or if you try to use
the primary color data type and you try to assign a variable value to something else, it won't work.
So this is just how we define it.
We first have to define the enum, we then have to use it again.
Definition is just saying this is what it is.
And so what we're seeing here is we have a primary color in white.
It can be as red, yellow and blue.
So variables that are declared to be primary color type can only be assigned the values red, yellow
and blue.
Inside the program, no other values will work.
So again, the first step defining it.
The second step is using it.
So after we've defined it, we can then declare a variable of this type primary color in the way that
we declare.
The variable now is we still use the enum keyword.
We then actually have the name of the enum that we specified when we defined it.
And then at the very end, we follow that with a variable list.
So here is an example is we have enum, we use the enum keyword, we have the name of the enum that
we want to use, which is the one we just created called Primary Color.
And then we just have names.
So here we have two enemies of type primary color.
My color in Greg's color.
So again, this is where we're using the enum.
We've already defined what the enum can be, just red, yellow or blue.
And now we're saying we have two variables named my color and gray color that can only contain one of
those three values, can't contain anything else.
So here we're declaring a variable, a primary color.
And again, this is defining two variables my color and Gray's color to be of type primary color.
Which is an enum type, the only permissible values that can be assigned to these variables.
These two variables are red, yellow and blue.
So we could say my color equals red.
And that's perfectly valid.
We could not say my color equals green.
That won't work because those values aren't valid for that enum type.
We couldn't say my color equals Jason because my color can only contain three values of the primary
color enum type.
So this is a very powerful data type because you're essentially defining your own data types that can
contain only certain values.
Another example here would be if we create an enum that is of type month and here we're saying that
month can only be equal to January, February, March, April, May, June, July, August.
teMber, October, November, December.
Again, this is the definition.
This is where we're saying what the valid values are.
If we were to go and use month, we would create a variable called Enum Month and then whatever name
you want to give it.
And then we can assign to that name any of these valid months, January through December.
And that is how you use an enum.
Now under the hood as far as how incomes are implemented in the C programming language, is there really
represented by numbers?
So the compiler actually treats enumeration identifiers as integer constants.
So the first name in the list that you define, if you were to try to print out the value, it'd be
a zero.
All this is saying again is how are they defined?
If you were to display the output, what are the actual values?
So what this means is if we have in a month, this month, we created a new variable of type enum month
and then we say this month equals to February.
This month is actually going to be equal to an integer.
But we can still compare it and use it to compare it against February.
But if you were to print out this month to the console, this month would actually be an integer value
corresponding to when it was defined in the list.
So in this case, February would be one.
Because this month enum the second one is in the list is February.
Right?
So if we go back, if you remember when we defined it, the second one is February.
And you should notice that it's one even though it's the second one.
Because what that means is the list starts is zero.
So January would be zero, February would be one, March would be two and so forth.
And this is important because you can then compare it against as well as comparing against actual values
like February.
Now, if you want to have a specific integer value associated with the enumeration identifier, you
can explicitly assign something using the assignment operator.
When you actually define the type.
So for example, here, if we define a new enum of type direction and we're saying that the valid values
for direction are up, down, left and right, you'll see that the third value left actually has an
equals that saying that left is going to be equal to ten.
So in this case, our enum type direction is defined with the values up, down, left and right.
Those are the only valid values that you can assign.
Variables of this type up is going to be equal to zero because it's the first element in the list definition.
One is going to be equal to down because it's the second element.
Again, it starts at zero.
Ten is going to be equal to left.
It's not going to be equal to two because we explicitly assign it to equal to ten.
So left is going to be ten because that's what we put in our definition.
And then right is going to be equal to 11 because it's going to be the immediately the one next after
it, it's the next one.
So if we had another value and there would be 12.
So you can explicitly assign values if you want to.
But again, what this is important is because when you do comparisons or if you print out the values,
you just have to know that the genomes are actually represented as integers inside the system.
But you can still actually use February, March, January when you sign to your enum, when you actually
compare those values against another value, you can use January, February, March, it still allows
you to use that, those nice names.
So we're going to have a nice little assignment or challenge on enemies so you can become a little more
familiar with it.
But the point here is how can you create an enum type?
And then how can you create variables of that type and assign values?
And again, it's nice because you can control what's being assigned.
If you don't want direction to be assigned, you know, right corner, you just define up, down, left
and right.
Another important data type that I want to discuss is a CHA or a care, depending on how you pronounce
it.
We've talked mainly about numeric data types.
We now talked about the enum.
Char represents a single character such as the letter L a.
The digit character six or semicolon.
And what you see here is the single quotes.
The reason we know their character is, even though one is a number, is because it's in single quotes.
So anything inside single quotes in a program is referred to as a character data type.
And typically the characters will be alphanumeric character characters like ABCD, FG, but they can
still be actual numbers or other characters on the keyboard.
And you can also declare character variables to be unsigned.
Unsigned is just means that they can't be negative.
So it doesn't it doesn't really make a lot of sense.
But what it's telling the compiler when you have an unsigned character is that it's, it's only a positive
number because remember, characters can also be represented as numbers in the ASCII table.
We're going to talk about that in a second.
But we'll talk about you also don't want to confuse the character data type with character strings.
A character data type is just a single character inside single quotes.
We have a whole other section where we're going to talk about strings.
This will be a sequence of characters, a number of characters.
So don't get character or char confused with character, string or string.
If you want to declare a character and use it, you can simply specify the data type, char or care,
and then the identifier.
So in this example, we have charbroiled.
That's declaring a variable named broiled of type char.
When we assign to that variable, we put it.
We put the value inside single quotes.
So broiled equals t is perfectly fine.
If we try to say boiled equals tea and tea is not inside quotes, that's not okay because tea is a variable.
And that would not be valid unless RT is also of type char.
Here we have royal equals double quotes.
T Again, that's not valid because double quotes and C means it's a string, which we'll talk more about,
but that's not valid.
So if you omit those single quotes, the compiler is just going to think that T is the name of a variable
and it's not going to work.
If you use double quotes, it's going to think you're using a string.
As I mentioned, you can also use numerical codes to assign values for characters, and it will just
use the ASCII table.
So the ASCII table is how characters are represented as numbers in the computer.
So there's this whole table.
And basically if you assign a number to a character type, it's going to think you're assigning the
ASCII value.
So what it's going to do, it's going to assign 65 to grade.
It's going to look 65 up in the ASCII table, and it's going to be the equivalent of whatever character
that is.
And you'd have to look in the ASCII table to see what that character is.
But, you know, I know B is like 48.
So again, this is the way the computer represents certain characters by numbers.
So you can't assign numbers to characters, they're just going to be converted to single characters
based on what they are in the ASCII table.
Escape characters.
This is a specific term that you're going to hear about where basically it's a special character that
represents an action and the C programming language.
So for example, you can have a special character that represents a back space or special character
that represents the new line.
Or a special character that represents the Terminal Bell ring.
And you can represent these actions by using basically an awkward symbol like a character inside single
quotes.
And I'm going to give you some examples.
These are also called escape sequences.
These are special characters that represent actions.
So escape sequences are also enclosed in single quotes when they're assigned to a character variable.
You often see these escape sequences inside of strings as well to represent an action.
So, for example, here we have character x equals single quote backslash n.
And you'll notice a lot of the escape characters start with a backslash.
But backslash end is a single character, and it looks kind of strange.
What it represents in a program is it represents a new line.
So if you were to try to print out that character as output, it wouldn't actually print out backslash
n, it would print out a new line.
You would see a new line.
Print it out.
So Char X will represent this character.
And if you were to print variable x, it would just advance the next line.
And there's a number of different escape sequences.
Here's a nice little table I took from a sea primer book that represent all the escape characters that
you can use that represent actions.
So we mentioned the backslash.
N That's a new line.
Backslash.
R Is a carriage return.
Backslash V is a vertical tab.
Backslash T I think is a horizontal tab.
So let me give you some examples of this and also give you some examples of Enum inside of our nice
code blocks editor.
So we here we have our familiar program with our main method and an enum.
Again, an enum is nice because you can, you can decide what values are assigned to it.
But there's two steps.
When you create an enum, you have to define the type and then you have to create the variable of that
type.
And you use the enum keyword for both.
So if I want to create a new type enum and I want to name a have this enum equal to some values, for
example, I could create an enum of type, let's say a.
Enum type gender, we see enum gender.
And then all we do is we use the back, the squiggly bracket.
Not to be confused with the square brackets squiggly bracket.
And we did then define the list of values that gender can be.
Basically, it can just be a male, and then we separate it with commas and then a female, and then
we end it with a semicolon.
And what this is saying is we have a new type called gender.
It's a type enum.
And the only values that can be assigned to variables of this type is male and female.
If we want to use this enum, we can create a new variable.
We just have to say enum the name of the type gender.
And then we can just say my, my gender.
Right?
And that's how we're declaring the variable.
We declare variable my gender, but we haven't assigned or initialized any values.
The only values that we can assign to it are male or female.
So we could say my gender equals male, and that's valid.
And if we were to go and compile.
This would be fine.
There's no errors.
And if we tried to assign something else, like let's just say we want to assign it a string, let's
try saying we want to assign in a number 44.
Now, if we try to build this.
You'll see that it built okay without an error because it was an int.
But if we try to put it in string or something would be more valuable.
It's going to say incompatible types when assigning.
So there's an error there.
It's pretty hard to see.
But basically, we're not allowed to assign it a string here.
Because it's not the right type 44.
You can only sign it male or female.
And then again, if we were to try to see what this represented in terms of integers so we can type,
we can say male.
We can then actually compare it to other variables so we can create another enum gender type of another
gender.
You always want to use good naming conventions here.
Make sure we spell correctly.
That's when we can say female God and then we can actually say create a boolean value to see.
True or false could say bool is male and we could say that's going to be equal to male equals female.
I'm sorry, not male equals female, but my gender equals.
Another gender and this would actually return false because my gender is not equal to another gender.
My gender is equal to male, another gender is equal to fail female sorry and is male would be set to
a false value or zero value.
We can also though actually see the values of enemies by printing them out.
And what we'll notice is that if we were to actually print out the value of the enum gender, which
we haven't learned how to print out variables yet, but we can do something like print f we would notice
that
gender, my gender is actually going to be equal to zero because that's the first in the list.
Another gender is going to equal to one.
So that's pretty neat.
I also want to talk a little bit about chars in our examples, remember.
We can create chars single characters with variable names.
We just use the char type and then we say something like Char, my character, and we can equal to anything
in single quotes.
It can be j f whatever we can even sign it variables which will represent ASCII numbers in the table.
But what this is nice is you can assign it escape characters which represent specific actions like a
backslash n.
And if we were to try to use this or print it out, it would print a new line feed so we could do a
print f and we could say print F, we'd have to do something like percent C, which we're going to talk
about in our next lecture and then print out my character.
And that would actually display to the screen a new line, just a line feed.
So if we were to build this.
And we ought to run it.
What we'd actually see is output is just the new line.
So you kind of see it up there.
There's a new line there.
So it's a specific action.
You're not printing out any output.
But we'll go through more examples of this.
What I was trying to demonstrate here is this nice concept of an enum and characters and escape sequences,
escape characters.
A great way to learn a lot of this stuff is to just play around type code inside the ID and see what
it does.
But to supplement this, I'll provide some challenges to help also with your assessment.
Hope this helped.
Thank you.