Writing control structures in pseudocode

The simplest control structure is the sequence. You have been writing pseudocode with those since the very first design. But you need to know how to write other control structures in pseudocode. Not all designs are just sequences! To show that you are creating an if/selection/branch structure, your pseudocode should look something like:

	4.0  if the input value is > 0
		4.1  output message "positive"
		4.2  set pflag to true
		4.3  add input value to ptotal
		4.4  output message "negative"
		4.5  set pflat to false
		4.6  add input value to ntotal

An if structure that didn't need an else would look like:

	3.0  if leg1 + leg2 < hyp 
		3.1  output "a triangle"
		3.2  area is leg1 * leg2 / 2

The indentation is really necessary to show to a quick glance that these two steps are depending on the step above them.

A longer example:

	1.0  set correct counter to 0
	2.0  get two random numbers from 1 to 10
	3.0  display the random numbers to the user as an addition problem
	4.0  input an answer from the user
	5.0  if answer is correct
		5.1  output correct message
		5.2  add 1 to correct counter
		5.3  tell the user incorrect message
		5.4  give user another chance at getting the answer
		(which would have more detail at a deeper level, 5.4.1...)
	6.0  tell user goodbye

If you wanted to repeat steps in a loop control structure, it would look like this:

	1.0  set try counter to 1
	2.0  get a random number from 1 to 50
	3.0  ask the user for a guess for the random number
	4.0  input a guess from the user
	5.0  while the guess is not correct
		5.1  tell the user it's not right
		5.2  add one to try counter
                5.3  if guess is less than the random number  
                     5.3.1  tell the user "you're low"
                5.4  else
                     5.4.1  tell the user "you're high"
		5.5  ask for another guess
	6.0  output the try counter "You took " this many " tries"

The way that 5.1 through 5.5 are indented show that they are in the body of the loop.

Another example of a loop

     1.0 ask user "how many generations to simulate?" store in numgens
     2.0 ask for file containing initial generation
     3.0 use numgens to control a loop (as final value)
            3.1  find new generation from old generation
            3.2  display new generation
            3.3  copy new generation to old generation
            3.4  add one to loop control variable
     4.0 say goodbye