Is trying to program making you loopy...?
So we've discussed two of the main building blocks of programming: Data and Control Structures.
With those, you can do things once for every time your program runs. Well what if you want to do more than once and done?
Loops.
As I've mentioned before, there are two basic kinds of loops. One kind runs a particular number of times that the program 'knows' in advance. The other type runs until some condition becomes true.
That first kind is a For loop. Here is an example of how you would use one...Suppose you wanted to print out the numbers from one to one hundred. The pseudo-code:
With those, you can do things once for every time your program runs. Well what if you want to do more than once and done?
Loops.
As I've mentioned before, there are two basic kinds of loops. One kind runs a particular number of times that the program 'knows' in advance. The other type runs until some condition becomes true.
That first kind is a For loop. Here is an example of how you would use one...Suppose you wanted to print out the numbers from one to one hundred. The pseudo-code:
- For the numbers 1 to 100
- Print the number
Sort of a trivial use though. How about if we want to also print out whether the number is even or odd:
- For the numbers 1 to 100
- If the number is even
- Print the number and the word even
- Else
- Print the number and the word odd
Did you just feel a twinge of excitement? The pieces are starting to come together...
Let's put that aside for now. What if you didn't know the starting and ending numbers and you had to ask the user?
- Ask the user for the starting number and store it in a variable called start_number
- Ask the user for the ending number and store it in a variable called end_number
- For the numbers from start_number to end_number
- If the number is even
- Print the number and the word even
- Else
- Print the number and the word odd
See, For loops are what you use when the program knows the starting and ending values. You (the programmer) don't need to know as long as just before the program gets to the beginning of the loop - the computer knows.
And what about while loops then? Well, those are for when you want to repeat a block of code an unknown number of times. For example, we want the program to ask you the names of everyone that you know and print out 'Say hello to <person name>' until you enter a blank name. To do this we'll use a variable to hold whether or not it should continue looping. Also, we will have to set it to 'true' before we start - otherwise the loop will not start. Here's the pseudo-code:
- Create the control variable (called 'continue') and set it to true
- While 'continue'
- Ask the user for a person's name
- If the name is blank
- Set 'continue' to false
- Else
- Print 'Say hello to ' and the name
Now, While loops can come in two version but different languages have either one or both of two variations. The first kind checks the condition at the beginning of the loop and the other kind checks the condition at the end of the loop. When the check is done at the beginning it is usually called a While-Do. As in 'while something is true run the block of code'. With a While-Do it is possible to not actually run the code in the block even once - just have the condition be false before you get to the start of the loop! That is why we had to set our control variable to true up above.
In this example, that is a bunch of extra work. What if we didn't want to do the extra work?
Well, if the programming language that you are using has the other version of the While loop - the Do-While. That makes this example work out a bit cleaner:
- Do
- Ask the user for a person's name
- If the name is not blank
- Print 'Say hello to' and the name
- While name is not blank
So much nicer! We don't have to keep a controlling variable and make sure it's set to the right value. We don't have to have a separate If-Then to test the condition that decides whether the loop should be continuing. We just have the block of code that we want to run and the controlling condition is tested by the loop rather than by us!
These programs that we have been writing pseudo-code for up until now are kind of silly. And that is intentional. We're not really solving problems - we're learning about the building blocks and practicing thinking about how to write algorithms in pseudo-code.
The last building block is Data Structures. Like I've said, there are entire books written about data structures. So for now we're only going to talk about the first and most basic one - the Array. But before we talk about them, we're going to have some practice with creating an algorithm to solve a problem that is often used to figure out whether people know how to create algorithms.
Next up - FizzBuzz!
Comments
Post a Comment