I’m a little bit unsure of what this section will teach us, but I think we’ll learn more about the keywords, etc. that C++ uses in a program. Operators are probably stuff like plus, equality, etc., but we’ll see.

It’s a relatively long section with 15 lectures and several 10+ minute long videos, so let’s dive in.

If you’re interested in taking the course yourself, you can find it on Udemy.

PS! I’m sorry this is so long, but it’s a long section. Please give your feedback in the comments if I should split up these longs posts in multiple parts.

adult doctor girl healthcare
I really need to get better at finding pictures that illustrate the topic.

Lecture 62: Section Overview

Again, an excellent overview of what we will learn.

As I said earlier, I was a little unsure of what expressions, statements, and operators meant. The only thing I could come up with was mathematical operators like plus, minus, etc. But after this section, we have some idea of what we’ll learn in these lectures.

Lecture 63: Expression and Statements

Here we get some examples of what expressions and statements are.

Expressions are a sequence of operators that gives us some result. Some examples are:

  • 5 + 2
  • a = b
  • number_1 > number_2

Statements are a complete line of code that contains one or more expressions.

  • double VAT_rate {0.25};
  • cout << “The VAT on this item is ” << VAT_rate * item_price << endl;

Notice that both statements end with a semicolon. That is what makes it a complete statement.

Lecture 64: Using Operators

Operators are pretty easy to understand, but C++ got a bunch of them.

For instance, you got the assignment operator (=) that assigns a value to something. You got plus and minus (+, -), etc. for calculations. You got operators that allow you to check the relation between different objects (less than, larger than, < and >) and then you use that comparison in logical operators to do one or several things, based on the result of the comparison.

Some operators work on just one object, like turning a positive number into a negative one. Then you have operators that work on two objects, for instance, multiplication. And lastly, you have some that operate on three operands.

I think this is one of the things that are way more complicated to understand when you read it in text than when you see it in a program. The instructor promised us practical examples in the coming lectures, and I think everything will get clearer at that point.

Lecture 65: The Assignment Operator

The assignment operator in C++ is =. Using variable_name = some_value will assign the variable variable_name the value some_value. It’s pretty easy, but there are a few things you need to know.

The types on each side need to be the same. Both need to be number-values, strings, etc. You can’t put “This is a sentence” into an integer variable. Another thing you can’t do is assigning a new value to a constant. You’ve told the compiler that the value shouldn’t change, so naturally, it will complain if you try to do just that.

The assignment operator also has a few tricks up its sleeve. For instance, you can assign multiple values in one operation.

This lecture shows us all of this in practical examples in this video.

One operator that looks very similar to the assignment operator and you will see in all C++ programs is == or two equal signs. For example, variable1 == variable2. This statement is checking if the values are the same. The statement is NOT assigning the value of variable2 into variable1.

Lecture 66: Arithmetic Operators

This is just as easy as it sounds. It’s the addition, subtraction, multiplication, and division you learned at school. You also get one that you didn’t learn, modulo, but modulo is just the remainder of a division operation.

All the first four operators work on several types, int, doubles, etc. This is our first example of overloading when an operator works with multiple types.

The modulo-operator is not overloaded and only works on ints.

When you watch this video, everything is shown in clear examples. There’s also a practical example showing how to convert Euros to USD in C++ using all we’ve learned so far.

The video seems to stop right in the middle of a sentence, but it’s just the wrapping up part, so I don’t think we’re losing any content.

Lecture 67: Increment and Decrement Operators

This lecture teaches us about the ++ or — operator. They’re easy to use, but there are still some pitfalls in using them.

You can use them both before and after the variable you want to use them on, var++ or ++var. This can cause trouble if you place it in the wrong location. The instructor shows us with practical examples of what happens when, so you should know the difference after this lecture.

You might remember I used this operator in the last sections challenge, where I used it in some for-loops. That is one of the places that this operator is frequently used. The instructor doesn’t recommend you use this operator too much as it can easily be unclear what happens, but for-loops are one of the places he mentions it being OK using it.

DO NOT use it twice on the same variable in the same statement. That’s undefined behavior, so you do not know what will happen when doing that.

This is an example of a phone operator. She’s not included in C++.

Lecture 68: Mixed Expressions and Conversions

Sometimes you use an operator on variables of different types. C++ will then try to convert the operands to be of the same type. If it can’t convert the types, the compiler will throw an error at you.

C++ arranges the types into a hierarchy of lower and higher types. Higher types can hold larger values than lower types. C++ can then use coercion to promote a lower type to a higher type or demote a higher type to a lower type.

These conversions can have the effect of you losing information. You can, for instance, convert a double to an int. If the value of the double is 100.2, the int will only hold the value 100, and the 0.2 part is lost.

Sometimes the compiler doesn’t do a conversion when we want it to. We can then explicitly tell it to do that by using what’s called explicit casting.

This lecture goes through the rules of what happens when you do casting in C++ and ends with us writing a program that asks for three integers and calculates and prints the total sum and the average.

