Introduction to Control Structures

Controls structures allow you to change the flow of your program, to execute different parts of your program based on certain criteria and to iterate through code blocks. Control structures fall into two categories; decision-making structures such as the If and Select Case statements, and looping structures such as the For-Next and Do-Loop statements.

The decision-making, or branching structures, allow you define criteria based on data values, and then execute code based on those data values. If a variable is less than 5, you can execute one code block, and if the variable is greater than 5, you can execute a different code block, and if the variable is equal to 5, you can execute yet another code block. Being able to make decisions within your program is crucial for implementing the logic that your program describes.

The looping structures are also necessary elements to any program, since you will often need to process groups of information in a loop, such as reading a disk file or getting input from the user. Data manipulation almost always involves looping, since you will need to iterate through data structures to gather specific information that you will need in your program. FreeBasic has a rich set of both decision-making and looping structures.

A Program is a State Machine

You can think of a program as a state machine, where at any given moment, the program is in a particular state, or condition, which changes over time. This may sound a bit cryptic, so an example may help illustrate what this means.

Suppose it is getting close to evening and you realize that you are getting hungry. Call this the hungry state. Your stomach is empty, and your brain is telling you to get some food. You run down to the local hamburger joint, get a nice juicy double cheeseburger and consume it. You are now full, so call this the satisfied state. This is basically how a state machine operates.

At one moment, your stomach is empty and the state is hungry and at the next moment, your stomach is full and the state is satisfied. You started in one state (hungry), executed a branching action (drove to the burger joint) and ended in another state (satisfied). Programs do much the same thing.

A variable is like your stomach. If the variable is empty, the program is in the empty state; that is, there is no meaningful data within the variable, so the program cannot do any meaningful work with that data. Programs are designed to do something, so you need to transition from the empty state to the satisfied state, by executing a branching statement (driving down to the burger joint) in order to initialize the variable with data. Once the variable is initialized (you ate the hamburger) the program changes state and now you can do something meaningful, since you now have meaningful data.

Control structures are the transition mechanisms that enable you to change the state of your program. If you are writing a game, you can use a Do-Loop to wait for user input. Once the user presses a key, you can then use an If or Select Case statement block to act on this key press. The program moves from a waiting state, to an action state, and then back to a waiting state, until another key is pressed or the program terminates. To effectively use control structures within your program, you must keep in mind the various states of your program, both before and after state transitions.