;-------------------------------------------------------------------------------
;
; ;-------------------------------;
; ; Conditions ;
; ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
;
; This part contains two sections.
; You can copy and paste each section
; to a new program to try it out, if you want.
;
;-------------------------------------------------------------------------------
;
; Programming is a lot like talking simple english sometimes.
; Suppose we want to know if we have enough money to buy something.
;
; Say, we have 100 credits (in the year 3207).
;
Money = 100
;
; And we want to buy a new ultramolecular drive.
; Suppose there are 2 versions available,
; the cheap one, and the expensive one :P
;
Cheap_Drive = 50
Expensive_Drive = 150
;
; So how do you find out what we can afford?
; Let's convert english to code - it really works miracles.
; Watch the following lines closely.
;
; 1) First, when can we afford something?
; When we have enough money.
;
; 2) So, if we have more money, then we can afford it.
;
; 3) Furthermore, if the amount of money we have is greater than the price of
; the thing we want to buy, then we can afford it.
;
; 4) If amount of money is greater than value of thing, then we can afford it.
;
; 5) If money is greater than value, then we can afford it.
;
; 6)
If Money > Cheap_Drive Then Print "We can afford it"
;
; Almost correct.
; Thing is, we can also afford it when our amount of money
; is equal to what we want to buy.
;
If Money >= Cheap_Drive Then Print "We can still afford it"
;
; Here's how it works (syntax):
;
; IF (condition) THEN (action)
;
; Let's take a look at the condition first.
; What if we were to stick it in a variable? (say what?)
;
Condition = Money >= Cheap_Drive
Print "Can afford cheap drive: " + Condition
;
Condition = Money >= Expensive_Drive
Print "Can afford expensive drive: " + Condition
;
; Syntax:
;
; Variable = (condition)
;
; In fact, you can stick pretty much anything in a variable :P
;
; What happens is when the result of a comparison is true,
; e.g. if Money is indeed more than Cheap_Drive,
; the resulting value is 1.
;
; When the result of a comparison is false, the value is 0.
;
; I bet you can guess the resulting value of the comparison below.
;
Condition = Money = Money
Print "Our money is our money: " + Condition
;
; What happens here is:
; Money is compared with Money;
;
; Condition = ( Money = Money )
;
; Money is indeed equal to Money, so the resulting value is 1.
;
; Coincidently there are 2 keywords which relate to this.
; They are True, and False, where True equals 1, and False equals 0.
; So when you type:
;
Condition = True
;
; Then Condition contains a value of 1 indeed.
; So True and False match well with comparisons.
;
; To further bedazzle you there's the Not keyword.
; It reverses a comparison.
;
; So
;
Condition = Not False
;
; equals True.
;
; And
;
Condition = Not True
;
; equals False.
;
; Actually, Not will turn any value not equal to zero into False (zero).
;
; Here, take a look at the following code:
;
Condition = 200
If Condition Then Print "We have a condition"
;
; Condition is not equal to 1, but the action is still executed. (eh?)
; The actions will be executed when the resulting Condition is non-zero.
; Again, that means anything not equal to False (zero).
;
; Comparisons generally use one of these: =, <, >, >=, <= or <>.
; Basically the same as in maths.
;
; You can even combine maths with comparisons,
; and there are a lot of various other keywords (Not, And, Or, etc)
; and operators (+, -, /, *, etc) you can use.
;
; You should find a list of all these
; with a lot of more technical details
; in the language reference which can be
; accessed from the first screen you see
; when you start Blitz.
;
; Let's try combining a comparison with some simple maths.
;
Result = Money > Expensive_Drive
If Result + 1 Then Print "We still have a condition"
;
; We don't have enough money for the expensive drive.
; So that would mean False, e.g. 0.
; We add 1 to that (the IF condition), so we get... 1.
; 1 is non-zero so our action (Print) is executed.
;
; Now, here's another way to use IFs.
;
; IF (condition) THEN
; (action)
; (action)
; END IF
;
; That way you can execute multiple actions when a condition is met.
;
; Okay now look at this:
;
If True Then Print "It's true!" Else Print "It's not true!"
;
; Which can also be written as:
;
If True
Print "It's true!"
Else
Print "It's not true!"
End If
;
; - The Then is missing!
; That's right, it's completely optional.
; Same as ever, coming down to personal preference :)
; Use it in any way you see fit.
;
; - What about the Else?
; The Else part will be executed when the (condition) is False.
;
; Here's the syntax:
;
; IF (condition) THEN
; (action)
; (action)
; ELSE
; (action)
; (action)
; END IF
;
; Ofcourse the examples above are not very useful :)
; So we'll return to our money.
;
If ( Money >= Cheap_Drive = True ) Then
Print "We can afford the cheap drive"
Else
Print "We can't afford the cheap drive"
End If
;
; Actually you don't need the = True part,
; and those brackets () are again, optional.
; But in this case the brackets () improve readability.
; In other cases you might need them to force the order of evaluation. (say what?)
;
; Comparisons, maths, etc are handled, calculated, evaluated in specific order.
; For example, in maths, multiplication has priority over addition.
;
Result = 10 + 10 * 10 ;( Results in 110 )
;
; So if you wanted to add first and multiply later
;
Result = ( 10 + 10 ) * 10 ;( Results in 200 )
;
; the brackets would come in handy :)
; You can apply this in any evaluation.
;
Print ((((((((((((((((( 1 )))))))))))))))))
;
; Anyway, I think we figured out by now that we can afford the cheap drive.
; You try figure out how to find out if we can afford the expensive drive :P
;
; Here's an interesting solution:
;
If Money >= Expensive_Drive
Print "We can afford the expensive drive!"
Else If Money >= Cheap_Drive
Print "We can afford the cheap drive"
Else
Print "We can't afford anything :P"
End If
;
; You can stack IFs like that.
; Else after Else after Else.
;
; Syntax:
;
; IF (condition1) THEN
; (action)
; (action)
; ELSE IF (condition2) THEN
; (action)
; (action)
; ELSE IF (condition3) THEN
; (action)
; (action)
; END IF
;
; Be aware though that, as with most things,
; the list is read from top to bottom.
; So if you compare with a Cheap_Drive first,
; it will never tell you that you have enough
; to buy the expensive drive :/
;
; Ofcourse what we've done so far is comparing integers with integers.
; You can also compare floats, strings and pointers.
; But we'll get to those later on in variable collections.
;
; Imagine you have an amount of money like before.
;
Money = 100
;
; What if you wanted to check if it was in a certain range?
; Defined by a minimum and maximum value.
;
Minimum = 50
Maximum = 150
;
; Here's one way;
;
If Money >= Minimum
If Money <= Maximum
Print "In range! (1)"
End If
End If
;
; Here's another;
;
If ( Money >= Minimum ) And ( Money <= Maximum )
Print "In range! (2)"
End If
;
; Yes, these brackets are optional,
; in this case :)
;
; Let's set our Money to another value,
; so it falls outside our range.
;
Money = 0
;
; Now we can check if it's indeed out of range.
;
If Not Money >= Minimum And Money <= Maximum
Print "Out of range! (1)"
End If
;
; Here's another way;
;
If Money < Minimum Or Money > Maximum
Print "Out of range! (2)"
End If
;
;-------------------------------------------------------------------------------
;
; Suppose you're interested in buying some weapons.
; And there are a number of weapons you can choose from.
; We'll give each weapon a unique number.
;
Const Tazergun = 1
Const Lazergun = 2
Const Phazergun = 3
;
; And we pick a weapon.
;
Weapon = Lazergun
;
; It's actually a lot like the 'state-of-life' from the first part.
; So I'll refer to these as so called state identifiers.
;
; Now suppose we want to display on screen
; which weapon we have selected.
;
Select Weapon
Case Tazergun
Print "Tazergun"
Case Lazergun
Print "Lazergun"
Case Phazergun
Print "Phazergun"
End Select
;
; You could also do it like this:
;
Select Weapon
Case Tazergun : Print "Tazergun"
Case Lazergun : Print "Lazergun"
Case Phazergun : Print "Phazergun"
End Select
;
; If you would convert this code to IFs,
; here's what it would look like:
;
If Weapon = Tazergun Then
Print "Tazergun"
Else If Weapon = Lazergun Then
Print "Lazergun"
Else If Weapon = Phazergun Then
Print "Phazergun"
End If
;
; We know that each weapon
; (Tazergun, Lazergun and Phazergun)
; has a unique number (1, 2 and 3).
;
; Now what if we were buying
; an unregistered weapon so to speak.
;
Weapon = 6
;
; We could add a number
; for each weapon not registered.
; But we can also do this:
;
Select Weapon
Case Tazergun : Print "Tazergun"
Case Lazergun : Print "Lazergun"
Case Phazergun : Print "Phazergun"
Default : Print "-Unregistered-"
End Select
;
; Just the addition of Default
; and any number not in our list
; will end up as being an unregistered weapon.
;
; Here's what it looks like using IF statements:
;
If Weapon = Tazergun Then
Print "Tazergun"
Else If Weapon = Lazergun Then
Print "Lazergun"
Else If Weapon = Phazergun Then
Print "Phazergun"
Else
Print "-Unregistered-"
End If
;
; Note the added Else.
; E.g. very similar to the Default.
;
WaitKey
End
;
;-------------------------------------------------------------------------------