Variables and Data Types (Part 1)

The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
2h 17m
Students
54
Ratings
5/5
starstarstarstarstar
Description

This course provides you with a solid understanding of the fundamentals of C++. We will take a look at the components of the programming language and then put these into practice through a couple of projects that we will run through at the end of the course.

Learning Objectives

  • Learn how to store different types of data in main memory
  • Understand how to manipulate and perform operations on that data, including performing arithmetic on numbers
  • Understand how programs make decisions
  • Learn how you can write your programs to communicate with users

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • Experienced C++ programmers who want to stay sharp!
  • College students and anyone studying C++

Prerequisites

This is a beginner-level course and so no prior knowledge of C++ is necessary.

 

Transcript

Previously, we discussed how to print output to the console. Now, we're going to explore some other concepts that are essential to becoming a good C++ programmer. The first concept is that of a variable. For now, just remember that a variable is a named "chunk" of memory. It's like a box that can hold something. So, what determines what kind of thing can live in this box or what we can put into it? That's where data types come in. A data type is, as the name suggests, the type of data that something like a variable represents. Let's get to some coding, shall we? So, I'm going to create a new project here, make sure that it is under the C++ language, Empty Project, hit 'Next'. And we're going to call our project VariableFun. And click 'Create'. So, as we did with the other empty project, we're going to right click 'Source Files', go to Add, New Item, and make sure that we have a source file here. And again, you can call it pretty much anything, but I'm going to call it main.cpp. I'm going to also fill in what we call the skeleton code that we had before. Int main(), return 0. Okay. So, we have the iostream ready as well because we need that so that we can use cout. So, let's dive in and declare a variable named myInt, so int myInt. And then on the next line we will say myInt = 15. So, what we have done on the first line is we've declared the variable myInt, and on the next line we have initialized it or assigned it the value 15. This 15 is also called a literal constant. So, the name of the variable myInt is something that I came up with; I just named it. I could call it myInt. I could call it number baskets. I could call it whatever I'd like as long as it makes sense. Int on the other hand is a keyword in C++ and it is a data type. So, we shouldn't use this as an identifier for the variable. Another term for identifier is just variable name. So, again we have variable declaration, variable initialization because we give it an initial value. Note that there are some special rules about coming up with identifiers. You can start your identifier with any letter, upper or lowercase, the underscore, but you can't start it with a number. So, identifiers can contain numbers. So, I could call this myInt1 or my10Int or something like that, but you can't start it with a number. So, they can contain numbers but the first character in the identifier can't be a number, and also you can't contain special symbols like the &, the @ symbol, any of the operators like the addition operator, subtraction operator, multiplication, division, modulars, etc. Things that we'll look at a little later. You also can't use names that are already keywords like int or return, and you cannot have a space in the variables name. So, I couldn't call this my Int, because then, it has no idea what's going on here. Another syntax feature you should note is that I also put a semicolon at the end of each of these lines, as you can see here; right here, and right there, and also there. They form something in C++ that we call a statement, and that's why we need a semicolon at the end because C++ requires semicolons at the end of statements. So, the semicolons, if you're not familiar, on a standard typical American keyboard, they're right next to the L on the keyboard, and they're on the same key as the colon. So, what is an Int anyway? An Int data type indicates that our variable can hold an integer, which as you might know from mathematics, is just a whole number. In other words, integers don't contain any decimal data whatsoever. Integers are just zero, the counting numbers 1, 2, 3, etc., and their opposites -1, -2, -3, etc. 1.5 would not be an integer. The equal sign that I use in here actually takes on a special meaning in C++ as well. This is called the assignment operator because I'm assigning the value 15 to myInt. The first time we assign a value to a variable you can also use the term initialization because the variable is getting this initial value. But in general, we just use the term assignment. So, we're saying that myInt is assigned to the value 15. Let's print out our variable myInt to the console. This will combine what we learned before with what we're learning now. So, I give it a couple extra whitespaces just to make it look a little prettier but you really don't have to. I could have all of this code crammed right next to each other, but this just makes it look a little better. So, I'm going to print myInt followed by a new line here, and let's run it. So, Debug, Start Without Debugging. And as you can see, it prints out 15. It doesn't print out the identifier of the variable, it prints out the contents of the variable. So, that is 15 right there. If I changed it to 20, run it again, it would print out 20. So, I'm going to leave it as 15. So, that's a good job. We're going to look at another useful data type, the double. So, we're done with the integer right now, we might want to declare or use a double. So, I'm going to write double myDouble = 3.14159. Now, a double is a data type that says our variable can hold a real number. Whereas the integer type cannot accommodate decimal values, doubles can. So, it can accommodate the three but it also can accommodate values with a decimal place, so 3.14159, which is like Pi. Notice this time I did things a little differently. I declared and initialized my variable all on the same line. This is an alternative to what I did earlier with the integer. In fact, we could have declared and initialized our integer similarly all on one line, if we had wanted to. So, I could have put Int myInt = 15 all on one line and not split it across two lines. This is just showing you two alternatives to do the same thing; perform the initialization. Let's print this double out in addition to the integer we're printing out already to see if you can do this on your own. So, what I want you to do is to pause the video and this will be a challenge. Come back when you're finished or if you get stuck. All right. Were you able to get the double to print to the console? If you did, that's fantastic. If you didn't, don't be discouraged. The more practice you get, the better and better you're going to become as a C++ developer. Let's add the code to print the double out together. So, right below this myInt, I'm going to put myDouble. Now let's run it. Debug, Start Without Debugging. It compiles it and then runs the application. You'll notice that I have 15. That was from the integer being printed out here. And then I have 3.14159. That is from my double. Excellent. So, there we go. As a quick note, when we name variables, besides the obvious rules about syntax that we discussed earlier, there are also something called conventions that are followed by most developers. For example, in C++, variables should always start with a lowercase letter. Usually, I use syntax called camel casing, because when I write a variable name like state tax rate, I write it like this. So, if I wanted to write my state tax rate, I'd say double stateTaxRate = 0.06 like that. Notice how the first letter s is lowercase. The subsequent letters are mostly lowercase but are capitalized if I come to a new word, and I would do this even if it was a partial word as well. Because we can't have spaces in variable identifier and writing all of the words making up a variable name would make it hard to read, camel case names make the names much easier to read. Also in case you're wondering why it's called camel casing, it's because the words make up the identifier like stateTax and then a Rate, they look like the humps of a camel. Pretty cool, huh? So, in this lecture, we learned a lot about data types and variables such as how to declare variables and store values in them using the assignment operator. We focused on numeric types such as the integer and the double. We have some more awesome data types to explore in the next lecture, including the character and string data types, so let's check that out.

 

About the Author
Students
474
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics