Results 1 to 4 of 4

Thread: Simple question

  1. #1
    Join Date
    Jan 2012
    Posts
    1,104
    Mentioned
    18 Post(s)
    Quoted
    211 Post(s)

    Default Simple question

    Simba Code:
    if (function_A or function_B) then
        DoSomething;
      NextCommand;
    If function_A returns with True, will function_B be skipped or not?

  2. #2
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    This is called short circuit evaluation. All conditions will be evaluated before continuing if you use the code you provided.

    You could also test this yourself like this:
    Simba Code:
    function One : boolean;
    begin
      result := false;
      Writeln('One ran');
    end;

    function Two : boolean;
    begin
      result := true;
      Writeln('Two ran');
    end;

    begin
      if One or Two then
        Writeln('Expression is true');
      Writeln('Next');
    end.
    Edit: You should use a more descriptive title next time as I almost skipped it .

    You can do this if you want short circuit evaluation:
    Simba Code:
    function One : boolean;
    begin
      result := false;
      Writeln('One ran');
    end;

    function Two : boolean;
    begin
      result := true;
      Writeln('Two ran');
    end;

    begin
      if One and (Two or One) then
        Writeln('Expression is true');
      Writeln('Next');
    end.
    Last edited by Sex; 04-04-2012 at 06:53 PM.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  3. #3
    Join Date
    Jan 2012
    Posts
    1,104
    Mentioned
    18 Post(s)
    Quoted
    211 Post(s)

    Default

    Thanks for the answer, but you my point was to have the first function returned with True so second function no need to be compiled. So second function is like a second method of doing the job. Like this:
    Simba Code:
    function One : boolean;
    begin
      result := True;
      Writeln('One ran');
    end;
    function Two : boolean;
    begin
      result := False;
      Writeln('Two ran');
    end;
    begin
      if One or Two then
        Writeln('Expression is true');
      Writeln('Next');
    end.

    Output is:
    Code:
    One ran
    Expression is true
    Next
    EDIT: I dont really understand the usage of ur second code (short circuit)... if One is true, then the line is true, if Two is true, One needs to be true too....What is this for?
    Last edited by Shatterhand; 04-04-2012 at 07:26 PM.

  4. #4
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Ah, in that case.
    Simba Code:
    function One : boolean;
    begin
      result := true;
      Writeln('One ran');
    end;

    function Two : boolean;
    begin
      result := false;
      Writeln('Two ran');
    end;

    begin
      if not One and (Two or One) then
        Writeln('Expression is true');
      Writeln('Next');
    end.

    Oh wait...I guess PS does use short circuit evaluation for its and/or operators...
    Therefore your code you gave would do exactly what you want it to .
    , I feel like an idiot lmao.
    Last edited by Sex; 04-04-2012 at 09:33 PM.
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •