Author Topic: BASIC programming tips  (Read 49849 times)

0 Members and 1 Guest are viewing this topic.

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #75 on: 07:59, 12 March 17 »
Code: [Select]
5 DEFINT a-b


??

* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 237
  • Likes Given: 216
Re: BASIC programming tips
« Reply #76 on: 20:51, 12 March 17 »
Code: [Select]
5 DEFINT a-b
??
Not quite was I was meaning (but you are right). More like is there a BASIC command (i.e. MOD, XOR, AND etc...) that could do this sequence without the IF statements. I feel that is the section that slows the program down.
Keeping it Kiwi since 1977

Offline remax

  • 6128 Plus
  • ******
  • Posts: 1.115
  • Country: fr
  • Liked: 406
  • Likes Given: 447
Re: BASIC programming tips
« Reply #77 on: 21:25, 12 March 17 »
perhaps use "x MOD y"


Code: [Select]
5 DEFINT a
10 a=1
20 a=a+1
30 PRINT a MOD 3+1
40 GOTO 20
« Last Edit: 21:29, 12 March 17 by remax »

Offline ronaldo

  • Dev
  • 6128 Plus
  • *****
  • Posts: 616
  • Country: es
    • Fremos Blog
  • Liked: 867
  • Likes Given: 801
Re: BASIC programming tips
« Reply #78 on: 22:05, 12 March 17 »
Easiest way I can think of  :D
Code: [Select]
10 PRINT "1 2 3 2 ":GOTO 10

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #79 on: 22:29, 12 March 17 »
Not quite was I was meaning (but you are right). More like is there a BASIC command (i.e. MOD, XOR, AND etc...) that could do this sequence without the IF statements. I feel that is the section that slows the program down.
I've seen the functions MAX & MIN being used instead of IF statements to determine if a variable reaches a certain value. There's a little bit of information about it in Basically BASIC Amstrad Amstrad article in Issue 112. The idea there is MIN can be used so the variable never becomes larger than the number you want and MAX is used so the number never drops below the number you want.

Does that make sense?

Though the @remax example probably does the trick.  :D


Okay, so the example relax posted just goes 1 2 3 1 2 3 instead of 1 2 3 2 1 2 3. Using MAX & MIN there are a couple of ways of writing it, though it still involves IF statements and to be honest they are probably going to be slower than what you've written. So my examples look like this:


Code: [Select]
20 IF a=MAX(a,3) THEN b=-1
30 IF a=MIN(a,1) THEN b=1
40 PRINT a;
50 a=a+b



or:


Code: [Select]
20 IF MAX(a,3)=3 THEN b=-1
30 IF MIN(a,1)=1 THEN b=1



and so on...




Easiest way I can think of 
Code: [Select]
10 PRINT "1 2 3 2 ":GOTO 10



It just needs a semi-column ";" then you will be in business.  :)
« Last Edit: 23:37, 12 March 17 by AMSDOS »
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline remax

  • 6128 Plus
  • ******
  • Posts: 1.115
  • Country: fr
  • Liked: 406
  • Likes Given: 447
Re: BASIC programming tips
« Reply #80 on: 23:35, 12 March 17 »
My bad, i had read the goal a bit too fast...


Code: [Select]
5 DEFINT a
10 a=0
20 a=a MOD 4+1
30 PRINT 2-(8*a)/3+2*a*a-a*a*a/3
40 GOTO 20



:P
« Last Edit: 23:50, 12 March 17 by remax »

Online Urusergi

  • CPC6128
  • ****
  • Posts: 189
  • Country: es
  • Liked: 276
  • Likes Given: 1307
Re: BASIC programming tips
« Reply #81 on: 23:46, 12 March 17 »
 The sequence without the IF statements... not sure if it's faster than yours
Code: [Select]
10 DEFINT a,b:a=3
20 DIM b(3):b(0)=1:b(1)=2:b(2)=3:b(3)=2
30 a=(a+1) MOD 4
40 PRINT b(a):GOTO 30

Online Urusergi

  • CPC6128
  • ****
  • Posts: 189
  • Country: es
  • Liked: 276
  • Likes Given: 1307
Re: BASIC programming tips
« Reply #82 on: 00:07, 13 March 17 »
Code: [Select]
30 PRINT 2-(8*a)/3+2*a*a-a*a*a/3
a better approach...
Code: [Select]
30 PRINT ((a+2)*a*a+2) MOD 4

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #83 on: 00:32, 13 March 17 »
The array approach is noticeably faster than the mathematical approach. I made some adjustments to @Urusergi routine to do a more complicated stringed approach to this problem:

Code: [Select]
10 DEFINT a:a=0
20 c$="1 2 3 2 "
30 a=a MOD 8+1
40 PRINT MID$(c$,a,1);:GOTO 30

Note the Spaces in Line 20 are important if you want the spacing right.  :laugh:
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 237
  • Likes Given: 216
Re: BASIC programming tips
« Reply #84 on: 01:58, 13 March 17 »
Thanks for all the great answers guys. I knew there would be a formula to work this out.
I have gone for the combination of @remax and @Urusergi formula as it is the result I am after not the actual printing on the screen.

I must admit the array solutions were clever but of no use in what I am hoping to do.  :P
Keeping it Kiwi since 1977

Offline mr_lou

  • 6128 Plus
  • ******
  • Posts: 3.036
  • Country: dk
    • index.php?action=treasury
    • 8-bit Memoirs - a Blu-ray diskmag-like eBook about the 8-bit era
  • Liked: 1201
  • Likes Given: 2423
Re: basic programming tips
« Reply #85 on: 14:39, 10 July 17 »
How to put machine code in Basic without FOR READ POKE NEXT DATA. Short and fast way, just in one PRINT. :D

Line 5 is need only once to generate 32 characters. Place them in quotes in PRINT in line 10. Now line 5 is no needed any more. Save and RUN and press any keys. ;)

This is LDIR demonstration. Working after reset or after SYMBOL AFTER 240. :)

Of course CAT is just tu show whatever on screen. Because CALL HIMEM+13 make copy from &c000 to &4000, and CALL HIMEM+1 opposite. :)

5 FOR a=1 TO 63 STEP 2:PRINT CHR$(1)CHR$(VAL("&"+MID$("19F02101401101C001D019F13FEDB0C92101C01119F2014001D03FEDB0C90432",a,2)));:NEXT:EDIT 10
10 PRINT"":CAT:CALL HIMEM+13:WHILE-1:CLS:CALL &BB18:CALL HIMEM+1:CALL &BB18:WEND

I'm rather intrigued by this technique.

Shouldn't it also be possible to achieve the same using variables?

Code: [Select]
code$="<the code string>"
address = peek(@code$+1)+peek(code$+2)*256
call address

The advantage of adding machine code this way is obvious, but what are the disadvantages (if any)?

Offline roudoudou

  • 6128 Plus
  • ******
  • Posts: 813
  • Country: fr
    • urban exploration
  • Liked: 1109
  • Likes Given: 675
Re: basic programming tips
« Reply #86 on: 16:14, 10 July 17 »
The advantage of adding machine code this way is obvious, but what are the disadvantages (if any)?


this is specific to Amstrad, not compatible with all basic (or CPU)
use RASM, the best assembler ever made :p

I will survive

Offline mr_lou

  • 6128 Plus
  • ******
  • Posts: 3.036
  • Country: dk
    • index.php?action=treasury
    • 8-bit Memoirs - a Blu-ray diskmag-like eBook about the 8-bit era
  • Liked: 1201
  • Likes Given: 2423
Re: basic programming tips
« Reply #87 on: 16:28, 10 July 17 »
this is specific to Amstrad, not compatible with all basic (or CPU)

That's ok. I only have eyes for the CPC anyway.  ;)

Offline ronaldo

  • Dev
  • 6128 Plus
  • *****
  • Posts: 616
  • Country: es
    • Fremos Blog
  • Liked: 867
  • Likes Given: 801
Re: BASIC programming tips
« Reply #88 on: 17:55, 10 July 17 »
Thanks for all the great answers guys. I knew there would be a formula to work this out.
I have gone for the combination of @remax and @Urusergi formula as it is the result I am after not the actual printing on the screen.

I must admit the array solutions were clever but of no use in what I am hoping to do.  :P
Don't know what your exact goal is, but here you are 2 proposals that may be of interest:
Code: [Select]
10 DEFINT a-z
' Generate numbers consecutively
20 FOR i=1 to 3:?i:NEXT:?2:GOTO 20

Code: [Select]
10 DEFINT a-z
' initialize: i=generated num, m,n=generation parameters
20 i=1:m=3:n=2
30 GOSUB 100:?i:GOTO 30
' Generate next i number
100 i=i XOR m:m=m XOR n:n=n XOR 2:RETURN

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 237
  • Likes Given: 216
Re: BASIC programming tips
« Reply #89 on: 03:45, 11 July 17 »
Thanks @ronaldo
I like your use of the XOR to get the desired sequence.
I wanted something for an animation sequence (in BASIC) where I have 3 frames of animation but wanted frame 2 to be displayed every 2nd time. 1,2,3,2,1,2,3 etc...
I haven't ever had time to do anything with it though as I ended up doing other things instead.
Keeping it Kiwi since 1977

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 237
  • Likes Given: 216
Re: BASIC programming tips
« Reply #90 on: 08:44, 30 July 17 »
I have another question which I'm hoping is very simple.

I have a variable counting down from 100. I would like it to print as a 3 digit number therefore adding 0's in front if below 100.
i.e. 99 = 099 and 9 = 009.

I have tried using the PRINT USING command but can't get it to work.

Any ideas?
Keeping it Kiwi since 1977

Offline mr_lou

  • 6128 Plus
  • ******
  • Posts: 3.036
  • Country: dk
    • index.php?action=treasury
    • 8-bit Memoirs - a Blu-ray diskmag-like eBook about the 8-bit era
  • Liked: 1201
  • Likes Given: 2423
