Expand "if" command

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
graves
Enthusiast
Enthusiast
Posts: 160
Joined: Wed Oct 03, 2007 2:38 pm
Location: To the deal with a pepper

Expand "if" command

Post by graves »

Hi
1. It would be very useful to have a command "between" (like SQL). At this momment I'm using this macro

Code: Select all

macro between(btwfld, btwfrm, btwto)
  btwfld >= btwfrm AND btwfld <= btwto
endmacro

...
if between(datafield, valuefrom, valueto) ...

But it would be much more readable this construct:

Code: Select all

if datafield between valuefrom and valueto ...
2. Also we need to check same datafield against a lot of values (chained by OR), like this:

Code: Select all

if datafield = value1 OR value2 OR value3 ... 
Instead, I can use this construct:

Code: Select all

select datafield
  case value1, value2, value3:,,,
  default: ...
endselect
But it's not the same thing

BOTH cases can be unified in the next (generic) construct:

Code: Select all

if datafield [>=<] value1 [AND,OR] [>=<] value2 ....
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Expand "if" command

Post by PB »

graves wrote:But it would be much more readable this construct:

Code: Select all

if datafield between valuefrom and valueto ...
I don't understand? How is the above any different to just doing this:

Code: Select all

if value(datafield)=>valuefrom and value(datafield)<=valueto
:?:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Why not doing this:

Code: Select all

value=59

Select value
   Case 1 To 10, 90 To 100
      Debug "between 1-10 or 90-100"
   Case 14, 24, 34, 44, 54, 64, 74, 84
      Debug "value ends with 4"
   Case 35 To 41, 43
      Debug "value between 35-41 or equal to 43"
   Default
      Debug "any other value"
EndSelect
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

If you're using OR statements you should group each part of the expression in parenthesis:

Code: Select all

(btwfld >= btwfrm And btwfld <= btwto) Or (N=2 And R=1)
This is the proper way to make this kind of statement 'more readable'.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Expand "if" command

Post by luis »

[quote="graves"]Hi
1. It would be very useful to have a command "between" (like SQL). At this momment I'm using this macro

Code: Select all

macro between(btwfld, btwfrm, btwto)
  btwfld >= btwfrm AND btwfld <= btwto
endmacro

...
if between(datafield, valuefrom, valueto) ...


Hi, I believe it's really something you can already do in many ways without the need to add another keyword to the language.
Post Reply