Qbasicnews.com
September 21, 2019, 10:31:37 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: Absolute need of goto 4 correct functioning of loop routine.  (Read 12193 times)
Mac
Senior Member
**
Posts: 243


WWW
« Reply #15 on: February 02, 2005, 05:57:28 PM »

Hi, Meg.

My turn. I pointed out that JohnK had a bug in his bowling program. It gives 30 points on a strike, when it should be 10 + next two balls.

You noted that I should check the instructions.

Ouch! True. He clearly states that a strike gets 30.

My definition of a bug has always been "deviation from written specifications", not "user does not like how the program runs".

In this case, the written specifications clearly state that if you get a strike, you get 30 points. I might not like how the program runs, but have to admit that it has no bug in that regard.

Thanks for pointing that out.

I will probably get a similar note from John. (*gulp*)

Mac
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #16 on: February 03, 2005, 02:41:38 AM »

Your Exit sub/funk/for/do/etc would be translatet to a JMP in ASM and JMP is GOTO.
;*)
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Diroga
Been there, done that
*****
Posts: 1087


« Reply #17 on: February 03, 2005, 05:31:11 AM »

well in ASM is not JMP importatn?
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #18 on: February 04, 2005, 03:29:20 AM »

Quote from: "Diroga"
well in ASM is not JMP importatn?


Very important.
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Mango
Wandering Guru
***
Posts: 360



« Reply #19 on: February 12, 2005, 08:45:49 AM »

Hi meg.

I am *not* a goto proponant.  However, I've found it useful in this program, which I think is a reasonable way to program a small prime number calculator.  

 

Code:

defint a-z
DIM SmallPrimes(1 to 3500) AS INTEGER

CLS
PRINT "This program sticks primes in an array then prints 'em"

'set initial values
a = 0
max = 3
smallprimes(1) = 3
next1 = 5
count = 1

'find first 3500 primes and put them in array, smallprimes()
'use 2-byte integer for small primes
DO WHILE count < 3500
  sqroot = SQR(next1)

  DO
      a = a + 1
      IF next1 MOD smallprimes(a) = 0 THEN GOTO notprime
  LOOP WHILE sqroot > smallprimes(a)

  count = count + 1
  smallprimes(count) = next1

notprime:

  next1 = next1 + 2
  a = 0
LOOP


FOR z = 1 TO 3500
   PRINT smallprimes(z);
NEXT z

Logged
Mac
Senior Member
**
Posts: 243


WWW
« Reply #20 on: February 12, 2005, 11:55:57 AM »

Hi, Mango

True, you save a few lines of code by that GOTO, but if you insert them as I have done below, I think the program is a bit easier to follow and it doesn't take any longer to run.

Mac

P.S. Both versions don't list "2", the first prime number. LOL.

Code:
DIM Max AS INTEGER: Max = 3500
DIM SmallPrimes(Max) AS INTEGER
CLS
PRINT "This program sticks primes in an array then prints 'em"
' set initial values
DIM a AS INTEGER: a = 0
SmallPrimes(1) = 3
DIM Next1 AS INTEGER: Next1 = 5
DIM Count AS INTEGER: Count = 1
DIM SQroot AS INTEGER
' find first 3500 primes and put them in array, smallprimes()
' use 2-byte integer for small primes
DO WHILE Count < Max
  SQroot = SQR(Next1)
  Prime = -1: ' Assume prime number
  DO
    a = a + 1
    IF Next1 MOD SmallPrimes(a) = 0 THEN Prime = 0: EXIT DO
  LOOP WHILE SQroot > SmallPrimes(a)
  IF Prime THEN Count = Count + 1: SmallPrimes(Count) = Next1
  Next1 = Next1 + 2
  a = 0
LOOP
FOR z = 1 TO 20: :  PRINT SmallPrimes(z); : NEXT z: PRINT
FOR z = Max - 20 TO Max: PRINT SmallPrimes(z); : NEXT z: PRINT
SYSTEM

Logged
Mango
Wandering Guru
***
Posts: 360



« Reply #21 on: February 12, 2005, 12:56:17 PM »

Hi mac.  Thanks for the reply.  

I don't think the goto has any value if all it does is reduce the number of lines of code...but if it saves a comparison (1 less if statement) then it has value.  

finding the first 3500 primes is near instantaneous...but if you use that short list (note it fills all the 16-bit primes) to find long int (32-bit primes) primes, then skipping that extra comparison when you already know the answer saves time.  

Cheers
Logged
Mac
Senior Member
**
Posts: 243


WWW
« Reply #22 on: February 12, 2005, 01:12:29 PM »

Good pont, Mango.

It's been a long time since I have considered efficieny. I was a mainframe programmer years ago who would spend days trying to eliminate one instruction. Now I am spoiled by zillion MHZ computers and simply had forgotten that instructions take time. Even now, I wonder how many seconds it would save in an actual case. But too lazy to experiment.

By the way, if you want to see some work by me on modularity, go here:
http://www.network54.com/Forum/message?forumid=190883&messageid=1108145374

Cheers

Mac
Logged
Mac
Senior Member
**
Posts: 243


WWW
« Reply #23 on: February 12, 2005, 03:12:51 PM »

I decided to test after all. I used pure DOS so there were no Windows interruptions and tested with GOTO and EXIT SUB.

GOTO took 378 seconds.
EXIT SUB took 580 seconds

You were right as rain! GOTO can save significant time.

I made a post on your behalf here:

http://www.network54.com/Forum/message?forumid=182035&messageid=1108231683

Mac
Logged
Mango
Wandering Guru
***
Posts: 360



« Reply #24 on: February 13, 2005, 12:18:21 AM »

Hey Mac...nice test.  This little bit of code is just about the only place I've used a goto in a LONG time.  I revisited a prime finder I made way back about 2 yrs ago while learning c++...wanted to see speed difference compared to QB...and there was that ugly goto...lingering.  

Not particularly interesting, but it's a good way to eat clocks, and thus compare compiler efficiency, and let me get a handle on the impact of juggling efficiency vs code readability.

In c++, I've found that if you use good algorithms and clean code, and an optimizing compiler,  the little tweaks that I'm often tempted to test in QB make no significant difference.

Cheers, and thanks for the input.

Joe
Logged
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!