Re: BASIC programming tips
« Reply #91 on: 09:18, 30 July 17 »
Probably not the best solution, but....

Code: [Select]
10 for n=100 to 0 step -1
20 a$=right$("00"+mid$(str$(n),2),3)
30 print a$
40 next

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #92 on: 09:29, 30 July 17 »
I have another question which I'm hoping is very simple.

I have a variable counting down from 100. I would like it to print as a 3 digit number therefore adding 0's in front if below 100.
i.e. 99 = 099 and 9 = 009.

I have tried using the PRINT USING command but can't get it to work.

Any ideas?


I'm doing something like that with this program, though it's using the Condensed number set to count down.


So what I did was setup an Array n%(0) to n%(2) with the values, so in your case it would be n%(0)=1 n%(1)=0 n%(2)=0 the main guts of the program is Lines 610 to 710 to count down the values. To put those values from an the array into an integer Line 620 puts it into an Integer called Result, 630 to 650 converts the numbers from the Integer array, into a String Array and to impose the Condensed Number set I've added 220 to get the condensed set character.


But having said all that, PRINT USING I think is the proper approach (the reason I did the above related to my Pascal game and incorporating a Condensed Number counting system into it), unfortunately it's an technique I'm not too familiar with But if it can be done, then it would be somewhat faster than the approach I've used. The approach I used was also the technique used for my Silly Number Guessing Game.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline andycadley

  • Supporter
  • 6128 Plus
  • *
  • Posts: 888
  • Liked: 424
  • Likes Given: 72
Re: BASIC programming tips
« Reply #93 on: 18:20, 06 August 17 »
Alas PRINT USING doesn't have any way of specifying leading zeroes in the format specifier, so you're best of with the MID$ option.

Online Urusergi

  • CPC6128
  • ****
  • Posts: 189
  • Country: es
  • Liked: 276
  • Likes Given: 1307
Re: BASIC programming tips
« Reply #94 on: 20:14, 06 August 17 »
Another arithmetic solution:

Code: [Select]
10 FOR i=100 TO 0 STEP -1
20 a$=HEX$(96*(i\100)+6*(i\10)+i,3)
30 PRINT a$
40 NEXT

If it's limited to 99 then we can delete the "96*(i\100)+" part
« Last Edit: 20:20, 06 August 17 by Urusergi »

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #95 on: 12:43, 10 September 17 »
Thanks @ronaldo
I like your use of the XOR to get the desired sequence.
I wanted something for an animation sequence (in BASIC) where I have 3 frames of animation but wanted frame 2 to be displayed every 2nd time. 1,2,3,2,1,2,3 etc...
I haven't ever had time to do anything with it though as I ended up doing other things instead.


Ironically I found myself writing an INK animation sequence, though mine alternates between 2 colours going back and forth. Another line is added which begins with the opposite colour back and forth, so when INK is used with alternating colours it appears to be moving. It works through I think the coding could be improved.  ???
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline SRS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 581
  • Country: de
  • Schneider CPC464 - what else ?
  • Liked: 568
  • Likes Given: 311
Re: BASIC programming tips
« Reply #96 on: 23:19, 20 February 18 »
One thing I just (re)learned today:

Code: [Select]
FOR I=0 to 1000:NEXT
ist FASTER than

Code: [Select]
FOR I=0 TO 1000:NEXT I
I sometimes forget we have interpreter not compiler ;)

Online Urusergi

  • CPC6128
  • ****
  • Posts: 189
  • Country: es
  • Liked: 276
  • Likes Given: 1307
Re: BASIC programming tips
« Reply #97 on: 00:25, 21 February 18 »
This reminds me that depending on how you write the code you can save bytes in basic.

Code: [Select]
10 FOR I=100 TO 0 STEP -1:NEXT
this take 3 bytes more than:

Code: [Select]
10 FOR I=100TO 0STEP-1:NEXT

Offline skylas

  • CPC664
  • ***
  • Posts: 84
  • Country: gr
    • AMSTRAD SAKIS
  • Liked: 114
  • Likes Given: 106
Re: BASIC programming tips
« Reply #98 on: 15:18, 05 April 18 »
Hello all

I have a question regarding BASIC command REMAIN
I use AFTER command, and for disabling it i use <PRINT REMAIN(1)>, as it is said in Amstrad handbook.
I would like to ask if this number can be calculated as a variable.
 I tried <PRINT REMAIN(1), X>, but this prints both remaining time and X variable.
I tried also <if PRINT REMAIN>1> or X=REMAIN but these does not help as it results in syntax error.
Is there a way to use this number on calculations?

Offline skylas

  • CPC664
  • ***
  • Posts: 84
  • Country: gr
    • AMSTRAD SAKIS
  • Liked: 114
  • Likes Given: 106
Re: BASIC programming tips
« Reply #99 on: 15:31, 05 April 18 »
Ok sorry, my mistake! i just had to write x=REMAIN(1) just before entering PRINT REMAIN(1) and disabling it!