PB Survival guide A must read!!!

Share your advanced PureBasic knowledge/code with the community.
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

PB Survival guide A must read!!!

Post by Zebuddi123 »

Hi to all Just looking through Blueznl Survival Guide and came across C shorthand stuff and was just looking how i could apply that shorthand. How about a multi variable for loop ?

The Idea is to use 3 possible iteration integers based on the result of 2 variables a,b with the 3 possible results being based on = < >

May come in useful for some, I quite like the idea! Thanks Blueznl :)

Updated as a macro.

Because it uses bitwise OR more than one result would give the wrong result. So the Operators are built into the macro with = < > supported.

Code: Select all

a = 3 : b = 23

;if a = b loop 30   if a < b loop 120  if a > b loop 60

Macro VarLoop(Var_A, Var_b,  Multiplier_integer, Equals_Iteration, LessThan_Iteration, GeaterThan_Iteration)
	(  (Multiplier_Integer * (Bool( Var_A = Var_B) * Equals_Iteration)) |  (Multiplier_Integer * (Bool( Var_A < Var_B) * LessThan_Iteration))  |  (Multiplier_Integer * (Bool( Var_A > Var_B) * GeaterThan_Iteration))   )
EndMacro 

CallDebugger

For i = 1 To VarLoop(a, b, 6, 20, 10, 5) 
	Debug i
Next
Zebuddi. :)
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
Derren
Enthusiast
Enthusiast
Posts: 316
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: PB Survival guide A must read!!!

Post by Derren »

What is the multiplier (6) needed for?
I was a bit confused, because the comment doesn't predict the outcome.
The way your code now works is:
if a = b loop 120 (20*6)
if a < b loop 60 (10*6)
if a > b loop 30 (5*6)
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: PB Survival guide A must read!!!

Post by jack »

why would you want to write such obfuscated code?
it's not worth it unless you are entering an obfuscated code contest.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: PB Survival guide A must read!!!

Post by netmaestro »

No offense, and thanks blueznl for your work on the survival guide, seriously. -however- I don't see how this snippet would help someone survive the learning curve for PB. My eyes are still crossed from trying to read that macro :lol:
BERESHEIT
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: PB Survival guide A must read!!!

Post by TI-994A »

Derren wrote:What is the multiplier (6) needed for?
Exactly; what is the multiplier for?

It seems to work without it, and appears more readable as well:

Code: Select all

;if a = b loop 30   if a < b loop 120  if a > b loop 60

a = 1 : b = 2
  
For i = 1 To (Bool(a = b) * 30) | (Bool(a < b) * 120) | (Bool(a > b) * 60)
  Debug i
Next
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply