I remember when I first learned to program. It was in BASIC on the good old Commodore 64. There were loads of GOTOs all over the place, and the program code was difficult to follow.

Hopefully the functions C++ gives us is way better control and overview of the source. I suspect we will see at least some if-else statements, some for-loops, some while-loops, and even more I can’t remember right now.

This section is even more extensive than the previous one, so I have split this section into two blog posts. That way I can push them faster to you, and they are more readable since they aren’t so long.

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

men working at night
These guys looks like they have control.

Lecture 76: Section Overview

In the overview, we get an introduction to the three building blocks a C++ program consists of. These are sequences, selections, and iterations. With these three items, we can write any algorithm we want.

We also get a quick review of all of the different kinds of selection- and iteration structures that exist in C++. These will be further explained in the following lectures of this section.

Lecture 77: if Statement

This is an 18-minute whopper of a video, but when you see it, you will understand why. In addition to introducing and explaining the if-statement, we also learn three other features of C++; blocks, indentation, and scope.

The if-statement is a fundamental element of every program, and it merely tests if some expression validated to true and if it does, executes one or several lines of code. This is where the equality, relational and logical operators we learned about in the last chapter comes in.

One example from real life I’m pretty sure shouldn’t need more explanation can be coded like this:

if (traffic_light == red) 

You notice that I’ve indented the stop_car()-line. That is to make it easier for us to read the code. The C++ compiler really doesn’t care, but it makes it easier for us humans to read the code.

But what do you do if you want to do more than one thing? That’s where blocks come in. Something it seems we have already used in this course but haven’t thought about or had a word for. A block is one or more statements encased between { and }. If you look at the previous programs you’ve written in this course, you will see that the main()-function is just such a block.

Here’s an example of using a block with an if-statement:

if (traffic_ligth == green && car_is_stationary == true) {

One cool thing with blocks like this is that they are also what is called a scope. What this means is for example that if you declare a variable inside a block, it will only be available inside the block.

The instructor shows all this in a practical example using four different tests conditions. I like that he writes the programs as he talks about it, even if that means we will see errors, typos, etc. I think that the lessons we learn by hearing his reasoning as he works out the program is more important than just getting to see the finished result.

Lecture 78: if-else Statement

This is very similar to the if-statement we saw in the last part. You just add an else-statement that will execute if the first statement is not performed. This means you can read an if-else statement as If this check validates to true the do this, else do this other thing.

You can also expand on this with if-else-if. With several statements like those, you can do multiple checks at once.

Again, the lecture is closed off with practical examples using CodeLite. I can’t really come up with a better case myself, so I didn’t. 😊

Lecture 79: Nested if Statement

You can also nest if and if-else statements within each other. You’ll probably find yourself doing this quite often, as many decisions can’t be decided by just one statement.

But this leads to a problem, which Franks describes as the dangling else problem. In short when you have several ifs and one just else, which if-statement does the else belong to? The rules in C++ is that an else belongs to the nearest if.

This can get very confusing, so it’s essential to use proper indentation so you can see which if belongs to which else.

The significant part of this lecture is again Frank going through two different practical examples. I will recommend you do them together with the instructor as you will see the efficient use of the indentations.

In the shipping calculation example, you might see a glaring omission in the input checking. I would also add a check to see if one or more of the dimensions are below 1. So I did. 😊 As usual, you can find my code for this course on GitHub.

I’m a little sad that it doesn’t look like CodeLite is smart enough to un-indent, if that is a word, when you’re not using a code block with {}. I need to check if that can be changed somehow.

Lecture 80: switch-case Statement

Another 20-minute lecture. We really get our money’s worth in this section. 😊

This selection statement is a little different as the switch statement runs a control expression and gets a specific answer that is an integer or integer literals. You then write one or more case statements, and C++ will run the one that matches the result of the switch expression.

The syntax is very different from an if-else statement, but the instructor shows us several examples on how this works and does some live coding in CodeLite, so I feel confident I now know how this works.

In the first example, I elected to do my checks a little different and used toupper() on the input we got from the user. That way my select statements are a bit shorter, but I guess the instructor did it the way he did as a refresher on the Boolean operators we learned about earlier. You know where to go if you want to see my code, hint GitHub.

In the other example, Frank quickly shows us how C++ handles a switch-case statement using an enumerator. If you forget to check for all values in an enumerator and don’t include a default, the compiler will give you a warning about this. That’s a pretty neat feature, as you will then know if you have handled all the different possibilities or not.

Lecture 81: Conditional Operator

No, it’s not a malformed question lacking any words, that is the conditional operator in C++.

I’ve seen that in many C++ programs and never understood how it worked. It should be said I didn’t work hard to understand it either, nor did I do a Google search, but it does look a little bewildering.

Luckily it isn’t, and this 10-minute video is all it takes to get to grips with it.

It is very similar to an if-else statement and goes like this:

(expression to be evaluated)?(return this if true):(return this if false);

The instructor shows us several slides on how this works and also goes through a practical example in CodeLite. So, in the end, you should have a good grasp on this strange, little operator.

End thoughts

I will do this section in two parts since it is another long one, but before I go, I just have one more thing to say.

When the instructor is doing his practical examples in CodeLite, listen to him, hear him out to see what he will show you and then pause the video and try to do it yourself first. You will learn SO much more by doing that.

You might not solve it, you might do it in a completely different way or maybe precisely as Frank does it. Either way, you have learned more coding by just trying it yourself first.

Next time I’ll complete this section and do all lectures on looping, the challenge, and the section quiz.

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