Boolean expressions are evaluated similarly to the way arithmetic expressions are evaluated.
Most modern compilers have a bool
type having the values true
and false
.
You can write a function so that it returns a value of true
or false
. A call to such a function can be used as a Boolean expression in an if-else
statement or anywhere else that a Boolean expression is permitted.
One approach to solving a task or subtask is to write down conditions and corresponding actions that need to be taken under each condition. This can be implemented in C++ as a multiway if-else
statement.
A switch
statement is a good way to implement a menu for the user of your program.
A block is a compound statement that contains variable declarations. The variables declared in a block are local to the block. Among other uses, blocks can be used for the action in one branch of a multiway branch statement, such as a multiway if-else
statement.
A for
loop can be used to obtain the equivalent of the instruction “repeat the loop body n
times.”
There are four commonly used methods for terminating an input loop: list headed by size, ask before iterating, list ended with a sentinel value, and running out of input.
It is usually best to design loops in pseudocode that does not specify a choice of C++ looping mechanism. Once the algorithm has been designed, the choice of which C++ loop statement to use is usually clear.
One way to simplify your reasoning about nested loops is to make the loop body a function call.
Always check loops to be sure that the variables used by the loop are properly initialized before the loop begins.
Always check loops to be certain they are not iterated one too many or one too few times.
When debugging loops, it helps to trace key variables in the loop body.
If a program or algorithm is very difficult to understand or performs very poorly, do not try to fix it. Instead, throw it away and start over.