====== 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 are something else that is not defined. 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 three 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 ===== ^Operator^Purpose^ | = | 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 Let's break down this example. dim as string myName create a variable called myName as we learned from the previous section input "What is your name? ",myName //input// is a new keyword that asks the user for input and stores it in a variable, myName in this case. if myName = "Praetor" then print "Ave Praetor!" **if** myName is "Praetor" **then**, perform some function. In this case, print "Ave Praetor" to the screen else print "You are not praetor" end if **if** myName is anything **else** besides "Praetor", perform another function. This time print "You are not praetor" to the screen and **end** the statement. But notice if you tell your program your name is "praetor" instead of "Praetor". It will tell you that your name is not Praetor! But why? The "=" operator in FreeBASIC matches strings as **string literals**. This is different behavior from some other programming languages. So it will only respond when myName is exactly 'Praetor'. While there are many ways to solve this, the easiest for our purposes is to make myName all upper-case, like so if ucase(myName) = "PRAETOR" then... But what if you want it to match it exactly? This requires **regular expressions** which matches a string against a format. Regular expressions are out of the scope of this beginner's tutorial, but there are plenty of resources you can turn to. What if you have many things you wish to test? Well you can do that with the **elseif** statement, like so: if Ucase(myName) = "PRAETOR" then print "Ave Praetor!" elseif Ucase(myName = "SMJ") then print "Howdy SMJ!" else print "I don't know you" end if You can have as many **elseif** statements as you wish, and while you don't necessarily need an **else** statement, it is considered good form to have one to catch any unknowns a user may have. You'll see why when we discuss functions and subroutines. We can also use bitwise operators like **AND**, or **OR** to test multiple things as well. Like such. if Ucase(myname) = "PRAETOR" or "SMJ" then print "I know you!" ... **OR** states that if myName is either PRAETOR or SMJ, then the condition is true.