PART 5: Selection Statements Selection Statements are used to make programs have differing outcomes, instead to executing the same way every time they are run. A selection statement checks an expression, sees if it is True or False, then if True executes the rest of the statement otherwise it skips to below the statement and continues with execution. There are two types of selection statements on the calculator, but one can only be used on models before the AFX series. They are the If/Else statement and the => arrow. An If/Else statement works like this 1->A If A=1 //expression to be evaluated Then ?HI? //result if expression is true ?HOW ARE YOU? Else ?BYE? //result if expression is false ?SEE YOU LATER? If End //end of statement Result: Since A is 1 the statement evaluates to true, therefore HI HOW ARE YOU Is printed. If you replaced the first line with 0->A then the if statement would evaluate to False, and BYE SEE YOU LATER Would be printed. An If statement can contain many different things, and can be many lines long, they are the keystone to making a game, and before going on you should feel comfortable using them. The => arrow is also very useful, it is a single line selection statement that takes up less space but can do less than a normal if/else. This is the same code as above, except using the => arrow instead of the if statement. 1->A A=1=>?HI? A=1=>?HOW ARE YOU? A<>1=>?BYE? // <> means not equal to A<>1=>?SEE YOU LATER? Neat huh? As you can see, the => arrow provides cleaner looking code, and occasionally takes up less space than the if statement. In general, the best time to use a if statement is when you have at least to lines of code to be put inside, otherwise use the => arrow. PART 6: Advanced Loops Now that you have a good understanding of both loops and selection statements I will go into the more advanced features of both. First off we?ll talk about nesting. Nesting is when you take one loop and put it inside another. Here is an example: 0->C For 1->A To 10 //Step can be omitted instead of using Step 1 For 1->B To 10 A+B+C->C Next Next C_ // the _ represents the output sign, it displays whatever is before it and Pauses until [EXE] is pressed. By executing this code you will get this output: 1100 Disp //caused by _ Now we will go through the code and look at what each line does. Line 1: 0->C Assigns the value 0 to the variable C Line 2: For 1->A To 10 Tells the program that you will be looping until A is equal to 10, adding 1 with each loop. Line 3: For 1->B To 10 Tells the program that you will be looping until B is equal to 10, adding 1 with each loop. Line 4: A+B+C->C The current value in A will be added to the current value of B, that value is then added to the current value of C and then assigned to C. Line 5: Next Goes to line 3. Line 6: Next Goes to line 2. Line 7: C_ Displays the final value of C. As you can see, the second loop executes completely for each iteration (loop) of the first loop. Nesting also applies to selection statements, you can nest if or => inside each other as needed. Example: A<1=>A>0=>?HI? If A<1 Then if A>0 Then ?HI? End If End If This brings us to another point: Part 7: Logical Operators As you have just seen, there are times when you will want more than one condition in a selection or loop, you can nest the statements or you can use a logical operator: And, Or, Not. And and Or are operators which can be used in selection statements to specify additional conditions, therefore the code above could be written: A<1 and A>0=>?HI? Instead of: A<1=>A>0=>?HI? The Or operator allows you to specify alternate conditions which are evaluated independently and if any are true then the entire statement is considered true. This means you can do this: If A=1 Or B=1 Then ?TRUE? End If When this executes it will print TRUE if A or B equals 1. Handy huh? The Not operator can also be used in selection statements, though it is not used nearly as often. Not returns False if the statement is true (or 1) and True if the statement is False (or 0) so: If Not (A=1) Then ?TRUE? End If This will print true if A is any number but 1. In this case the Not statement is used as an <> (not equal to) statement, but it has other uses. We will cover more on this later.
Casio BASIC Tutorial
#1
Posted 25 April 2003 - 12:00 AM
#2
Posted 25 April 2003 - 01:28 AM
#3
Posted 25 April 2003 - 10:46 AM
reminds me of when i tried to write a program on pc (i was 6) to add two numbers from one to ten. that was before i learned how to do numeric variables - i learned strings first.
it was something like
input x$
input y$
if x$="1" and y$="1" then print "2"
etc
etc
i never finished it.
#4
Posted 25 April 2003 - 11:06 AM
#5
Posted 25 April 2003 - 11:11 AM
CrimsonCasio; thanks for your permission. I will publish it on CasioCorner as soon as the whole article is ready. Thanks again and good luck!
#6
Posted 26 April 2003 - 09:24 AM
#7
Posted 28 April 2003 - 02:09 AM
You didn't know that???
I've been programming with Menu returns since the Dark Ages!!!!
Nearly all of my programs have a quit menu with the simple command 'Stop' (or 'Disp' may work) and some text on the screen so the user can decide whether to Restart (EXE), goto the Menu (MENU) and Shut Down (Shift + AC). And what's so good about that? Don't you set you hopes a little higher?
P.S. For rjstephens, if you work on calc under one of the sections (e.g. PROG, RUN etc.) and return to the menu, when you go back, everything will be as you left it and not the default screen.
In fact, I'm going to post a new topic with programming tips...
#8
Posted 23 August 2003 - 03:37 AM
#9
Posted 23 August 2003 - 03:42 AM
#10
Posted 23 August 2003 - 04:16 AM
What ?press, shift, vars, jump, F3
"Isz" ?
#11
Posted 23 August 2003 - 05:10 AM
I'll restate it: Press shift, vars, F3, F3
#12
Posted 23 August 2003 - 12:04 PM
In AFX 2.0+ ?yeah, you wanted to know where the => command is.
I'll restate it: Press shift, vars, F3, F3
#13
Posted 23 August 2003 - 12:09 PM
so this (A=1=>2->A) becomes (if A=1:Then 2->A:IfEnd). note that you can replace the colons with line breaks.
if you have a link cable you can upload a program that has the => in it then use copy/paste to use it in your programs.
#14
Posted 23 August 2003 - 12:12 PM
#15
Posted 23 August 2003 - 12:14 PM
#16
Posted 23 August 2003 - 01:48 PM
#17
Posted 23 August 2003 - 02:56 PM
How ?just any... you can use the FA-123 to create a program that only has the => just look for it in the menus.
#18
Posted 24 August 2003 - 05:00 AM
my game "whack it" has a few of them in there, here's a direct link for ya
http://www.geocities...ams/whackit.zip
#19
Posted 29 August 2003 - 10:48 AM
I can't find it !you upload any prog to your calc with the => already in there then you copy and paste it out
my game "whack it" has a few of them in there, here's a direct link for ya
http://www.geocities...ams/whackit.zip
#22
Posted 30 August 2003 - 08:47 AM
#23
Posted 30 August 2003 - 09:32 AM
there's a bunch of them in there all i can suggest is look harder
#24
Posted 30 August 2003 - 12:31 PM
\Text X,11,"\->":\Do
\Getkey\->G:\LpWhile \ Not G
\Text X,9," "
G=28\ And X\<>28\=>X-7\->X //here =>
G=37\ And X\<>49\=>X+7\->X
\LpWhile G\<>31
X=49\=>\Goto3
\If X=42
\Then \ClrText
" HIGH SCORES"
#25
Posted 01 September 2003 - 03:25 PM
Thank You
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users