dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: Conditional Operator (? :)

  1. #1
    Join Date
    Jul 2017
    Posts
    9

    Conditional Operator (? :)

    Hello,
    I try to translate a javascript program to Delphi.
    My question is about Conditional Operator (? : )

    I have to translate the following line:

    return cw > 0 ? true : cw < 0 ? false : true; // colinear
    cw is extended, floating?

    How can I write this in Delphi or pascal?

    Sincerely

    Peter

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,395
    Its called a Ternery and as the dictionary term stats, it comprises of three parts, the condition and the expression return values make the other two party.

    If you google for it, you will find the answer.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Jul 2017
    Posts
    9
    Hello,
    Thanky you for your contribution!

    I now now about Ternery, but I still would need a translation!

    Any help appreciated!

    Peter

  4. #4
    Join Date
    Jul 2017
    Posts
    9
    Hello,

    Would this be the right translation?
    Javascript:

    return cw > 0 ? true : cw < 0 ? false : true; // colinear

    Delphi:
    if(cw > 0) then
    begin
    bool := True;
    end
    else
    begin
    if(cw < 0) then
    begin
    bool := True;
    end
    else
    begin
    bool := False;
    end;
    end;
    Result := bool;

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,395
    Ooops... Sorry, my typo, its Ternary...

    I can't help as I have not been exposed to Delphi, its why I suggested you google for it, some search like convert JavaScript ternery to Delphi and see what pops up.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  6. #6
    Join Date
    Mar 2011
    Posts
    124
    "return cw >= 0;" is doing the same as that double ternary expression.
    I haven't used delphi/pascal for over 15 years.
    Maybe you can just do something like "Result := (cw >= 0);" or "exit (cw >= 0);".

  7. #7
    Join Date
    Jul 2017
    Posts
    9
    Kever,
    So it is shorter than in Javascript!
    Thank you a lot.

    Sincerely
    Peter

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
  •  
HTML5 Development Center