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. Prior to C++11, beware of integer division and modulus using negative numbers.

The increment and decrement operators can be used to easily increment or decrement numbers. Avoid the postfix versions of these operators whenever possible.

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.

The comma operator can compress multiple statements into one. Writing the statements separately is usually better.

The conditional operator is a nice short version of an if-statement, but don’t use it as an alternative to an if-statement. Only use the conditional operator if you use its result.

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.

Quiz time

Question #1

Evaluate the following:

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

b) (4 > 6 && true)

c) (3 >= 3 || false)

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

Question #3

Why should you never do the following:

a) int y{ foo(++x, x) };

b) double x{ 0.1 + 0.1 + 0.1 }; return (x == 0.3);

c) int x{ 3 / 0 };

O.1 -- Bit flags and bit manipulation via std::bitset |

Index |

5.7 -- Logical operators |

Shouldn't question 2a read "7%4"?

Nope because %=modulas=reminder, /=quotient

so when 7 is divided by 4, quotient is 1 and reminder is 3.

therefore 7 / 4 = 1 and 7 % 4 = 3

Thank you for such an amazing service. I wouldn't want quarantine any other way :))

Greetings from Denmark!

Question #3a's answer:

> 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.

I think you should explicitly say that foo(++x, x) causes undefined behaviour, instead of just implying it.

This chapter was well written, thank you :)

`foo(++x, x)` doesn't cause undefined behavior (anymore). You could even modify `x` twice: `foo(++x, ++x)`, still no undefined behavior.

Since C++17, all side effects of 1 argument are finished before evaluation of another argument begins. The order of evaluation is still unspecified though.

If you find a place on learncpp that still claims undefined behavior for this, please let me know.

Ah, ok. Thanks!

23.07.20 Chapter #5 completed. Well written & understandable. Questions are on point. Thanks!

(true || false) ? 4 : 5

What’s the ? And how does this work?

That's the conditional operator from lesson 5.5

Isn't another reason not to do 3b is the redundancy in the if statement? (x == 3) already returns true or false. Hence we can simply use return (x == 3); rather than if (x == 3) then return true; else return false;

yeah, you were right but it is a question so author asked it in detail.

That's right, I updated the quiz. Thanks for pointing it out!

Hello :)

In the last question:

"Divide by 0 will crash the program."

I think you meant division by zero.

Thanks, answer updated

Hey nascardriver!

I've found a typo again:) In "Quick Review" at the beginning , "The Modulus (%) operator returns the remainder from an integer division." modulus is capitalized, should be "modulus".

Fixed, thanks!

Hi,

This summary doesn't mention commas and the conditional operator (section 5.5).

Well spotted, comma and conditional operator added.

Hey Alex,

First of all, thank you so much for making this tutorial. It's been so very helpful. I totally appreciate all the work you've put into making this site.

I had a question:

#Question 3 b)

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

I changed this to

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

It returns true.

I don't understand why it's returning true exactly. Is a implicit conversion to double causing the program to return false? If we used the "f" suffix here, as you cover in a previous chapter, does it eliminate the rounding error? So it should be okay if we compare float numbers as long as we remember the f suffix? Could you please explain this?

I'm sorry if you've talked about this already. I'm kind of new to programming (well, not exactly, but I never took it seriously before).

Thank you in advance for your time!

I updated the quiz question a bit, both to get rid of the mismatch between the float variable and the double literals, and to actually make the statement break as expected.

Strangely enough 0.1 + 0.1 == 0.2 is true, but 0.1 + 0.1 + 0.1 == 0.3 is false, which just goes to highlight the unpredictable nature of floating point precision errors.

in the solution for last part of Question #2, we can see: "14 % 5 = 2 remainder 4, so this equals 4." which I guess it should be written: "14 / 5 = 2 remainder 4, so this equals 4."

I mean the operand "%" must be changed to "/"

Fixed. Thanks!

Here is my updated solution with a struct (Question #2)

"Variables" is a bad name as it doesn't tell anything about the struct's contents.

`guess` shouldn't be stored in the struct, it's a one-time use variable and there's no need to remember what it was after it has been processed.

Packaging the variables is a good start. It's not all to useful with what you know so far, but you'll learn about more ways of fitting things into objects later.

For the 3rd question in the quiz, clarifying that the binary is unsigned would be nice.

Good point. Amended. Thanks for pointing that out!

Quiz 5d - may actually work correctly and return true, even though neither 0.1 nor 0.2 will be represented exactly. If the compiler and hardware are consistent, the only difference between the representations of 0.1 and 0.2 will be a change of 1 in the exponent (indicating a change by a power of 2), and there should not be any additional errors introduced by the addition calculation given that the same number is being added to itself. I suggest replacing "will" with "may".

Done. Thanks for the feedback.

Alex,

Would advise everyone to execute Quiz 5) on computer. Comments on 5d): In float x = 0.1+0.1;, float x is assigned double 0.1+0.1, and because of double precision if statement is false. However, float x = 0.1f + 0.1f; if (x == 0.2f) -> true.

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.

Just a note, some of the formatting is not consistent in the quiz sections. Some questions have a line before part a) and some don't. Example:

2) Answer the following:

a) 7 / 4

b) 14 % 5

3) Convert the following from binary to decimal:

a) 1101

b) 101110

Fixed, thanks!

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).