Announcement

Collapse
No announcement yet.

Alternate Switch-Case coding

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Alternate Switch-Case coding

    I know its a bad coding practise to use goto-label but since switch-case is not working in 4DGL, is the below code a better alternative or are there a better way to handle these?
    Thanks

    func checkSelection(var itemSelected)
    var giveDiscount;
    if (itemSelected==10) giveDiscount := 0.1; goto okDone; endif if (itemSelected==20) giveDiscount := 0.15; goto okDone; endif if (itemSelected==30) giveDiscount := 0.17; goto okDone; endif
    okDone: return giveDiscount;
    endfunc

  • #2


    First thing to note, 4DGL is 16bit integer based and does not support float,so you will need to scale the values somehow.

    It would be more code efficient this way:-

    func checkSelection(var itemSelected)var giveDiscount;
    if (itemSelected==10) giveDiscount := 1; else if (itemSelected==20) giveDiscount := 15; else if (itemSelected==30) giveDiscount := 17; endif return giveDiscount;endfunc

    and could also be done like

    func checkSelection(var itemSelected) if (itemSelected==10) return := 1; else if (itemSelected==20) return := 15; else if (itemSelected==30) return := 17;
    else
    return 0;
    endif
    endfunc
    Regards,
    Dave

    Comment


    • #3


      Also, if your data set is fixed, you can do the following:
      #DATA word discount 0, 1, 15, 17, 25, 32, 64 //Added a few extra#END
      func checkSelection(var itemSelected) return (discount[lookup16(itemSelected,10,20,30,40,50,60)]);endfunc
      If the item is not found in the list, then a zero is returned (no Discount). If the itemsSelected was 10, then: lookup16(itemSelected,10,20,30,40,50,60) will return a 1, and discount[1] is a 1, which gets returned.
      This works esp. well if you need to check a lot of values.......
      _______________
      Best Regards,
      Howard

      Comment


      • #4


        Thanks Dave. OK, noted on the float. I'll make changes to the code on that.
        Also, is there a limit as to how many nested if-elseif in 4DGL? I ask because I would foresee a lot more checking of values than the example I posted.

        Comment


        • #5


          Thanks Howard. Yes, lookup16() would be most appropriate.

          Comment

          Working...
          X