fb_controls
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
fb_controls [2022/12/03 18:39] – praetor | fb_controls [2022/12/04 01:26] – praetor | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Control Structures ====== | ====== Control Structures ====== | ||
- | Control structures are statements that modify the running state of your program - that is, they turn your program into a **state machine**. 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. | + | Control structures are statements that modify the running state of your program - that is, they turn your program into a **state machine** |
- | In FreeBASIC, as well as other programming languages, there are two types of control structures. | + | 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, | * Decision Making Structures - makes a series of decisions using **if, | ||
Line 42: | Line 42: | ||
<code freebasic> | <code freebasic> | ||
- | dim as integer num | + | dim as string myName |
+ | |||
+ | input "What is your name? ", myName | ||
+ | |||
+ | if myName = " | ||
+ | print "Ave Praetor!" | ||
+ | else | ||
+ | print "You are not praetor" | ||
+ | end if | ||
- | input " | + | </ |
- | if num > 10 then | + | Let's break down this example. |
- | print "Your number | + | |
+ | <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 | ||
+ | |||
+ | <code freebasic> | ||
else | else | ||
- | print "Your number | + | print "You are not praetor" |
+ | end if | ||
+ | </ | ||
+ | **if** myName | ||
+ | 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