Author Topic: AndAlso  (Read 6961 times)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2788
    • View Profile
AndAlso
« on: 2012-Jan-07 »
The is a Freebasic command that is really for optimisation of IF statments. Instead of each expression being evaluated only the next one is evaluated if the previous one is true.

This can already be done using nested if statements but the AndAlso command, makes for a very clean and readable(in single line) IF/THEN statement and it makes for very efficient code when you dont need to evaluate extra conditions when one fails.


From the FB manual...
Code: (glbasic) [Select]
If...Then Control flow statement for conditional branching


Syntax

If expression Then [statement(s)] [Else [statement(s)]] [End If]
or
If expression Then : [statement(s)] [Else [statement(s)]] : End If
or
If expression Then

[statement(s)]

[ ElseIf expression Then ]

[statement(s)]

[ Else ]

[statement(s)]

End If


Description

If...Then is a way to make decisions. It is a mechanism to execute code only if a condition is true, and can provide alternative code to execute based on more conditions.

expression can be one of several forms:

a conditional expression, for example:

x = 5

multiple conditions separated by logical bit-wise operators, for example:

x >= 5 And x <= 10

multiple conditions separated by logical short-circuit operators, for example:

y <> 0 AndAlso x \ y = 1
(in this case, "x \ y = 1" will only be evaluated if "y <> 0" is True)

any numerical expression, in which case a value of zero (0) represents False, and a non-zero value represents True


Both multi-line and single-line Ifs can be nested. In the latter case, the optional End Ifs can be useful to control where nested Ifs begin and end.

In the -lang fb and -lang fblite dialects, colons (:) can be used instead of newlines to construct multi-line If blocks on a single line.


Example


'' Here is a simple "guess the number" game using if...then for a decision.

Dim As Integer num, guess

Randomize
num = Int(Rnd * 10) + 1 'Create a random number between 1 and 10...
               
Print "guess the number between 1 and 10"

Do 'Start a loop

    Input "Guess"; guess 'Input a number from the user

    If guess > 10 OrElse guess < 1 Then  'The user's guess is out of range
        Print "The number can't be greater then 10 or less than 1!"
    ElseIf guess > num Then  'The user's guess is too low
        Print "Too low"
    ElseIf guess < num Then  'The user's guess is too high
        Print "Too high"
    ElseIf guess = num Then  'The user guessed the right number!
        Print "Correct!"
        Exit Do   'Exit the loop
    End If

Loop 'Go back to the start of the loop




Dialect Differences

