BBC BASIC for Windows
Programming >> BBC BASIC language >> Just Thought I'd Mention...
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1284961174

Just Thought I'd Mention...
Post by Matt on Sep 20th, 2010, 05:39am

Reviewing another program someone had written, I found a little trick that I didn't know existed. It's a bonus with the +=, -=, etc. The use of it I have known since starting work with BB4W. But what I've just discovered is quite useful.

Original BBC Basic:
Code:
(1.) VARIABLE% = VARIABLE% + CONSTANT 
(There was no other way)

BB4W:
Code:
(1.) VARIABLE% = VARIABLE% + CONSTANT
(2.) VARIABLE% += CONSTANT 

Now, if VARIABLE% didn't exist in the first place command (1.) would error. However, command (2.) does not. This is useful when closing a window, for instance.

Code:
VARIABLE% += 0: IF VARIABLE% THEN [close window] 

Just thought I'd mention it. Well done Richard.

I know there must be thousands of other useful bonuses in the change from BBC Basic to BB4W, but I also wondered whether anyone else knew of anything little like this. Or is this just opening a whole box of bicuits?

Matt
Re: Just Thought I'd Mention...
Post by admin on Sep 20th, 2010, 08:21am

on Sep 20th, 2010, 05:39am, Matt wrote:
Code:
VARIABLE% += 0: IF VARIABLE% THEN [close window] 

Just thought I'd mention it.

Yes, many of my programs use that technique in their PROCcleanup routines. For example this in cpuspeed.bbc:

Code:
      DEF PROCcleanup
      pCPU% += 0  : IF pCPU%  SYS !(!pCPU%+8), pCPU%   : pCPU% = 0
      pEnum% += 0 : IF pEnum% SYS !(!pEnum%+8), pEnum% : pEnum% = 0
      pSvc% += 0  : IF pSvc%  SYS !(!pSvc%+8), pSvc%   : pSvc% = 0
      pLoc% += 0  : IF pLoc%  SYS !(!pLoc%+8), pLoc%   : pLoc% = 0
      SYS `CoUninitialize`
      ENDPROC 

It's specifically documented in the Wiki here:

http://bb4w.wikispaces.com/Forcing+a+variable+to+exist

Richard.
Re: Just Thought I'd Mention...
Post by knudvaneeden on Sep 20th, 2010, 12:41pm

on Sep 20th, 2010, 05:39am, Matt wrote:
Reviewing another program someone had written, I found a little trick that I didn't know existed. It's a bonus with the +=, -=, etc. The use of it I have known since starting work with BB4W. But what I've just discovered is quite useful.


This shorthand notation +=, *=, ... exists since 1969.
Ken Thompson and Dennis Ritchie of Bell Labs introduced it in their C language.
Nowadays used in several other computer languages, like C++, and also BBCBasic for Windows.
Re: Just Thought I'd Mention...
Post by knudvaneeden on Sep 20th, 2010, 1:15pm

Because C was a successor of e.g. the earlier B language, I checked if it maybe existed earlier than 1969, and found:

"Compound assignment operators, such as a += b to “add b to a” were popularized by the C and Pascal, but first available in ALGOL 68"
http://hhh.gavilan.edu/dvantassel/history/assignment.html#_Toc123623884

So it must have been used since about 1968.
Re: Just Thought I'd Mention...
Post by admin on Sep 20th, 2010, 4:28pm

on Sep 20th, 2010, 12:41pm, knudvaneeden wrote:
This shorthand notation +=, *=, ... exists since 1969.

I don't think the OP was commenting on the existence or function of the += operator, but the fact that it can be used to create a variable. Just because other languages have the same operator it doesn't follow that it will have that behaviour.

Incidentally, BBC BASIC didn't have the += operator (and friends) until version 5, in around 1986.

Richard.