Author Topic: pascal "in statement" equivalent?  (Read 1629 times)

Offline baicskillet

  • Mc. Print
  • *
  • Posts: 29
    • View Profile
pascal "in statement" equivalent?
« on: 2010-Feb-17 »
Hi, this is sort of a newbie-like question, but I'm trying to learn glbasic by porting a simple pascal program, and the program uses very often the "in" statement, as in "x in [1,2,3,4]".
Is there an equivalent for the in statement in Glbasic, or is there another way to do the same the in statement does?

thank you!

MrTAToad

  • Guest
Re: pascal "in statement" equivalent?
« Reply #1 on: 2010-Feb-17 »
The easiest way would be to set up an array and then use FOREACH to go through each element.

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: pascal "in statement" equivalent?
« Reply #2 on: 2010-Feb-18 »
I read that as being part of a loop (i.e x will equal 1, then 2, then 3, then 4)
If that's the case it's easy

Code: (glbasic) [Select]
local x
for x = 1 to 4
    // do something with x here
   debug "x = "+x
next

As MrTAToad says, you could also use an array. This would be helpful if the numbers weren't sequential (in which case a loop wouldn't help you) - if they were "1,2,3,748" for example
Code: (glbasic) [Select]
dimdata myarray[],1,2,3,748
foreach x in myarray[]
   // do something with x here
   debug "x = "+x
next

Offline baicskillet

  • Mc. Print
  • *
  • Posts: 29
    • View Profile
Re: pascal "in statement" equivalent?
« Reply #3 on: 2010-Feb-18 »
Awesome, thank you both for your replies! Hmm, from what I've been looking about the "in" statement from Pascal is that is checks whether the variable before the "in" statement is somewhere in the variable or variables after the "in" statement.  So I suppose a foreach loop that compares the before variable with each element in the array, right? Yeah, the number aren't sequential, and sometimes they are letters and strings instead.  I'll try that foreach loop and let you know!