0% found this document useful (0 votes)
162 views

Structured Programming

Structured programming is a technique that uses three basic constructs: sequence, repetition, and selection. This avoids using goto statements and makes code easier to read and maintain. Extensions were later added like for loops and if/else statements to make programs more readable. Structured programming breaks programs down into simple blocks that are connected through these basic constructs.

Uploaded by

Emmanuel Tuffet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views

Structured Programming

Structured programming is a technique that uses three basic constructs: sequence, repetition, and selection. This avoids using goto statements and makes code easier to read and maintain. Extensions were later added like for loops and if/else statements to make programs more readable. Structured programming breaks programs down into simple blocks that are connected through these basic constructs.

Uploaded by

Emmanuel Tuffet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

To Documents

Structured Programming
Background

Since the invention by Von Neumann of the stored program computer, computer
scientists have known that a tremendous power of computing equipment was the
ability to alter its behavior, depending on the input data. Calculating machines had, for
some time, been able to perform fixed arithmetic operations on data, but the potential
of machines capable of making decisions opened up many new possibilities. Machines
that could make decisions were capable of sorting records, tabulating and
summarizing data, searching for information, and many more advanced operations
that could not even be imagined at the time.

In early programming languages, like Fortran (first invented in 1954) and various low
level machine languages, the goto statement allowed the computer to deviate from
the sequential execution of the program instructions. The goto statement was
recognized to be a very powerful construction, and soon, programs of increasing
complexity and power were developed.

However, the increasingly complex code that resulted from goto statements became
harder and harder to maintain. Dijkstra, in 1966, was one of the first persons to
recognize that this run away complexity of programs was due to the overuse of the
goto statement (Dijkstra, E. W., "Go To Considered Harmful," Communications of the
ACM, March 1966). In fact, it was determined shortly thereafter, that the goto
statement is not needed at all. Dijkstra showed that any program construction that
could be created with goto statements could be created more simply with the
sequence, repetition and decision constructions that are discussed in the following
sections. This was the birth of the discipline of Structured Programming.

Structured Programming in Everyday Life

1. Sequence Execute a list of statements in order.

Example: Baking Bread

Add flour.
Add salt.
Add yeast.
Mix.
Add water.
Knead.
Let rise.
Bake.

2. Repetition Repeat a block of statements while a condition is true.

Example: Washing Dishes

Stack dishes by sink.


Fill sink with hot soapy water.
While moreDishes
Get dish from counter,
Wash dish,
Put dish in drain rack.
End While
Wipe off counter.
Rinse out sink.

3. Selection Choose at most one action from several alternative conditions.

Example: Sorting Mail

Get mail from mailbox.


Put mail on table.
While moreMailToSort
Get piece of mail from table.
If pieceIsPersonal Then
Read it.
ElseIf pieceIsMagazine Then
Put in magazine rack.
ElseIf pieceIsBill Then
Pay it,
ElseIf pieceIsJunkMail Then
Throw in wastebasket.
End If
End While

Structured Programming in Visual Basic

Structured programming is a program written with only the structured programming


constructions: (1) sequence, (2) repetition, and (3) selection.

1. Sequence. Lines or blocks of code are written and executed in sequential order.

Example:
x = 5
y = 11
z = x + y
WriteLine(z)

2. Repetition. Repeat a block of code (Action) while a condition is true. There is


no limit to the number of times that the block can be executed.

While condition
action
End While

Example:

x = 2
While x < 100
WriteLine(x)
x = x * x
End

3. Selection. Execute a block of code (Action) if a condition is true. The block of


code is executed at most once.

If condition Then
action
End If

Example:

x = ReadLine()
If x Mod 2 = 0
WriteLine("The number is even.")
End If

Extensions to Structured Programming

To make programs easier to read, some additional constructs were added to the basic
three original structured programming constructs:

1. Definite Repetition (For Loop) Combine initialization, checking a condition,


and incrementing a counter in a single statement called a for statement. Here is
the general form:

For i = 1 To n Step k ' Step k is optional


action
Next

Example:

For i = 1 To 20
WriteLine(i)
Next i

Example:

For i = 20 To 1 Step -1
WriteLine(i)
Next

2. If-Then-Else Statements Execute the first action whose corresponding


condition is true. Here is the general form:

If condition1 Then
action1
ElseIf condition2 Then
action2
ElseIf condition3 Then
action3
Else
defaultAction
End If

Example:

If n = 1 Then
WriteLine("One")
ElseIf n = 2 Then
WriteLine("Two")
ElseIf n = 3 Then
WriteLine("Three")
Else
WriteLine("Many")
End If

3. Select Statement Execute the action corresponding to the value of the


expression.

Select Case value1


Case value1
action1
Case value2
action2
Case value3
action3
Case Else
defaultAction
End Select

Example:

Select Case n
Case 1
WriteLine("One")
Case 2
WriteLine("Two")
Case 3
WriteLine("Three")
Case Else
WriteLine("Many")
End Select

You might also like