In the -lang fb and -lang fblite dialects, if there is a new line, a single-line comment ('), a colon (:), or a Rem statement directly after THEN, then the IF will be multi-line. Any other statement will result in a single-line IF.
In the -lang qb dialect, if there is a new line or a single-line comment (') directly after THEN, then the IF will be multi-line. A colon, a Rem or any other statement will result in a single-line IF.


Differences from QB


END IF was not supported in single-line IFs in QBASIC.
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
Re: AndAlso
« Reply #1 on: 2012-Jan-07 »
Could you not use SELECT and CASE to determine which IF expressions to evaluate?
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2788
    • View Profile
Re: AndAlso
« Reply #2 on: 2012-Jan-07 »
No I don't think so, that's not its purpose, SELECT/CASE is for a different situation, that is, executing a specific piece of code that matches a specific expression. AndAlso, is the same as AND, except in certain situation its more efficient. As I said, it can be recreated with nested ifs but its not as efficient or as neat.

Code: (glbasic) [Select]
IF Check1(x1) ANDALSO Check1(x2) ANDALSO Check1(x3) ANDALSO x1 > 0 ANDALSO x1 <=10 THEN Blah=0One way to achieve this is...

Code: (glbasic) [Select]
IF Check1(x1)
  IF Check1(x2)
    IF Check1(x3)
      IF x1 > 0
         IF x1 <= 10
           blah=0
         ENDIF
      ENDIF
    ENDIF
  ENDIF
ENDIF

The optimisation feature here is that the function "Check1()" may need only be called once if it fails early or that any failed expression can break out before the rest of the IFs expressions are evaluated. A lot less code may be executed when compared to a traditional IF/AND/THEN.

« Last Edit: 2012-Jan-07 by bigsofty »
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4448
    • View Profile
    • FUED
Re: AndAlso
« Reply #3 on: 2012-Jan-08 »
would be nice, my codes have a few of those nested ifs

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
Re: AndAlso
« Reply #4 on: 2012-Jan-08 »
what's wrong with:

Code: (glbasic) [Select]
IF   (  Check1(x1) AND Check1(x2) AND Check1(x3) AND x1 > 0 AND x1 <=10  )    THEN Blah=0
?

cheers
Ocean

I was gonna post whats wrong with using AND but thought I must have stupidly missed something :D
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Offline Ruidesco

  • Mr. Polyvector
  • ***
  • Posts: 236
    • View Profile
Re: AndAlso
« Reply #5 on: 2012-Jan-08 »
What Bigsofty means is that using ANDs the whole chain of equations has to be evaluated no matter their individual results, while with ANDALSOs the chain evaluation is aborted when one of the equations turns to be false, thus saving some process time; that is why the concatenation of ANDALSOs works like nestled IFs, but is cleaner when it comes to code formatting.
« Last Edit: 2012-Jan-08 by Ruidesco »

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2788
    • View Profile
Re: AndAlso
« Reply #6 on: 2012-Jan-08 »
The problem with AND is that it's a logical operator, there may an occasion that using a logic calculation is required, part of an algorithm for example(even within an IF). As such the compiler does not always know what or when to cut out.

AndAlso is not the same as AND it more closely to nested IFs, one way that may operate GLB in a similar manner is...

IF a=b THEN IF c=d THEN IF e=f THEN x=0

Although that's a bit messy and I'm not sure it would work.

It's quite a popular command, used in VB,C#, Freebasic etc... Here's the VB manual entry for it for example, http://msdn.microsoft.com/en-us/library/cb8x3kfz(v=vs.80).aspx

« Last Edit: 2012-Jan-08 by bigsofty »
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10839
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: AndAlso
« Reply #7 on: 2012-Jan-09 »
Nohoooo!

See this examle:

Code: (glbasic) [Select]

DEBUG "with AND\n"
IF ok() AND ok() THEN PRINT 0,0,0
DEBUG "now with OR\n"
IF ok() OR ok() THEN PRINT 0,0,0
DEBUG "not ok with AND\n"
IF notok() AND ok() THEN PRINT 0,0,0

FUNCTION ok:
    DEBUG "Call to ok()\n"
    RETURN TRUE
ENDFUNCTION

FUNCTION notok:
    DEBUG "Call to notok()\n"
    RETURN FALSE
ENDFUNCTION

you will notice, there's only one call to "ok()" for the OR operator, because if the first condition is true, the 2nd condition does not matter anymore and won't be evaluated.
Same for the last line, where "notok()" already returns false and the ok() won't be called.
AND and OR are evaluated left to right.


Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
Re: AndAlso
« Reply #8 on: 2012-Jan-09 »
Thanks Kitty, didnt know that, now i can reorganise some code.
Could you plz, mention that in the help file, cuz its an nice feature, but not the expected behaviour.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2788
    • View Profile
Re: AndAlso
« Reply #9 on: 2012-Jan-09 »
Well this is a pleasant surprise, thanks for clearing that up Gernot.  :good:

BTW is this a GCC or GLB optimisation?
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline Crivens

  • Prof. Inline
  • *****
  • Posts: 913
    • View Profile
Re: AndAlso
« Reply #10 on: 2012-Jan-09 »
That's what I expected to be honest. It's what the ancient language I use at work does, although admittedly I've seen languages that process every single part of an OR.

Cheers
Current fave quote: Cause you like musicians and I like people with boobs.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10839
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: AndAlso
« Reply #11 on: 2012-Jan-10 »
that's the way the C/C++ code generated works.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2788
    • View Profile
Re: AndAlso
« Reply #12 on: 2012-Jan-10 »
Really surprised here, the andalso command is still being actively introduced to new languages. You've got to ask yourself the question why, when old languages have features that remove the need to have them at all? Anders Hejlsberg designed C# and Delphi, he is at the top of my personsallist of coding geniuses, again though, why does a smart guy introduce a defunct command into C#, that is not really needed? Weird.

Anyways, I digress, again, thanks for clearing that up Gernot.  :good:
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline Crivens

  • Prof. Inline
  • *****
  • Posts: 913
    • View Profile
Re: AndAlso
« Reply #13 on: 2012-Jan-10 »
Three reasons. Either we aren't 100% understanding what this new command does, they don't understand what the old commands do in the majority of languages, or they are doing it to make things more readable and therefore less complex for the general programmer.

Talking about readable, did anyone ever use Hypercard on the old Macs? I did my whole 3rd year BSc project in it and was the most amazingly readable language I've ever seen. You could even have spaces in variables. Sometimes when trying to work things out you could get away with typing what you wanted to do as you would say it in English and it would work. Brilliant. Probably rose tinted spectacles there. It was 20 years ago almost and I was always very very drunk etc...

Cheers
Current fave quote: Cause you like musicians and I like people with boobs.