Structured Programming

This article describes different ways of what is called as structured programming.

At the very simplest level programming consists of issuing a sequence of commands to a computer to achieve an objective. Once you understand the concept of structured programming and how a particular programming language implements them you can then write a program in that language.

Sequences of Instructions

Here the program flows from one step to the next in strict sequence.

not available

Example: A sample code for adding numbers would look something like the following.Start AddNumbers
    Number1=100
    Number2=20
    Sum=Number1 + Number2
End

Conditional Branching

This is the most used part of structured programming. Here the program reaches a decision point; if the result of the test is true then the program performs instructions in Path 1, and if false it performs the actions in Path 2. This is also known as a conditional construct because the program flow is dependent on the result of a test condition.

not available

Example: A sample code for identifying the higher number of the two would look something like the following.Start GreaterNumber
    Number1=100
    Number2=20
    If Number1 > Number2 Then
        Message "Number1 is greater than Number2"
    Else
        Message "Number2 is greater than Number1"
    End If
End

Conditional Looping

In this construct the program steps are repeated continuously until some test condition is reached, at which point control then flows past the loop into the next piece of program logic.



not available

Example: A sample code for adding numbers from 1 to 100 would look something like the followingStart SumOfNumbers
    CurrentNumber=1
    Loop Till CurrentNumber > 100
        SumOfNumbers= SumOfNumbers + CurrentNumber
        CurrentNumber= CurrentNumber + 1
    End Loop
End

Shared Procedure

Here the program performs an identical sequence of actions several times. For convenience these common actions are placed in a procedure, which is a kind of mini-program and can be executed from within the main program. Such procedures area also called sub-routines or functions. This is the most important part of structured programming as it helps reduces code repetitions.

not available

Example: A sample code for checking if a file exists would look something like the following. Notice that Check1 and Check2 both use the shared procedure FileCheck.Start Check1
    Filename="c:\temp\andy.txt"
    FileCheck Filename
End

Start FileCheck (File)
    … Code to check if file in present on the system
    If FileExists then
        Message "File is present"
    Else
        Message "File not found"
    End if
End

Start Check2
    Filename2="c:\temp\john.txt"
    FileCheck Filename2
End
Don't worry if all this seems a bit confusing; read it for a second time and move on. Things will get clearer as you learn more. And remember the sample codes given above are only for illustrative purposes. The actual syntax of coding differs for every programming language, but the fundamental structures remain the same.

Back to Visual Basic for Beginners