Transcript for:
C++ Variables Overview

hey guys i'm gonna explain what variables are a variable you probably remember from middle school math class a variable is a representation of some number or value there are two steps to creating and using a variable declaration and assignment we'll begin with declaration to declare a variable we need to list the data type of what we're storing exactly in programming you can store more than just numbers you can store characters even whole sentences etc let's work with whole numbers to store a whole number we will type int for integer then we need a unique identifier for this variable for now let's just say x we're used to working with like x and y in math class right we will end the statement with a semicolon this step is declaration now to assign a variable you take the variable's name that unique identifier then we will set this equal to some number since we declared this variable as an int it will store an integer maybe like five now this variable behaves as if it was the value that it contains it will behave as if it was the number five then to display a variable we can use standard output std c out we will display x and let's see what we have there's our value five this first step is declaration the second step is assignment the nice thing about doing this in two steps is that you can later assign your variable a value if you know what value you would like to give your variable right away you can do that at the beginning of your program you could combine both of those steps int x equals 5 and that would do the same thing in cases where you don't know what value you would like to give a variable you could assign it later such as when you accept user input you don't know what the user is going to type so let's create another int y equals 6 then let's display whatever y is i'll add a new line let's copy this paste it okay x is five y is six we could even do something like this let's say int sum equals x plus y then we'll display whatever our sum variable is c out sum the sum of x plus y is 11. now there's different data types depending on what you need to store within a variable exactly the int data type stores a whole integer let's think of a few examples of whole integers what about an age that's typically a whole number inch age equals 21. let's think of two more examples what about a year int year equals 20 23. how about days int days equals seven the int data type can only store a whole number with days what if we assign this a value of 7.5 let me show you what happens all right i will display days all right that decimal portion is truncated when i display days and we attempt to assign 7.5 well this variable can't store that decimal so it's truncated if you need a number that includes a decimal portion there's a different data type for that and that is a double this is a number including decimal a few examples of a double would be maybe a price there's dollars and cents double price equals 10.99 what about a gpa a grade point average that includes a decimal double gpa equals 2.5 then maybe a temperature double temperature equals 25.1 guess this could be in either celsius or fahrenheit then let's display maybe price price yeah and that decimal portion is not truncated much like what you see with whole integers if you need a number that includes a decimal portion use a double now we have the char data type that stores a single character type char maybe we're working with student grades i'll name this variable grade equals then to store a single character you use single quotes this student has an a two more examples what about an initial singular not initials char initial uh what about b so i'm going to display initial initial okay we have our single character of b now check this out what if i attempt to store more than one character i'll add c we have a warning we have an overflow in conversion from int to char so what's displayed is the last character just c so chars can only store a single character here's one more example of a char data type what if we're working with currency what type of currency will we work with char currency equals maybe a dollar sign if we're working with a different type of currency we could change this to a different symbol yeah that's the char data type it stores a single character next on our list is booleans boolean a variable that's boolean has only two states true or false to create a boolean variable you type bull then a variable name so these are applicable to anything that has two states what if somebody is a student they're either a student or not a student bool student equals true if they're not enrolled in classes or they graduated you could set this to be false think of a light switch the light switch can either be on or off you could say a light switch is boolean there's only two states true or false how about bull power is something powered on or not power equals true if it's turned off that could be false maybe we have a store and we need to mark if something is for sale or not like is it available bool for sale equals true if an item in our store isn't for sale like it's not available we could set this to be false so that's the idea behind boolean values it has two states true or false the last data type i'll cover is strings a string is technically an object that represents a sequence of text think of it as the char data type but we can store more than one character even whole sentences like a name or an address strings are provided from the standard namespace to declare a string we would type standard string then a variable name what about just name like restoring a user's name place your text within a set of double quotes then why don't you type your first name then we will store that within this variable name then to test it let's display it standard output we will display name and there is your first name let's create a couple more examples what about a day of the week standard string day then pick a day i like friday what about food standard string food i like pizza i'll store this series of text as a string then maybe an address standard string address equals make up some address one two three fake street basically speaking a string is a type of object that represents a sequence of text such as a name a day an address etc now i'm going to show you how we can display a variable along with some text i would like to display hello then whatever your name is i will type what is known as a string literal we're literally printing a string hello follow this string of text with a variable my variable name is good then let's display it hello then whatever your first name is but you do have to pay attention to spacing after my word hello i'm going to add a space there hello bro then let's display our age so i'm going to add a new line character standard output we will display u are age years old there hello bro you are 21 years old uh make sure to pay attention to the spacing as well because it's easy to mess that up all right so those are variables we covered a few of the basic data types but there's more advanced data types once we gain a little bit more experience with this that i'll cover we have integers which store a whole number doubles which are numbers that include a decimal portion chars are single characters booleans are either true or false then strings represent a sequence of text an important note with strings is that you can include numbers but they're treated differently so yeah those are variables in c plus plus your assignment is to in the comment section post a integer variable a double a character boolean and a string think of some examples preferably some examples that i may have not covered already that would be good practice well yeah and that's an introduction to variables in c plus plus