-
Book Overview & Buying
-
Table Of Contents
The C++ Workshop
By :
In this chapter, we've learned about program flow and how we can manipulate the flow of execution through our applications. This is fundamental for representing logical systems.
We started by looking at basic if/else statements. These allow us to branch our code based on conditions and are one of the most fundamental ideas in programming. With this branching ability, we're able to replicate logical systems and behaviors by controlling the flow of execution through our application. We then looked at some alternatives to the basic if/else statement, such as switch and ternary statements.
Next, we looked at a number of different loops. We started with while and do while loops; loops that run indefinitely so long as the condition they're checking is true. We then looked at for loops, which run for a set number of iterations. Finally, we looked at range-based loops, which are useful for iterating over collections. We ended by looking at how we can ensure our loops are efficient, ending them early with the break statement, or by skipping iterations with the continue statement.
We put all of these new skills to practice by building a simple game that allowed the user to guess a number that had been randomly selected. We allowed the user to input a number of values in order to set up the game, and then gave them a number of guesses to try to find the number. We employed everything we learned in Chapter 1, Your First C++ Application, as well as if/else statements and a couple of the loops that we looked at in this chapter.
In the next chapter, we're going to take a closer look at the various data types that C++ offers. We'll start by looking at the various built-in types (int, double, char, and so on), moving onto looking at arrays and collections of these types. We'll then move onto ideas such as storage lifetime, scope, classes, and structs. With an understanding of C++ applications in general, controlling the flow of execution, and soon how to represent and store our data in various data types, we're well on our way to a functional understanding of the C++ language.