Reference: Program Structure

Excerpted from an email by Jenn Vesperman to the newchix mailing list.

In response to an email talking about the difficulty of forming an initial program design:


Ok. To start with, simplify your tool set. Yes, eventually you would be
more efficient if you used the full range of syntactic tools. But for
the moment.. don't worry about it. 

Trim your kit of loops to 'for' and 'while'. 
Trim your kit of conditionals to 'if - then - else - end'. 

With the full range available to you, you're standing at a banquet,
starving and unable to decide what to eat first. So start small.


The next step is to analyse the problem, to try to figure out first 
WHAT you are trying to do, then HOW you want to do it. This is the
essence of programming - everything else is gravy. Seriously.


Example analysis:

Let's say we're trying to make a program to calculate and display the
first 20 elements in the fibonacci sequence. The sequence goes:
1 1 2 3 5 8 13 21

It starts with two 1s, then each item in the sequence is calculated by
adding the previous two items.


That last sentence tells me that I want something in the core of my code
that looks like:

    next_element = element_a + element_b;
    print(next_element);

(For tidiness, I should probably print a space, too.)
Then I want something to set up for the next element:

    element_a = element_b;
    element_b = next_element;


So the core code looks like:

    next_element = element_a + element_b;
    print(next_element);
    print(" ");
    element_a = element_b;
    element_b = next_element;


So.. that's how I calculate any -one- fibionacci number (after the two
starting 1s), and how I juggle my variables so I'm set up for
calculating the number after it.


Now.. how do I loop it? I know I need to do this again and again, but
how?

With our cut-down list of options, it's either a 'for' loop or a 'while'
loop. It doesn't really matter which loop you use. It doesn't even
REALLY matter if you just cut and paste this down the page the right
number of times. Loops are just easier to read and change. :)

Assuming we don't want the cut-and-paste option, let's try a while loop.

I said we'd do this twenty times, so:

counter = 1;
while (counter <=20) {
    counter = counter + 1;
}


That (if we paste the other code in) would almost work.. except that we
haven't initialised element_a or element_b. So..


element_a = 1;
element_b = 1;
counter = 1;
while (counter <=20) {
    next_element = element_a + element_b;
    print(next_element);
    print(" ");
    element_a = element_b;
    element_b = next_element;
    counter = counter + 1;
}


... almost there. But we have to deal with the problem of the two
initial ones. That loop would print the twenty elements of the
fibionacci sequence starting from the 2.

Try:

element_a = 1;
element_b = 1;
print("1 1 ");
counter = 3;
while (counter <=20) {
    next_element = element_a + element_b;
    print(next_element);
    print(" ");
    element_a = element_b;
    element_b = next_element;
    counter = counter + 1;
}



It would be just as correct to replace the while loop with a for loop. 
They would mean the same thing.

for (counter = 3; counter <= 20; counter = counter + 1) {

}



I hope this example of figuring out HOW helps...