Structured Programming2. Sequence |
|
Join the DiscussionMore of this FeatureTutorialsStructured 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 BlockStatements 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 } FunctionA 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 } The next structure type we will look at is branching. Note that this article is copied from the "Ask Felgall" website with the permission of the copyright owner. |

