|
| 1 | + |
| 2 | +'002 Numeric variables.bas SmallBASIC 0.12.6 [B+=MGA] 2016-06-08 |
| 3 | + |
| 4 | +? "!!! Right click mouse or tap 3 vertical bars (Android)" |
| 5 | +? "!!! to access the popup menu that gives access to vital" |
| 6 | +? "!!! functions. Call this GET POPUP." |
| 7 | +? |
| 8 | +? "GET POPUP now and select View Source to see this code!" |
| 9 | +? |
| 10 | +? "Numeric variables:" |
| 11 | +? |
| 12 | +? "Variables are containers. The variable/container has a" |
| 13 | +? "name and the variable's value is the contents found under" |
| 14 | +? "the name. They are called variables because the contents," |
| 15 | +? "the values, are or can be switched." |
| 16 | +? |
| 17 | +? "With SmallBASIC the name must start with a letter or _ " |
| 18 | +? "and may be followed by more letters, _ or digits." |
| 19 | +? |
| 20 | +? "In SmallBASIC letters are not case sensitive so:" |
| 21 | +? "MyVariable = myvariable = MYVARIABLE = MYvariable" |
| 22 | +? "just spell them the same (no spaces)." |
| 23 | +? |
| 24 | +? "... press any to continue" : pause : cls |
| 25 | + |
| 26 | +' Here are Two typical variable assignments, |
| 27 | +' loading a value into a named variable |
| 28 | +a = 5.05 '<== you can assign a number directly |
| 29 | + |
| 30 | +' or you can assign values from numeric expressions |
| 31 | +b_1 = 2 * 2 * a / 1.01 '<== should be 20 |
| 32 | + |
| 33 | +? "Here are two typical variable assignments just made: |
| 34 | +? |
| 35 | +? "Name:", "Value:",, "Name:", "Value:" |
| 36 | +? "a ", A,, "b_1 ", B_1 |
| 37 | +? |
| 38 | +? "Here is look at arithmatic operators +, -," |
| 39 | +? "* multiplication, / division." |
| 40 | +? "a + b_1 = ";A + b_1;" a - b_1 = ";A - B_1 |
| 41 | +? "a * b_1 = ";a * b_1;" a / b_1 = ";a / b_1 |
| 42 | +? |
| 43 | +? "For Powers use ^:" |
| 44 | +? "a squared is a ^ 2 = ";a^2 |
| 45 | +? "b_1 cubed is b_1 ^ 3 = ";b_1^3 |
| 46 | +? "The square root of a number n is n ^ .5 or n ^ (1/2)" |
| 47 | +? "The cubed root of a number n is n ^ (1/3)" |
| 48 | +? "So square root of a is a ^ .5 = ";a ^ .5 |
| 49 | +? "and cube root of b_1 is b_1 ^ (1/3) = ";b_1 ^ (1/3) |
| 50 | +? |
| 51 | +? "Integer division uses \ the slash that goes the other way." |
| 52 | +? "With Integer division the fractional part is just dropped." |
| 53 | +? "7 / 2 = ";7/2;" but 7 \ 2 = "; 7 \ 2 |
| 54 | +? "7 / 8 = ";7/8;" but 7 \ 8 = "; 7 \ 8 |
| 55 | +? |
| 56 | +? "Modulus returns the remainder, 5/3 has a remainder = 2" |
| 57 | +? "so 5 MOD 3 = 2 (or 5 % 3 = 2)." |
| 58 | +? "MOD or % work with integers (no decimal in number)." |
| 59 | +? "2 hours past 12 o'clock is 2 o'clock but what is" |
| 60 | +? "49 hours past 12 o'clock ?" |
| 61 | +? "It is 49 mod 12 or "; 49 mod 12;" o clock" |
| 62 | +? |
| 63 | +? "... press any to continue" : pause : cls |
| 64 | + |
| 65 | +'123456789012345678901234567890123456789012345678901234567890 |
| 66 | +? "Now that we have some variables and numeric operators," |
| 67 | +? "Let's try input of a radius and calculation of a circle's" |
| 68 | +? "Area = pi * radius ^ 2 |
| 69 | +? |
| 70 | +? "pi is a built in constant in SmallBASIC = ";pi |
| 71 | +? |
| 72 | +? "Next we will see INPUT keyword assigning the variable |
| 73 | +? "radius with a number you enter after a prompt enclosed" |
| 74 | +? "with double quotes:" |
| 75 | +? |
| 76 | +' The INPUT statement structure or syntax: |
| 77 | +' INPUT "prompt"; inputVARIABLE |
| 78 | +' the V's are pointing to parts |
| 79 | +' V key word V "prompt" semi-colon V Variable |
| 80 | +input "So please enter a radius now > "; radius |
| 81 | +? "We have assigned the value ";radius;" to radius," |
| 82 | +? "using an INPUT statement, a second way to assign" |
| 83 | +? "values to variables. Normally we would check INPUT" |
| 84 | +? "values but we haven't discussed IF THEN structure." |
| 85 | +? |
| 86 | +area = pi * radius ^ 2 |
| 87 | +? "A circle with radius ";radius;" has an area of ";area |
| 88 | + |
| 89 | +? |
| 90 | +? "... press any to continue" : pause : cls |
| 91 | + |
| 92 | +? "OK now is a good time to discuss the order operators" |
| 93 | +? "are handled, since it does not go from left to right." |
| 94 | +? "The main thing to remember is that + and - are done" |
| 95 | +? "last, next to last are * and / (mult. and divide) so: |
| 96 | +? "7 - 3 * 5 won't be 4 * 5 == 20 but 7 - 15 == -8." |
| 97 | +? " (I will start using == to mean equals)" |
| 98 | +? "When in doubt use () as the operations inside are" |
| 99 | +? "handled first: (7 - 3) * 5 == 4 * 5 == 20 |
| 100 | +? |
| 101 | +? "Remember the quadratic equation? |
| 102 | +? |
| 103 | +? "Here is a case where getting the math order correct" |
| 104 | +? "is vital specially when expressing the two x values" |
| 105 | +? "possible directly from the A, B, C constants for the" |
| 106 | +? "equations:" |
| 107 | +? |
| 108 | +? "For x^2 + 2*x - 3 = 0 |
| 109 | +? "if A = 1, B = 2, C = -3" |
| 110 | +? "then x = (-B + ( B^2 - 4*A*C)^.5 )/(2*A)" |
| 111 | +? " and x = (-B - ( B^2 - 4*A*C)^.5 )/(2*A)" |
| 112 | +rem do that in code: |
| 113 | +rem Next line is 3 variable assignments on one line, |
| 114 | +A = 1 : B = 2 : C = -3 |
| 115 | +' a colon : separates statements in Basic |
| 116 | +? "x = ";(-B + (B^2 - 4*A*C)^.5)/(2*A); |
| 117 | +? " and x = ";(-B - (B^2 - 4*A*C)^.5)/(2*A) |
| 118 | +? |
| 119 | +? "and for x^2 - 99.1*x - 202.2 = 0 |
| 120 | +? "let A = 1, B = -99.1, C = -202.2 |
| 121 | +A = 1 : B = -99.1 : C = -202.2 |
| 122 | +? "x = ";(-B + (B^2 - 4*A*C)^.5)/(2*A); |
| 123 | +?" and x = ";(-B - (B^2 - 4*A*C)^.5)/(2*A) |
| 124 | +? |
| 125 | +? "BASIC makes formula or equation handling easy!" |
| 126 | +? |
| 127 | +pause |
0 commit comments