User Tools

Site Tools


fb_controls

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
fb_controls [2022/12/03 18:46] – [Putting it all together] praetorfb_controls [2022/12/04 01:27] (current) – [Bitwise Operators] 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** 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 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,then,else** statements   * Decision Making Structures - makes a series of decisions using **if,then,else** statements
Line 27: Line 27:
 | >= | Greater than or equal to | | >= | Greater than or equal to |
  
-==== Bitwise Operators ====+===== Bitwise Operators =====
 ^Operator^ ^Operator^
 | AND | | AND |
Line 54: Line 54:
 </code> </code>
  
 +Let's break down this example.
 +
 +<code freebasic>
 +dim as string myName 
 +</code>
 +create a variable called myName as we learned from the previous section
 +
 +<code freebasic>
 +input "What is your name? ",myName
 +</code>
 +//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 = "Praetor" then
 +  print "Ave Praetor!"
 +</code>
 +**if** myName is "Praetor" **then**, perform some function. In this case, print "Ave Praetor" to the screen
 +
 +<code freebasic>
 +else
 +  print "You are not praetor"
 +end if
 +</code>
 +**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
 +
 +<code freebasic>
 +if ucase(myName) = "PRAETOR" then...
 +</code>
 +
 +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:
 +
 +<code freebasic>
 +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
 +</code>
 +
 +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) = "PRAETOR" or "SMJ" then
 +   print "I know you!"
 +...
 +</code>
 +
 +**OR** states that if myName is either PRAETOR or SMJ, then the condition is true.  
fb_controls.1670093165.txt.gz · Last modified: 2022/12/03 18:46 by praetor