Using if, for, and range() and other control flow statements
Let's start with if
statements, which we first discussed in Chapter 4, Understanding Logical Reasoning. These are probably the most commonly used and known statements in algorithm design. You may recall learning about conditional statements in geometry as you studied reasoning and proofs. In those classes, you would write statements in if-then format. Take the following statement:
When it rains, I wear a raincoat.
This is not a conditional statement, at least not yet. If we were going to write it as a conditional sentence, then we'd have to have the if-then format, much like this sentence. Take a look at the converted statement that follows:
If it rains, then I wear a raincoat.
As you can see, we use conditions as part of our everyday lives. We just don't point them out.
When writing algorithms, we have to explicitly state what we need the algorithm to do, so we have to explicitly state these...