Beijer Electronics (formerly QSI Corporation)

Manufacturer of Mobile Data and Human Machine Interface Terminals.
It is currently Sun Nov 19, 2017 8:38 am

All times are UTC - 7 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Aug 08, 2011 3:06 am 
Offline

Joined: Wed Aug 03, 2011 8:44 am
Posts: 7
I have a little routine used to sort the elements of an array using Insertion Sort.

It looks like :

Code:
func Sort (myArray[] as reference to integer)
    dim i, j, newValue  as integer

    for i = 1 to (len(myArray) - 1)
        newValue = myArray[i]
        j = i
        while (j > 0 AND myArray[j - 1] > newValue) do
            myArray[j] = myArray[j - 1]
            j = j - 1
        loop
        myArray[j] = newValue
    next
endfunc


Unfortunately, it regularly crashes at the beginning of while loop.
The reason is, after first loop, j has the value zero, which is OK.
The loop should be entered if (j > 0) AND previous element in array is bigger than current element.
Since j is equal to zero, the first term of the boolean expression is false, thus the entire boolean expression is false.
In other languages (Java, C, etc.) the second term is not evaluated since it doesn't make sense.
Qlarity throws an exception trying to access a non-existing array element.

How can such a situation be solved in Qlarity ?
Are there some (undocumented ?) options to tell Qlarity, not to evaluate the entire expression if the first term is already false ?

I could of course use a different sorting algorithm but I ask myself how to deal with such a situation.

Regards,
Tony :-)


Top
 Profile  
 
PostPosted: Mon Aug 08, 2011 7:23 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Qlarity does not use short circuit evaluation. This was a deliberate design decision back when we originally wrote the language. Much of the syntax was intended to mirror that of BASIC, and none of the BASIC variants we tested used short circuit evaluation.

Over the years, I have wished for that feature and had on my list to add it. (Eventually VB gave programmers the ability to select short circuit or traditional evaluation by adding AndAlso and OrElse operators). However, I don't know when or if I will ever add that to the language.

While it is somewhat awkward, your loop can be modified slightly to do the right thing
Code:
        while j > 0 do
            if not (myArray[j - 1] > newValue) then
                break
            endif
            myArray[j] = myArray[j - 1]
            j = j - 1
        loop

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Tue Aug 09, 2011 1:08 am 
Offline

Joined: Wed Aug 03, 2011 8:44 am
Posts: 7
Thank you Jeremy,

that is exactly what I was looking for.

I was searching for a similar construct but I couldn't find the instruction break in the documentation :shock: :-(

Regards,
Tony :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 7 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group