**Quick review**

Always use parentheses to disambiguate the precedence of operators if there is any question or opportunity for confusion.

The arithmetic operators all work like they do in normal mathematics. The Modulus (%) operator returns the remainder from an integer division. Beware about rounding or sign errors when the operands of integer division and modulus are negative.

The increment and decrement operators can be used to easily increment or decrement numbers. Beware of side effects, particularly when it comes to the order that function parameters are evaluated. Do not use a variable that has a side effect applied more than once in a given statement.

Relational operators can be used to compare floating point numbers. Beware using equality and inequality on floating point numbers.

Logical operators allow us to form compound conditional statements. Bitwise operators allow us to modify or query individual bits.

**Comprehensive quiz**

1) Evaluate the following:

a) (5 > 3 && 4 < 8)

b) (4 > 6 && true)

c) (3 >= 3 || false)

d) (true || false) ? 4 : 5

Show Solution

a) (5 > 3 && 4 < 8) becomes (true && true), which is true.

b) (4 > 6 && true) becomes (false && true), which is false.

c) (3 >= 3 || false) becomes (true || false), which is true.

d) (true || false) ? 4 : 5 becomes (true ? 4 : 5), which is 4.

2) Answer the following:

a) 7 / 4

b) 14 % 5

Show Solution

a) 7 / 4 = 1 remainder 3, so this equals 1.

b) 14 % 5 = 2 remainder 4, so this equals 4.

3) Convert the following from binary to decimal:

a) 1101

b) 101110

Show Solution

a) 1101 is ((1 * 8) + (1 * 4) + (0 * 2) + (1 * 1)) = 8 + 4 + 1 = 13

b) 101110 is ((1 * 32) + (0 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (0 * 1)) = 32 + 8 + 4 + 2 = 46

4) Convert the following from decimal to binary:

a) 15

b) 53

Show Solution

a) Using method 1:

15 / 2 = 7 r1

7 / 2 = 3 r1

3 / 2 = 1 r1

1 / 2 = 0 r1

Reading the remainders from bottom to top: 1111

Using method 2:

Is 15 >= 8? Yes, with 7 left over.

Is 7 >= 4? Yes, with 3 left over.

Is 3 >= 2? Yes, with 1 left over.

Is 1 >= 1? Yes.

So this number is 1111 in binary.

b) Using method 1:

53 / 2 = 26 r1

26 / 2 = 13 r0

13 / 2 = 6 r1

6 / 2 = 3 r0

3 / 2 = 1 r1

1 / 2 = 0 r1

Reading the remainders from bottom to top: 110101

Using method 2:

Is 53 >= 32? Yes, with 21 left over.

Is 21 >= 16? Yes, with 5 left over.

Is 5 >= 8? No.

Is 5 >= 4? Yes, with 1 left over.

Is 1 > 2? No.

Is 1 >=1? Yes.

So this number is 110101 in binary.

5) Why should you never do the following:

a) int y = foo(++x, x);

b) int x = 7 / -2; // (prior to C++11)

c) int x = -5 % 2; // (prior to C++11)

d) float x = 0.1 + 0.1; if (x == 0.2) return true; else return false;

e) int x = 3 / 0;

Show Solution

a) Because operator++ applies a side effect to x, we should not use x again in the same expression. In this case, the parameters to function foo() can be evaluated in any order, so it’s indeterminate whether x or ++x gets evaluated first. Because ++x changes the value of x, it’s unclear what values will be passed into the function.

b) Prior to C++11, it’s unclear whether the compiler will round this up to -3 or down to -4.

c) Prior to C++11, it’s unclear whether this will result in 1 or -1 (note: You can still use % 2 for even/odd checking).

d) Floating point errors will cause this to evaluate as false even though it looks like it should be true.

e) Divide by 0 will crash the program.

Hi! Coach. Thak you again for this tutorial.

I have a question. Do we have to go through all the chapters in c++ to be really good at it? Let’s say I want to make games or robots. Do I have to do all the chapters or I should focus on specific chapters?

I have an idea and I wanted to share it. You can try to add interactive c++ competitions for fun and people will test their skill at the same time.

No, you don’t have to do all the lessons or chapters to be good. But the more you know, the better you’ll be, as a lot of stuff is inter-related, and the more tools you have in your toolbox the more likely it is you’ll know which tool you should use for a given task.

Ultimately its up to you to determine whether you’re likely to use something. If you think maybe not, spend less time on that lesson and come back later if it turns out not to be the case.

For quiz question 5c, the wording of "Why should you never do the following" might be a bit misleading, when

is valid for even/odd checking even if x is negative.

Fair point, I put added a parenthetical note to the answer.

Did you mean to use "particularly" in the following:

" Beware of side effects, particular when it comes to the order that function parameters are evaluated."

Yep, fixed. Thanks!

alex! you should perhaps consider adding donation links to this page.

this tutorial format is far more helpful than many books out there.

There are donation links in the Support section of the site, for readers who are interested in helping contribute to defray hosting costs.

Thanks, this really helped me. I didn’t move on until I understood this lesson and the one before it. Please note you mention enum (which I don’t know yet) I think you previously used an enum but removed it. Please can you clarify this and remove that part in the lesson thank you

Hey Alex!

Can you please explain the 1st question d part? I don’t know how and where did that bitwise OR operator got in between 4 and 5.

Just a typo. It’s fixed now.

In the comprehensive quiz, there is a line break missing.

…

1) Evaluate the following:

a) (5 > 3 && 4 < 8) b) (4 > 6 && true)

…

Just a small thing, but if you can make something perfect, why not? π

EDIT: Awesome website btw!!! I am looking forward to more quizzes!

Thanks! The site sometimes decides to interpret < as an html tag instead of a less than, and that screws up the formatting.

hii alex !! kudos for this awesome work of yours.

but why don’t we have quizzes after chapter 6?

please add them soon.

I’m working on adding them as I rewrite the lessons but I can only write so fast. π

π

In C++11 the modulo division for negatives is defined now.

Yes, I noted this in the lesson on modulus and division but forgot to update the quiz. Thanks for the note.

It turns out we don’t have further quizzes. How to access more?

I’m intending to add more as I update the articles.

Great tutorial!Whether I was learning c++ or other programming language somewhere else, everything seemed much complicated than on this site. I would say that this is the site where everything is detailed and well explained, even through c++ is really hard to learn/teach, I follow this tutorial without any troubles so far, I wish you all the best, my friend!

What I don’t understand from the quiz is #4; whether or not i’m using 4 bit, 8 bit etc… How would I know that with 15 decimal, I should start of with “15 >= 8” instead of “15 >= 16”. The same goes for 53 decimal (or any other number for that matter…

You must always think in bytes allocated for the given number, because that is the smallest unit that can be addressed in memory. If you allocated an int for it, that should mean 4 bytes (at least, on 32 bit architectures). In a case like this quiz, you only need to check whether the given number can be stored in 1 byte (8 bits, like a char or bool). If it’s too big for that, then you must go up to 2 bytes (like short int). If it’s still too big, then 4 bytes (int) and so on.

When using method 2 of converting a decimal to binary, always start with the largest power of 2 that is smaller than your starting decimal number. So if your number is 15, start with 8 (2^3). If your number is 53, start with 32 (2^5).