I wrote a variant of the program where I first ask the user how many numbers he wants to enter. I then read the values into a vector using a for-loop, and at the same time, I calculate the total. In the end, I present the total and the averages using both integer math and by casting to double. As always you can find my source code on GitHub.

Lecture 69: Testing for Equality

This is an easy lecture as it’s just showing you that C++ can check if to values are equal or not equal. This is done using == or !=.

But even if it’s easy to check. This is also a source of bugs in many programs, as it’s easy to do things like val1 = val2 for checking for equality. This will not work as using just one equal sign assigns a value in C++. You must remember to use val1 == val2 to do these kinds of checks.

And again, this course goes further than any others I’ve taken. This is the first time I’ve heard about the keywords boolalpha and noboolalpha.

The lecture goes into practical examples showing you all how this works. It’s worth watching this lecture as you might get surprised at how doubles work and are stored internally in C++. Two different numbers might not necessarily look that way to C++. I also show this in my own test program.

Lecture 70: Relational Operators

In addition to checking for equality, C++ can also check other types of relations between different values. This lecture shows you four other relational operators that C++ contains now and a new one that will be introduced in C++20.

You have the standard >, <, >=  and <= for checking for greater than, less than, greater than or equal to, or less than or equal to.

C++20 will get a crazy three-way operator that can tell you with just one statement if two values are equal, or which side is the larger.

Frank again shows us practical examples of how this is done. I didn’t do my own source code for this part, as it should be reasonably self-explanatory.

Lecture 71: Logical Operators

This part goes into what’s called logical operators; not (!), and (&&) and or(||). You use these when you want to use boolean algebra on one or more statements. You will always get true or false as the answer when you do this.

The lecture goes through boolean algebra using a truth table to show you what these operators do. The instructor also shows you the precedence of these operators, so you know which one is getting evaluated first in a complicated statement.

He also explains something called short-circuit evaluation. This is common to many programming languages, and smart programmers use this to optimize the code. In short, it means that the program stops evaluating the expression as soon as it knows what the result will be, even if just some of the statements have been evaluated.

A significant part of this lecture is again devoted to practical examples using CodeLite. I didn’t write any code of my own this time either as I think Franks examples are comprehensive and much better than whatever I could come up with. We’re also nearing the section challenge, and I suspect logical operators will be a part of that.

Lecture 72: Compound Assignment Operators

This lecture goes through compound C++ operators. This is a kind of shorthand you can use to write less code.

An example is these two lines.

  • num1 = num1 + 3
  • num1 += 3

They both do the same, namely, add 3 to the value of num1. As you can see this can lead to much less writing and it’s also easy to read.

The lecture has a table showing 10 different operators C++ have and I would advise you to learn them as they are ubiquitous in C++ programs.

Lecture 73: Operator Precedence

This is yet another lecture you must accept and learn. The table shown in this part is not complete, but there are many lists on the internet showing you a full set, for instance, this one on cppreference.com.

The lecture then goes on to explain the difference between precedence and associativity. Precedence is which of different operators that are evaluated first. Associativity is which part of a statement that is evaluated first if the operators are the same or have the same precedence.

If you are unsure of the order in which an expression will evaluate, it’s always good practice to use parenthesis. That way you can be sure to get the result you expect.

Lecture 74-75: Section Challenge, Solution, and Quiz

For the challenge this time we are going to make a program that asks for an amount given in cents. We will then display how this can be divided up into a correct change in dollars, quarters, dimes, nickels, and pennies.

The instructor gives us one hint and one tip here. First, he asks us to write down in simple English, or another language, how you will solve the problem before starting to code. Next, he gives us a tip that this can be addressed using the modulo operator.

My thinking on how to solve this is as follows:

  1. Ask the user for the amount
  2. Divide amount with 100 to get the number of dollars and then use modulo 100 to get the remainder
  3. Divide remainder with 25 to get quarters and then use modulo 25 to get the new remainder
  4. Divide remainder with 10 to get quarters and then use modulo 10 to get the new remainder
  5. Divide remainder with 5 to get quarters and then use module 5 to get the pennies
  6. Print the results

All division is using integer division since we won’t be using decimal numbers in this program.

I tried to code this to the best of my ability, and as always you can find my solution on my GitHub-page. My program is not at all advanced. I could use checks to see if amount equals zero and then skip the calculation etc., but since we don’t learn about if-else and other such statements until next section, I chose to skip that here.

The instructor gives us two examples of how he did this. My version was pretty similar to his version using modulo, but he declared the value of dollars, quarters, etc. in constants which make it easier to read as it doesn’t use the magic numbers that my solution did. I, therefore, decided to change this in my uploaded source code.

In this sections quiz, I only managed 9 correct of the 10 questions. The difficulty ramps up now, as you really need to sit down and do the math, comparison, etc. to get the right answer. But I do see that the instructor has now included an explanation of the answer even when you get it right. That I DO like because then I know if my thought process was the right one.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s