[Gambas-user] WHILE and AND: please explain me....
Fabián Flores Vadell
fabianfloresvadell at ...626...
Fri May 7 04:35:53 CEST 2010
2010/5/4, Leonardo Miliani <leonardo at ...1237...>:
> I'm working with this little piece of code, it's the Insertion Sort:
>
> PUBLIC SUB InsertionSort(matrix AS String[])
> DIM i, j AS Integer
> DIM x AS String
>
> FOR i = 1 TO (matrix.Count - 1)
> x = matrix[i]
> j = i - 1
> WHILE j >= 0 AND matrix[j] > x
> matrix[j + 1] = matrix[j]
> j -= 1
> WEND
> matrix[j + 1] = x
> NEXT
> END
>
> But I get an "Out of bound" error in the WHILE test, because j begins
> -1.
>
> Usually (in other languages this is the behavior) the AND test checks
> the first condition, and if it is false then it checks the second one.
> OK... but shouldn't Gambas bypass the WHILE statement because j is
> negative?
>
> --
> Leonardo Miliani
> www.leonardomiliani.com
Hi Leonardo. You have to avoid than j can become negative when you use
it like index on the "matrix" array. So, I think that you can do it,
modifying the matrix index in the logical test, using the IF()
function:
WHILE j >= 0 AND matrix[IF(j>0, j, 0)] > x
--
Fabián Flores Vadell
www.speedbooksargentina.blogspot.com
More information about the User
mailing list