fb_controls
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
fb_controls [2022/12/03 23:27] – [Putting it all together] praetor | fb_controls [2022/12/04 01:27] (current) – [Bitwise Operators] praetor | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 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, | ||
+ | * 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 " | ||
+ | |||
+ | First some basic theory... | ||
+ | |||
+ | Computers " | ||
+ | |||
+ | 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: | ||
+ | |||
+ | <code freebasic> | ||
+ | dim as string myName | ||
+ | |||
+ | input "What is your name? ", myName | ||
+ | |||
+ | if myName = " | ||
+ | print "Ave Praetor!" | ||
+ | else | ||
+ | print "You are not praetor" | ||
+ | end if | ||
+ | |||
+ | </ | ||
+ | |||
+ | Let's break down this example. | ||
+ | |||
+ | <code freebasic> | ||
+ | dim as string myName | ||
+ | </ | ||
+ | create a variable called myName as we learned from the previous section | ||
+ | |||
+ | <code freebasic> | ||
+ | input "What is your name? ", | ||
+ | </ | ||
+ | //input// is a new keyword that asks the user for input and stores it in a variable, myName in this case. | ||
+ | |||
+ | <code freebasic> | ||
+ | if myName = " | ||
+ | print "Ave Praetor!" | ||
+ | </ | ||
+ | **if** myName is " | ||
+ | |||
+ | <code freebasic> | ||
+ | else | ||
+ | print "You are not praetor" | ||
+ | end if | ||
+ | </ | ||
+ | **if** myName is anything **else** besides " | ||
+ | |||
+ | But notice if you tell your program your name is " | ||
+ | |||
+ | <code freebasic> | ||
+ | if ucase(myName) = " | ||
+ | </ | ||
+ | |||
+ | 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' | ||
+ | |||
+ | What if you have many things you wish to test? Well you can do that with the **elseif** statement, like so: | ||
+ | |||
+ | <code freebasic> | ||
+ | if Ucase(myName) = " | ||
+ | print "Ave Praetor!" | ||
+ | elseif Ucase(myName = " | ||
+ | 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. | ||
+ | |||
+ | <code freebasic> | ||
+ | if Ucase(myname) = " | ||
+ | print "I know you!" | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | **OR** states that if myName is either PRAETOR or SMJ, then the condition is true. |
fb_controls.txt · Last modified: 2022/12/04 01:27 by praetor