Behaviour of OR statement with NEXT instruction

BeliasBelias Member Posts: 2,998
IF TBSalesLine.FINDSET THEN BEGIN
  REPEAT
    IF … then begin
      IF TBSalesLine.Quantity <> 0 THEN BEGIN
        ..
        TFLineMod2 := TRUE;
      END;
    END ELSE BEGIN
      …
      TFLineMod2 := TRUE;
    END;
    IF TFLineMod2 THEN
      INTSetLineNo2 := TBSalesLine."Line No.";
  UNTIL TFLineMod2 OR (TBSalesLine.NEXT=0);     
END;
Does the NEXT statement occurs if the TFLineMode2 is true?
my colleague on nav40 said yes, but i'm pretty sure that does not happen in nav2009 (i can't find the code, but i've had to revert the condition to make my code work)
-Mirko-
"Never memorize what you can easily find in a book".....Or Mibuso
My Blog

Answers

  • matteo_montanarimatteo_montanari Member Posts: 189
    Belias wrote:
    IF TBSalesLine.FINDSET THEN BEGIN
      REPEAT
        IF … then begin
          IF TBSalesLine.Quantity <> 0 THEN BEGIN
            ..
            TFLineMod2 := TRUE;
          END;
        END ELSE BEGIN
          …
          TFLineMod2 := TRUE;
        END;
        IF TFLineMod2 THEN
          INTSetLineNo2 := TBSalesLine."Line No.";
      UNTIL TFLineMod2 OR (TBSalesLine.NEXT=0);     
    END;
    
    Does the NEXT statement occurs if the TFLineMode2 is true?
    my colleague on nav40 said yes, but i'm pretty sure that does not happen in nav2009 (i can't find the code, but i've had to revert the condition to make my code work)

    Hi

    Yes, it will occur because Navision resolve every statement of a boolean condition, even if first statement is false and implicity the result will be false.

    You can test this with a small program:
    IF FALSE AND ResultFalse THEN
      MESSAGE('Final result true')
    ELSE
      MESSAGE('Final result false');
    

    resultfalse is a function like:
    ResultFalse() : Boolean
    message('ResultFalse checked');
    EXIT(FALSE);
    

    If navision does a complete calculation, it will show "ResultFalse checked" message.
    If navision will exit immediately at the first statement ("false"), wil not display the message.

    On Nav2009Sp1 Classic, i see the "ResultFalse checked" message.

    Bye

    Matteo
    Reno Sistemi Navision Developer
  • BeliasBelias Member Posts: 2,998
    AND condition means that all the "factors" have to be checked.
    OR condition implies that if the first condition is ok, the second one is not executed at all (in C# works like this, but if i'll find the code i wrote in NAV i'll post it)
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • matteo_montanarimatteo_montanari Member Posts: 189
    Belias wrote:
    AND condition means that all the "factors" have to be checked.
    OR condition implies that if the first condition is ok, the second one is not executed at all (in C# works like this, but if i'll find the code i wrote in NAV i'll post it)

    Hi

    If you change my test like :
    IF TRUE OR ResultFalse THEN
      MESSAGE('Final result true')
    ELSE
      MESSAGE('Final result false');
    

    "ResultFalse" function will be called even if the first statement is TRUE and there isn't reason to check the second statement.

    Matteo
    Reno Sistemi Navision Developer
  • BeliasBelias Member Posts: 2,998
    you're correct, it does the second part of the or, too :-k
    i have to find that piece of code ](*,) , because i'll probably have to correct it.
    thanks!
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Belias wrote:
    AND condition means that all the "factors" have to be checked.
    AND means that ALL factors must be TRUE, not that ALL factors have to be checked.

    OR also can imply that all factors must be checked - for example: FALSE OR FALSE OR FALSE OR something.

    It has nothing to do with how NAV checks logical conditions. As you were told, and checked on your own NAV always checks all part of logical expression.

    That's why I never write code like this example:
    IF someboolean AND someotherboolean AND somerecord.GET(...) THEN
    
    but in such a case always like this:
    IF someboolean AND someotherboolean THEN 
      IF somerecord.GET(...) THEN
    
    This warrants that fastest conditions are checked first, and GET is executed in latest step.
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • kinekine Member Posts: 12,562
    Belias wrote:
    AND condition means that all the "factors" have to be checked.
    OR condition implies that if the first condition is ok, the second one is not executed at all (in C# works like this, but if i'll find the code i wrote in NAV i'll post it)

    It was not correct in Pascal (C/AL is based on Pascal) and there were compiler options in Pascal to enable the "partial condition evaluation" or how it was named to work in this way...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • BeliasBelias Member Posts: 2,998
    ...It has nothing to do with how NAV checks logical conditions. As you were told, and checked on your own NAV always checks all part of logical expression.
    [...]
    That's why I never write code like this example:
    IF someboolean AND someotherboolean AND somerecord.GET(...) THEN
    
    but in such a case always like this:
    IF someboolean AND someotherboolean THEN 
      IF somerecord.GET(...) THEN
    
    This warrants that fastest conditions are checked first, and GET is executed in latest step.
    yes, you're right, that's how i write code, too...my observation is about the or condition: if the first part of the or is correct, there wouldn't be need to check the rest; anyway, as kine said, this is not true in c/al...for the next 2 weeks i'll be on holiday, see you soon guys! and thanks!
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
Sign In or Register to comment.