Let’s continue our journey through controlling the program flow in C++.
Last time it was all about evaluating an expression and doing stuff based on the result. Our four weapons to do that was the if-statement, the if-else statement, the switch-case statement, and the conditional operator. Frank also snuck in some learning about blocks, indentation, and scope.
In this second part, we will go through different kinds of loops and end with the section challenge and quiz.
Updated lecture 90-92 to reflect that I’ve now done the challenge and uploaded the result to the repository.
Lecture 82: Looping
C++ has two or four different types of loops, depending on how you look at it. Frank says three in the course, but I’ll stick with my claim of two or four.
The first is a for loop which iterates something X number of times. This is a very common loop, and I have already used it in some of my code examples in earlier sections.
Then there is a variant of this, the range-based for loop, where you, for example, go through all the elements of a vector. If you remember I used a for-based loop to do this in the section about arrays and vectors. If I had known about this type, I would have used that instead. But that’s what the course is for, learning something new.
The third one is the while loop, which has a variant in the do-while loop, so altogether four types of loops.
The difference between the while-types is that the while loop checks a condition at the beginning, while the do-while checks at the end. This means that a while loop may never do the code inside of it, but the do-while variant will execute the code at least once.
Lecture 83: for Loop
22-minute video? I thought for loops were easy… This video is actually full of information, not only on how to write a simple loop.
Of course, he shows you that, but we are also taught how to use the modulo operator for checking specific values in a loop. He talks about the conditional operator again. He speaks more on the importance of formatting your code and how, in some instances, the compiler will help you by showing you warnings. You learn about the comma operator for using multiple variables in one loop, and he also quickly mentions how to create an endless loop.
So as you understand, this lecture is chock full of examples. Some shown on slides, but again I like that most of them are practical examples in CodeLite. It’s all pretty straightforward, but if you haven’t written code before, I really do recommend you follow along and type the examples yourself.
There’s a neat example the instructor writes using the conditional operator we learned about just a few lectures ago. Do try to write that one yourself before you watch Frank do it. I managed to do it, but I had to think for a few minutes on how you could put an endline into a conditional operator. We have used that method before, and some of you might get it right away, but I bet some of you, like me, had to do some thinking.
The examples also do prepare you for the exercises he has recently added to the course, so if you did the typing while following the video, you should be set to solve the code exercise easily.
Lecture 84: range-based for Loop
This feature was introduced in C++11, so it’s a very new function.
It makes it very easy to go through a vector, string, array, etc. and manipulate each element as C++ iterates through the loop. And you can also use the auto keyword with the variable, making it even easier to use this.
This loop type is really simplifying a lot of the work needed to go through a collection compared to first finding out the size, then declaring an integer to count with, comparing that int to the size we found earlier, incrementing it, etc.
There have been some smart thinking by the C++ guys here, I must say.
Lecture 85: while Loop
This is the loop to use if you want to test an expression before you run the loop code. This means that sometimes the code in the body of the loop will not run even once.
You check a condition, and if it’s true, you run the code in the body of the loop. Then the check is rerun, and you either run the code, and the cycle continues, or you end the loop.
It’s therefore critical to change the variable you check in the loop body. If you don’t do this, you might end up with an infinite loop.
Lecture 86: do while Loop
You’ll use this version of the while loop if you want the code in the loop body to execute at least once. The test will be performed after the body has finished and depended on the result, you’ll rerun the loop or jump out of the loop.
Very similar but very nice to have both options as you sometimes do want your code to run at least once and sometimes you don’t.
The lecture ends with the instructor writing an example of how you could make a menu selection in C++. I watched his introduction, paused the video and wrote my solution before I watched the instructor do it. I really do advise you to do this. I chose another path but got the same end result. You’ll learn so much more doing it yourself first than just watching what the instructor does and then repeating it.
A bonus for me was that I got a bug in my code, so I had to brush up on my switch statements again to find out why the program wouldn’t do what I wanted it to. Bonus repetition for me there. YEAY!!
And DO think about why he had to use AND not OR in the test expression. My first thought was also that he should have used an OR, so when he used AND my brows kind of met in the middle for a while. But imagine that the user types an ‘a’ and do the test expression as a mental exercise. Or on paper, that works too.
You should end up agreeing with the instructor. And if you struggle, the answer can be found on the page about operators on cplusplus.com.
Lecture 87: continue and break
This was a change from the previous videos since this clocks in at under 3 minutes. But why make it long and complicated, when you can make it short and sweet?
But I really do see that these statements can be powerful tools when used in the right places. We’ve already used break in the switch-construct to terminate and jump out of the loop.
Continue is almost the same, but instead of jumping out of the loop, it jumps back to the beginning again continuing the next iteration.
At first, I had to think about that continue statement, as it sounds like you should use it to continue on in the program. But it got more evident when I found out that you must think of these two related to the loop you are in. You either break out of the loop and terminate it, or you continue the loop from the beginning.
Lecture 88: Infinite Loops
Infinite loops are just what they sound like. Loops that never ends.
Usually, these kinds of loops are unwanted and the result of a bug or poorly designed code, but the instructor does come up with a few examples where you would want to use a loop like this. But unless you’re writing an OS or a mobile app waiting for an input, the advice is to avoid these constructs.
Lecture 89: Nested Loops
Now we’re back to a lengthy video again, but again it’s mostly practical examples in the IDE after the instructor first shows us some cases.
I listened to what the instructor wanted and wrote my code before I looked at his. This means that my code is different from the instructors. His code is better designed to show nested loops of different types and the importance of proper indentation to help with readability. My source is as always viewable on my GitHub.
This shows how easy and powerful it is to use loops. You can do a lot with just a few lines of code.
Lecture 90-92: Section Challenge, Solution, and Quiz
This is a whopper of a challenge. The description is 64(!) lines long.
If you haven’t, I really advise you to download the source code for this section. That way you can have the instructions beside you while you work on your solution. You’ll find the instructor’s source code under Section 21 – Bonus Material and Source Code.
In short, we will print a menu, and the user will select an option, invalid options should not be allowed. The other options should let the user add numbers to a list, print them and see various information about the numbers on the list.
I streamed me working on the coding exercises and this challenge on my Twitch channel, https://twitch.tv/oldgrumpygamer. Sadly I had to remove the VOD, but it was much the same as how the instructor did it, except I used a switch statement and took four hours to do all the coding exercises and this challenge.
I also added some code for the extra challenges in there, so I can search the list and clear it. Also added an alternate version of the add function, so you can’t add a number that’s already in the list. If you missed the stream you can find my solution on GitHub.
I also did the quiz for this chapter on stream. Only got 8/10 right this time, I should at least have gotten 9. My tip is to read the code examples in the questions carefully. There might be a trap or two hidden there.
Leave a Reply