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 InstructionsHere the program flows from one step to the next in strict sequence.
Example: A sample code for adding numbers would look something like the following.
Sum=Number1 + Number2
Conditional BranchingThis 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.
Example: A sample code for identifying the higher number of the two would look something like the following.
If Number1 > Number2 Then
Message "Number1 is greater than Number2"
Message "Number2 is greater than Number1"
Conditional LoopingIn 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.
Example: A sample code for adding numbers from 1 to 100 would look something like the following
Loop Till CurrentNumber > 100
SumOfNumbers= SumOfNumbers + CurrentNumber
CurrentNumber= CurrentNumber + 1
Shared ProcedureHere 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.
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 Check1Don'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.
Start FileCheck (File)
… Code to check if file in present on the system
If FileExists then
Message "File is present"
Message "File not found"
Back to Visual Basic for Beginners