<< Chapter < Page
  Teaching beginners to code     Page 3 / 9
Chapter >> Page >

Thus, the general requirement for the sequence structure is that one or more actions may be performed in sequence after entry and before exit.

There may not be any branches or loops between the entry and the exit.

All actions must be taken in sequence.

The action elements themselves may be structures

However, it is important to note that one or more of the action elements may themselves be sequence, selection, or loop structures.

If each of the structures that make up the sequence has only one entry point and one exit point, each such structure can be viewed as a single action elementin a sequence of actions.

The sequence structure is the simplest of the three, and there's not much more that I can say about it.

The selection structure

The selection or decision structure can be described as shown in the pseudocode in Image 2 .

Image 2. the selection structure in pseudocode.

Enter Test a condition for true or falseOn true Take one or more actions in sequenceOn false Take none, one, or more actions in sequenceExit
Image 2. The selection structure in pseudocode.

Test a condition for true or false

Once again, there is only one entry point and one exit point.

The first thing that happens following entry is that some condition is tested for true or false .

The concept of something being true or false is commonly referred to as a boolean condition in computer programming (named after George Boole) .

If the condition is true, one or more actions are taken in sequence and control exits the structure.

If the condition is false, none , one or more different actions are taken in sequence and control exits the structure. (Note the inclusion of the word none here.)

The action elements themselves may be structures

Once again, each of the action elements in the sequence may be another sequence, selection, or loop structure.

Eventually all of the actions for a chosen branch will be completed in sequence and control will exit the structure.

Sometimes no action is required on false

It is often the case that no action is required when the test returns false. In that case, control simply exits the structure without performing any actions.

The loop structure

The loop or iteration structure can be described as shown in the pseudocode in Image 3 .

Image 3. the loop structure in pseudocode.

Enter Test a condition for true or falseExit on false On truePerform one or more actions in sequence. Go back and test the condition again
Image 3. The loop structure in pseudocode.

As before, there is only one entry point and one exit point. Note that in this case, the exit point is not at the end of the pseudocode. Instead,it follows the test.

Perform the test and exit on false

The first thing that happens following entry is that a condition is tested for true or false.

If the test returns false, control simply exits the structure without taking any action at all.

Perform some actions and repeat the test on true

If the test returns true:

  • One or more actions are performed in sequence.
  • The condition is tested again and the process is repeated.

During each iteration, if the test returns false, control exits the structure. If the test returns true, the entire process is repeated.

Each action element may be another structure

Each of the action elements may be implemented by another sequence, selection, or loop structure.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask