Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
On Exit question
#28
(06-03-2023, 03:10 PM)Dimster Wrote: Hi Steve - I do see the value in Option _Explicit catching those misspelled variables, however my control variables (x,y,i etc) are difficult to misspell. Terry has an interesting approach to nomenclature of variables and as B and you have pointed out, you guys use it all the time. So I'm guessing here you have specific control variables which you use all the time solely for loop/iterations? You would need a least two for sort routines and perhaps a 3rd as a backup. These 3 or so control variables would be Dimensioned when? On the fly as you need them or because you know they will probably be needed some time, they are Dimensioned as a routine course after Option _Explicit?

Steve highlighted the perfect example for the use case of OPTION _EXPLICIT. Having to DIM all variables is not a negative side effect of using it. Here is an example I coded in the last half hour. Both of these code listings do the exact same thing. The first example is reminiscent of what was found in those early BASIC books. The second example is what can be done using QB64 and a bit of variable planning. Imagine being handed the first example and your task is either to find a bug or modify the code in some significant way. Now you're handed the second listing and given the same task. Which would you prefer?

Code: (Select All)
'My cool program

DIM x(59)
DIM y(59)

r = 120
FOR i = 0 TO 6.178465447 STEP .104719753
    x(s) = COS(i - 1.5707963) * r + 320
    y(s) = SIN(i - 1.5707963) * r + 240
    s = s + 1
NEXT i

SCREEN _NEWIMAGE(640, 480, 32)
DO
    CLS
    _LIMIT 60
    ps = s
    s = VAL(RIGHT$(TIME$, 2))
    IF ps <> s THEN
        s6 = 0
    ELSE
        s6 = s6 + 1
    END IF
    CIRCLE (x(s6), y(s6)), 10
    PAINT (x(s6), y(s6))
    CIRCLE (x(s), y(s)), 10, _RGB32(255, 255, 254)
    PAINT (x(s), y(s)), _RGB32(255, 255, 254), _RGB32(255, 255, 254)
    _DISPLAY
LOOP UNTIL _KEYDOWN(27)
SYSTEM

Code: (Select All)
' 60th of a second sweeping clock
' Circles sweep around a clock face at 1 second and 60th second intervals

OPTION _EXPLICIT '                     force variable declaration

CONST PI = 3.1415926 '                 value of PI
CONST PI2 = 2 * PI '                   2 times PI (one full radian sweep)
CONST Radian60 = PI2 / 60 '            2 times PI divided by 60
CONST Radian4 = PI2 / 4 '              one quarter of the value of PI times 2
CONST SWIDTH = 640 '                   screen width
CONST SHEIGHT = 480 '                  screen height
CONST CCOLOR = _RGB32(255, 255, 254) ' one second sweep circle color

TYPE Tick_Coordinate '                 location of each clock tick coordinate
    x AS SINGLE '                      x coordinate
    y AS SINGLE '                      y coordinate
END TYPE

DIM Tick(59) AS Tick_Coordinate '      tick coordinates
DIM Radian AS SINGLE '                 loop counter
DIM Radius AS INTEGER '                radius of clock face
DIM Second AS INTEGER '                counter: one second
DIM Second60 AS INTEGER '              counter: 60th of a second
DIM pSecond AS INTEGER '               previous second

Radius = SHEIGHT / 4 '                                              calculate clock face radius
Second = 0 '                                                        reset second counter
FOR Radian = 0 TO PI2 - Radian60 STEP Radian60 '                    cycle through 60 radian points
    Tick(Second).x = COS(Radian - Radian4) * Radius + SWIDTH / 2 '  calculate x coordinate
    Tick(Second).y = SIN(Radian - Radian4) * Radius + SHEIGHT / 2 ' calculate y coodinate
    Second = Second + 1 '                                           increment second counter
NEXT Radian

SCREEN _NEWIMAGE(SWIDTH, SHEIGHT, 32) '                             graphics screen
DO '                                                                begin main loop
    CLS '                                                           clear screen
    _LIMIT 60 '                                                     update frame every 60th of a second
    pSecond = Second '                                              save previous second
    Second = VAL(RIGHT$(TIME$, 2)) '                                get current second
    IF pSecond <> Second THEN '                                     has a second elapsed?          <-- These lines synch the 60th
        Second60 = 0 '                                              yes, reset 60th second timer   <-- second circle to the top
    ELSE '                                                          no, still within same second   <-- of the sweep during second
        Second60 = Second60 + 1 '                                   increment 60th second counter  <-- value changes.
    END IF
    CIRCLE (Tick(Second60).x, Tick(Second60).y), 10 '               draw 60th second sweep
    PAINT (Tick(Second60).x, Tick(Second60).y)
    CIRCLE (Tick(Second).x, Tick(Second).y), 10, CCOLOR '           draw one second sweep
    PAINT (Tick(Second).x, Tick(Second).y), CCOLOR, CCOLOR
    _DISPLAY '                                                      update screen
LOOP UNTIL _KEYDOWN(27) '                                           leave when ESC pressed
SYSTEM '                                                            return to operating system
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply


Messages In This Thread
On Exit question - by NasaCow - 06-02-2023, 03:50 AM
RE: On Exit question - by mnrvovrfc - 06-02-2023, 05:29 AM
RE: On Exit question - by NasaCow - 06-02-2023, 06:00 AM
RE: On Exit question - by mnrvovrfc - 06-02-2023, 08:59 AM
RE: On Exit question - by Dimster - 06-02-2023, 11:19 AM
RE: On Exit question - by TerryRitchie - 06-02-2023, 11:27 AM
RE: On Exit question - by Dimster - 06-02-2023, 12:28 PM
RE: On Exit question - by Kernelpanic - 06-02-2023, 01:54 PM
RE: On Exit question - by TerryRitchie - 06-02-2023, 05:33 PM
RE: On Exit question - by bplus - 06-02-2023, 01:28 PM
RE: On Exit question - by bplus - 06-02-2023, 02:21 PM
RE: On Exit question - by Kernelpanic - 06-02-2023, 08:23 PM
RE: On Exit question - by bplus - 06-02-2023, 02:36 PM
RE: On Exit question - by bplus - 06-02-2023, 03:14 PM
RE: On Exit question - by bplus - 06-02-2023, 08:19 PM
RE: On Exit question - by bplus - 06-02-2023, 08:27 PM
RE: On Exit question - by Kernelpanic - 06-02-2023, 08:40 PM
RE: On Exit question - by Dimster - 06-02-2023, 09:33 PM
RE: On Exit question - by mnrvovrfc - 06-02-2023, 09:44 PM
RE: On Exit question - by TerryRitchie - 06-02-2023, 10:52 PM
RE: On Exit question - by bplus - 06-02-2023, 10:59 PM
RE: On Exit question - by TerryRitchie - 06-02-2023, 11:12 PM
RE: On Exit question - by bplus - 06-02-2023, 11:21 PM
RE: On Exit question - by Dimster - 06-03-2023, 01:29 PM
RE: On Exit question - by SMcNeill - 06-03-2023, 01:51 PM
RE: On Exit question - by Dimster - 06-03-2023, 03:10 PM
RE: On Exit question - by TerryRitchie - 06-03-2023, 07:21 PM
RE: On Exit question - by Kernelpanic - 06-03-2023, 03:26 PM
RE: On Exit question - by NasaCow - 06-04-2023, 03:03 AM
RE: On Exit question - by bplus - 06-04-2023, 03:20 PM
RE: On Exit question - by TempodiBasic - 06-04-2023, 04:05 PM
RE: On Exit question - by TempodiBasic - 06-04-2023, 05:38 PM
RE: On Exit question - by TerryRitchie - 06-04-2023, 08:35 PM
RE: On Exit question - by TempodiBasic - 06-04-2023, 10:51 PM
RE: On Exit question - by NasaCow - 06-04-2023, 11:59 PM
RE: On Exit question - by TerryRitchie - 06-05-2023, 12:31 AM
RE: On Exit question - by NasaCow - 06-05-2023, 12:50 AM
RE: On Exit question - by SMcNeill - 06-05-2023, 09:04 PM
RE: On Exit question - by bplus - 06-05-2023, 09:09 PM
RE: On Exit question - by SMcNeill - 06-05-2023, 09:17 PM
RE: On Exit question - by SMcNeill - 06-07-2023, 01:38 AM
RE: On Exit question - by TempodiBasic - 06-07-2023, 09:08 AM
RE: On Exit question - by TempodiBasic - 06-07-2023, 09:31 AM
RE: On Exit question - by NasaCow - 06-08-2023, 12:05 AM
RE: On Exit question - by mnrvovrfc - 06-09-2023, 09:31 PM
RE: On Exit question - by TempodiBasic - 06-09-2023, 09:14 PM
RE: On Exit question - by bplus - 06-10-2023, 05:24 PM
RE: On Exit question - by TempodiBasic - 06-17-2023, 12:18 AM
RE: On Exit question - by bplus - 06-17-2023, 02:29 AM
RE: On Exit question - by GareBear - 06-17-2023, 01:07 AM
RE: On Exit question - by TempodiBasic - 06-22-2023, 12:01 PM
RE: On Exit question - by bplus - 06-22-2023, 12:22 PM
RE: On Exit question - by TempodiBasic - 06-22-2023, 07:45 PM



Users browsing this thread: 75 Guest(s)