IIF

Is possible to make Visual Cobol code shorter similar to something like that ?

string ReturnValue = Expression == true ? TruePart : FalsePart;

Using this the code would be shorter by 4 lines.

Tags:

  • Verified Answer

    We don't have any direct equivalent for this in COBOL syntax.  Of course, it's possible to do this in two lines in COBOL:

    declare ReturnValue as string

    if Expression set ReturnValue to TruePart else set ReturnValue to FalsePart end-if

    ...but probably most people would split this and use normal indentation for the sake of readability.

    I think the C# (and C) '?' syntax is probably most useful in contexts like parameter passing, and it's certainly possible we might want to add a construct like this to the language at some point.

  • Ok, thank you.

    It's certainly possible to do it in two lines and it occurred to my mind but that's not what I'm looking for. I just like my code being simple but readable. That's why I very agree with you as for readability of code. But sometimes there're very simple parts of code with IF statement like following

    IF (l-anItem <> NULL)

        set x to 1

    ELSE

        set x to 0

    END-IF

    and in those cases such a syntax would be useful. Something like

    move IIF(l-anItem <> NULL, 1, 0) to x

    compute x = IIF(l-anItem <> NULL, 1, 0)

  • Ok, thank you.

    It's certainly possible to do it in two lines and it occurred to my mind but that's not what I'm looking for. I just like my code being simple but readable. That's why I very agree with you as for readability of code. But sometimes there're very simple parts of code with IF statement like following

    IF (l-anItem <> NULL)

        set x to 1

    ELSE

        set x to 0

    END-IF

    and in those cases such a syntax would be useful. Something like

    move IIF(l-anItem <> NULL, 1, 0) to x

    compute x = IIF(l-anItem <> NULL, 1, 0)

  • What you can do is write a Integrated Pre-processor that would expand the IIF syntax into standard Cobol code. That would mean that you code and animate the IIF statement, but actually execute the expanded code.

    For more information have a look at the Integrated Preprocessor Interface section of the documentation.