Está en la página 1de 8

Structured Programming

Structured programming is one of the several different ways in which a programming language can be constructed. It was originally introduced as a means of getting away from the 'spaghetti' code that was used in the early days and to provide some means by which programmers could more easily follow code written by other programmers. Structured programming is a procedure oriented method of designing and coding a program. A structured programming language does not allow just things to happen in any order within the code. There are a limited number of constructs that can be used within the code to define the execution flow. A structured program is one that only uses the constructs that are listed within this document. Most so called structured (or procedural) programming languages also permit constructs to be used that are not listed in this document. These additional constructs are to be avoided if a true structured program is to be produced. A structured program may be written out using pseudo code prior to being translated into whatever programming language that the program is to be written in. This pseudo code forms part of the program specification and is readable by anyone who understands structured programming regardless of whether or not they know the specific language in which the program has been written.

Sequence
Structured programming provides a number of constructs that are used to define the sequence in which the program statements are to be executed. Consecutive

Statements within a structured program are normally executed in the same sequence as they are listed within source code. If a code fragment consists of three statements following one another then statement one will execute first, statement two second, and statement three last. To change from this straight consecutive execution sequence requires the use of one of the other structured programming constructs which are described below.

pseudo code

statement-1 statement-2 statement-3


example

input a b = 5 + 2 * a print b

Block Statements may be blocked together. A block of statements may be substituted wherever a single statement is allowed. The symbol or keyword used to indicate the start and end of each block differs depending on the programming language used.
pseudo code

statement-1 statement-2 statement-3

Subroutine

A subroutine is a code segment that has been separated from the preceding and following code. A subroutine usually consists of a series of statements that perform a particular task. The task performed is usually identified by the name given to the subroutine. Once a subroutine has been defined it can then be called from one or more places within the program. This allows a program to perform the same task a number of times without having to repeat the same code. A single call statement replaces (stands in for) all of the statements contained within the subroutine. Parameters can be passed to a subroutine which will supply the data required to perform the task and perhaps to return values for use by the subsequent processing.

A subroutine can either be compiled with (internal to) the calling program or separately (external).
pseudo code

call subroutine-1(var1, var2) subroutine-1(var1, var2) { statement-1 statement-2 }


example

call output(a, b) output(a, b) { print a print b }

Function A function is similar to a subroutine except that a function always returns a value to the calling program. A function is usually called implicitly by embedding the function call into another statement in place of the returned value rather than having a separate call statement. A function works in the same way as a subroutine except in the way that it is called. A function can be compiled internally or externally. Some programming languages also provide functions built into the language itself.
pseudo code

var2 = function-1(var1) function-1(var1) { statement-1 statement-2 return var2 }


example

b = cube(a)

cube(a) { b = a * a * a return b }

Branching
There are two types of branching statements. The if statement is used for conditional execution of a single statement or to select which of two statements is to be executed. The case statement (sometimes referred to as the select statement) allows for selection of one statement out of three or more that should be executed. If

Statements can be executed conditionally by using an if statement. The if statement specifies a condition which gets tested when the if statement is executed. If the condition is true then the following statement is executed otherwise processing skips that statement. Optionally a second statement can be attached to the if statement that will be executed if the condition is false.
pseudo code

if condition true-statement else false-statement


example

if x > y print "x is bigger than y" else print "x is not bigger than y"

Case

A case or select statement allows for one of a number of statements to be executed depending on the value of a field. There is usually also an additional statement which is executed if none of the specified values is matched.
pseudo code

case fieldname value1: statement-1 value2: statement-2 value3: statement-3 otherwise: other-statement
example

case size 1: print "small" 2: print "medium" 3: print "large" otherwise: print "unknown"

Loops

Loops allow for the same statement to be executed a number of times in succession. There are three different loop constructs that can be used depending on whether the number of repetitions is known and also (where the number of repetitions is not known and is dependent on a condition) whether the loop is allowed to be bypassed if the termination condition is met before the loop is first executed. For

A for loop allows a statement to be executed a specified number of times. The for loop begins with a loop control variable assigned a specific initial value. This control variable in then incremented (or decremented) by a specified amount each time around the loop until a specified terminating value is reached at which time the statement following the loop is then executed.
pseudo code

for (initial-value, final-value, increment) statement-1


example

for (a = 3, a > 12, a = a + 2) print a

This example will output 3 5 7 9 11. While

A while loop allows a statement to be executed until a given condition is met. If the condition is met prior to executing the loop then the loop will not be executed. As soon as the condition is met, execution continues with the statement following the loop.
pseudo code

while not condition statement-1


example

while not end-of-file { read record write record }

Until

An until loop also allows a statement to be executed until a given condition is met but the condition will not be tested until after the loop has been executed once. Once the condition is met the statement following the loop will be executed.
pseudo code

do statement-1 until condition

También podría gustarte