Difference between revisions of "Structured Programming"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Selection)
(Repetition)
Line 44: Line 44:
 
  END SWITCH
 
  END SWITCH
  
=Repetition=
+
=Iteration / Repetition=
 +
Iteration means repetition. Most high level languages have three iterative structures:
 +
 
 +
===Unknown number of iterations===
 +
WHILE condition
 +
    statements
 +
ENDWHILE
 +
 
 +
===Given number of iterations===
 +
FOR x = n to m
 +
    statements
 +
ENDFOR
 +
 
 +
===Unknown number of iterations, but definitely once===
 +
REPEAT
 +
    statements
 +
UNTIL condition
 +
 
 +
=Structured Programming=
 +
Structured programming is only possible because the structures are built in to programming languages like Python, Java, VB and C#.
 +
 
 +
Early programming languages had no iterative statements and a selection statement could only be written like this:
 +
 
 +
IF (condition) GO TO label
 +
 
 +
This made it difficult to follow as your program jumped from one place to another.
 +
 
 +
An example of this approach is:
 +
 
 +
    n = 1
 +
LABEL1
 +
    a = USERINPUT
 +
    b = a * a
 +
    OUTPUT “Square = ”, b
 +
    n = n + 1
 +
    IF n < 5 GOTO LABEL1

Revision as of 14:32, 17 December 2018

In a high-level, procedural language such as Pascal, Visual Basic, Python or C#, an algorithm can be implemented using just three basic structures:

  • Sequence
  • Selection
  • Iteration

Sequence

A sequence consists of one or more statements following one after the other (all programs use sequence, you write them in a specific order).

Here is an example written in pseudocode:

hoursWorked = USERINPUT
hourlyRate = USERINPUT
totalCharge = hoursWorked * hourlyRate
OUTPUT “Total charge: ”, totalCharge

Selection

Selection is implemented using an IF…THEN…ELSE statement, it essentially makes a choice between one path or another.

Here is a pseudocode example:

IF sunIsShining THEN
   takeSunhat
ELSE
   takeUmbrella
ENDIF

The IF statement can also use ELSE IF to create more complex selection statements.

Here is a pseudocode example:

IF sunIsShining THEN
   takeSunhat
ELSE if Raining
   takeUmbrella
ELSE
   takeCoat
ENDIF

Some languages also have a CASE statement (switch case in C#). This allows you to replicate complex IF statements in a more straight forward way.

Here is a pseudocode example:

SWITCH (sun)
   CASE Shining:
      TakeSunHat
   CASE NotShining
      TakeUmbrella
END SWITCH

Iteration / Repetition

Iteration means repetition. Most high level languages have three iterative structures:

Unknown number of iterations

WHILE condition	
   statements
ENDWHILE

Given number of iterations

FOR x = n to m
   statements
ENDFOR

Unknown number of iterations, but definitely once

REPEAT
   statements
UNTIL condition

Structured Programming

Structured programming is only possible because the structures are built in to programming languages like Python, Java, VB and C#.

Early programming languages had no iterative statements and a selection statement could only be written like this:

IF (condition) GO TO label

This made it difficult to follow as your program jumped from one place to another.

An example of this approach is:

    n = 1
LABEL1
    a = USERINPUT
    b = a * a
    OUTPUT “Square = ”, b
    n = n + 1
    IF n < 5 GOTO LABEL1