User Tools

Site Tools


fb_controls

This is an old revision of the document!


Control Structures

Control structures are statements that modify the running state of your program - that is, they turn your program into a state machine and allows it to perform logical decisions. Your program can be in a different state depending on various conditions. For instance, if you're hungry then you are empty, but if you eat, you are full. Or if you're not hungry or empty, then you may be thirsty. Notice how the state can change depending on the conditions that are set. We call these conditionals and they impart intelligent logic into our programs.

In FreeBASIC, as well as other programming languages, there are two types of control structures.

  • Decision Making Structures - makes a series of decisions using if,then,else statements
  • Branching Structures - makes a decision based on an array of conditions using case and case select statements
  • Looping Structures - Performs looping while a condition is a certain state

In this section we will only focus on Decision Making Structures and Branching Structures. In the next section where we discuss loops, we'll delve more into Looping Structures.

It may be tempting to humanize your program and believe that there is a “maybe” state. There are no maybes. A conditional statement can only be either true or false. We call this Boolean Logic.

First some basic theory…

Computers “think” in binary terms. A bit can only be either a 0 or 1, therefore it's running state is absolute. In fact, at the most fundamental level, a computer processor is a series of transistor gates that can only work in a black or white way.

Conditional structures have two types of operators - relational and bitwise. Together we can create very powerful statements that represent a wide variety of computational logic.

Relational Operators

OperatorPurpose
= Equal
<> Not equal
< Less than
> Greater than
Less than or equal to
>= Greater than or equal to

Bitwise Operators

Operator
AND
EQV
IMP
OR
NOT
XOR

Don't let bitwise operators intimidate you. For the purpose of tutorial we'll be focusing on the AND and OR operators.

Putting it all together

A usual conditional structure will look as follows:

dim as string myName
 
input "What is your name? ", myName
 
if myName = "Praetor" then
  print "Ave Praetor!"
else
  print "You are not praetor"
end if
fb_controls.1670107973.txt.gz · Last modified: 2022/12/03 22:52 by praetor