Control Structures in Programming

An Explanation of the Three Basic Structures Found in Programs

© Dawn Brewer

Feb 14, 2009
Program Code, Mario Alberto Magallanes Trejo
For programmers it is essential to understand the three control structures, sequence, repetition and selection which are the elementary building blocks for all programs.

Beginners can find Programming very confusing, Just like any other subject, there are a lot of new concepts to understand and lots of new terminology to learn. One of the most basic concepts in programming is control structures.

There are three control structures and all programmers must learn what they are and how they can be used. These three basic structures can be used to develop any kind of program and they are explained below.

Sequential Programming

The most straightforward control structure is the sequence. A list of what to do, step by step.

For example:

  • do the first thing
  • do the next thing
  • do another thing

In pseudocode:

  • Declare a variable called count
  • Set count to 1
  • Write out the value of count

Repetition in Programming

Repetition is also called iteration and is used when something is repeated over and over again, so anything where the program goes round in a loop. Typically programmed using code such as WHILE, REPEAT and FOR statements.

For example:

  • Do the first thing
  • While (some condition is TRUE)
    • Do b
    • Do c
  • Do the next thing

In pseudocode:

  • Set count to 0
  • WHILE (there are items in a pile)
    • Increase count by 1
  • Write out the number of items

Another example: to write out numbers 1 to 10

  • FOR count = 1 to 10
    • Write out count
  • Write out ‘finished’

NOTE: a FOR loop is used when it is known exactly how many times to go round the loop

Selection in Programming : Making Decisions

Selection is used to make a decision to go down one path or another, often programmed using code such as an IF statement, or a CASE statement.

For example: To Pack my bag for work)

  • Pack my PC
  • Pack my money
  • If it's raining
    • Pack my coat

In pseudocode:

  • Work out total cost
  • If (total cost > £10) THEN
    • Write out ‘no delivery charge’
  • ELSE
    • Write out ‘£5 delivery charge’
  • Write out ‘thankyou’

Another example using a CASE:

  • Write out the day
  • CASE (day)
    • Monday: write out ‘first working day of the week’
    • Friday: write out ‘last working day of the week’
    • Saturday: write out ‘the first day of the weekend’
    • Sunday: write out ‘the second day of the weekend’
  • ENDCASE

Sometimes there is confusion between the Repetition and Selection statements, as both use a condition. A condition is a test, resulting in true or false. One way of describing the difference is that when making a selection, the condition is only ever tested once. When used in repetition, the condition is tested a number of times, every time the program decides whether to loop.

Beginners who take the time to learn the basic building blocks of programming will find coding in new languages easier and developing computer programs requires an understanding of the three basic control structures outlined above.


The copyright of the article Control Structures in Programming in Computer Programming is owned by Dawn Brewer. Permission to republish Control Structures in Programming in print or online must be granted by the author in writing.


Program Code, Mario Alberto Magallanes Trejo
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Jun 24, 2009 10:30 PM
Guest :
I found this article very useful and still a little confusing because the pseudo codes are what I am not used to.
Sep 1, 2009 10:41 AM
Guest :
i am taking a programming class and you have clarified some areas up for me...thank you!!
2 Comments