Variables and Data Types (Part 3)

The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
2h 17m
Students
37
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

In the previous lectures, we've learned about the basic numeric types such as double and int. And we also explored character based data with both the char data type which represents single characters and the string data type, which contains entire text strings. Now we're going to take a brief look at yet another data type that is used extensively in programming in many languages, especially related to logic and decisions made by the programs, the Boolean data type, which in C++ is just bool, B-O-O-L. Boolean variables are named after the Irish mathematician, George Boole, who is considered by many to be the father of modern symbolic logic. A bool can take only one of two values, namely true or false. So, let's create a new project called BooleanFun. Empty and we'll create BooleanFun. All right. And let's, of course, create our main. We go to right click and Add, New Item, cpp or C++ File and we will name it main.cpp. Once we've done that, we can fill in our skeleton for main. All right.  So, now let's create a Boolean variable isRaining and assign it the value false. So, bool isRaining = false. Now, let's print isRaining and see what happens, isRaining. So, should we get false? Let's see what happens. So, Debug, Start Without Debugging and we get a 0. Well, that's interesting. Well, what happens if we change isRaining to true and then rerun it? Debug, Start Without Debugging, we get a 1. So, with false we get a 0 and true we get a 1. So, what's going on here? The answer is that in C++, a zero value is considered false and a non-zero value is considered true. Boolean and C++ are actually equivalent to integers. They're kind of like integers in disguise if you will and contain either a for false or 1 for true. So, is there any relatively simple way to print out the actual text: true or false for the corresponding Boolean values? There is, and it's fairly simple, but also quite sophisticated and exciting. We need to use what's called the boolalpha manipulator after our cout and our stream insertion operator. So, let's put boolapha here. This is a special variable and it actually exists in a library called iOS, which is ultimately included in iostream. So, write there boolalpha. You only must use it once and all your booleans will print out the textual equivalent true or false, rather than a 1 or 0. That's pretty cool, isn't it? So, you'll see a lot more of the power of Boolean data types in future lectures when we deal with relational operations, logical operations, and making decisions in the code. For now, we have this basic tool in our tool build, so we can move on. So, let's run this just to make sure though. So, Start Without Debugging, and it does in fact print true. Good. Before we move on though, I'd like to issue you another short challenge. I want you to create a fresh new project and using a Boolean variable to represent whether it is currently sunny and another Boolean variable representing whether it's warm wherever you currently are. Make sure to use the boolalpha manipulator to print out true or false instead of 1 or 0. So, pause the video, and come back when you're done or if you get stuck. I hope you were able to solve that challenge, but regardless we will do it together so that you can see how it works. Let's create a brand new project called SunnyWarm. So, we'll close this one. File, Close Solution, Create a new project, Empty Project. Make sure it's under C++, Next and SunnyWarm. All right, we will add the Source File by right clicking Add, New Item, main.cpp. And we can add our skeleton program here. Alright, so we'll create two Boolean variables, isSunny and we will set them to true or false based on what's going on in the current area. So, currently, I'm in Michigan and it's in June, so it's pretty warm. So, I'd say is sunny and it is also warm. So, both of those are true. If it was winter in Michigan, then it would be quite cold, right?  So, yours don't have to contain the same values as mine, of course. So, if it's cold where you are, you would want to put false for isWarm. But in some areas, it can still be sunny and be cold at the same time; Michigan is a good example. So, let's try printing these out, so we'll say boolalpha. And I could just do that separately and then put isSunny and then cout << isWarm << endl; All right. Let's see what happens. So, Debug, Start Without Debugging, and it says true for both of these. Awesome.  So, what if it wasn't sunny but it was still warm? Maybe it's really late at night and it's super, super warm outside but it's not sunny? So, let's see what that looks like: false and true. Awesome.  So, looks pretty good to me. I will see you in the next lecture when we talk about basic documentation using a feature called commands. Let's get going.

 

About the Author
Students
316
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