4. Building Blocks Revisited


"Is everything all right, sir?"
"No. No, it's not. Some smegger's filled out this 'Have You Got a Good Memory' quiz."
"Why, that was you, sir. Don't you remember?"
--Kryten and Dave Lister, Red Dwarf

Before we start with functions in the next section, we're going to quickly tie this in with that very important thing to remember back at the beginning of the guide. Now what was it...oh, well, I guess I gave it away with this section title, but let's keep talking as if that didn't happen.

Yes, it was basic building blocks, and how you take a specification and turn it into little bite-sized pieces that you can easily translate into blocks of code. I told you to take it on faith that I'd tell you some of the basic pieces, and I'm just reminding you here, in case you didn't notice, that all those statements back there are little basic building blocks that you can use in your programs.

Such as, if the specification reads:

Assignment: Write a program that repeatedly accepts user input and then prints the numbers between 0 and the entered number. If the user enters a number less than or equal to zero, the program will exit.

You now have enough information to figure out all the basic building blocks to make this program happen. You'll have to steal scanf() and printf() usage from previous examples, but the rest of the parts correspond to various statements you've seen in the previous section.

Note that off the top of my head, I can think of many many ways to implement this assignment. If you come up with one that works, good for you! Sure, it might not be the best, but what is "best", anyway? (Well, it turns out best is defined as what your professor or boss thinks is best, but let's be happily theoretical for the moment. Ahhh.)

So! Do some breakdown of the above assignment, and come up with the basic structure you'd use. In fact, go ahead and code it up, and try it out!