We have learned a bit about variables and about logical expressions. So far all of our programs were a sequence of statements to be run in the order written. Such programs are very predictable. It becomes a lot more fun when statements can be run in different orders.
There are a handleful of constructions that allow statements to be run other than straight line. My favorite is the "while" statement. It has this form:
[the keyword while] [a logical expression] [a block of code]The precise syntax will have the logical expression enclosed in parenthesis and the block of code enclosed in curly braces.
» Try the example code in the window and see what it does.
The meaning of the while statement is that it runs the block of code repeatedly until the logical expression is no longer true. When the while statement is encountered the first time, the logical expression is evaluated. If the expression evaluates true, the block of code is run. Then, after the block is run, the expression is again evaluated. Maybe this time it will evaluate false, because the value of the expression might have been changed by the code run. If it is still true, the block of code is run again. And so forth.
The expression is evaluated just at the block of code is being considered to be run: the first time, and each time thereafter. When the logical expression is false, the code block is not run, and we continue with the statement after the while construct.
» Modify the program to write the "Boom!" program. This program first prints out "Counting down ..." and then prints out the numbers descending from 10 to 1. After it gets to one it prints "Boom!" and exits.
This is a pretty decent program, all things considered!
Oh by the way, please use good programming style. This means that you should indent the block of your while loop so that it is easy to see what is in the block and what is not in the block. You might want to express your individuality in your choice of indentation. But, you might also rather just adopt a standard indentation style.
For these pages, I have adopted what they call (today) Allman style, which puts the braces on separate lines, at the indentation equal to the while keyword. (Normally I follow the One True Brace Style, but here I am more interested in readability). I have also adopted a 3 space indentation rule, which shows I "have flair", or a nervous twitch. Four spaces is the norm.
Furthermore, choosing variable names is also part of good programming style. I tend to use i, j, k, for variables with control the loop. I don't use a nice name such as "the_variable_that_controls_the_loop" or "this_is_the_number_to_print_counting_down" because with such a reusable, non-descriptive name I am promising that the use of the variable will be so local that its entire use will be in your eye and you can therefore ignore the name. If I give a variable a special name I am promising that you should perhaps pay attention to the name.
This is where the discussion goes. It's a long box underneath both the other boxes.