Useful alternative control structures?

后端 未结 28 913
礼貌的吻别
礼貌的吻别 2021-01-30 02:20

Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my

28条回答
  •  清酒与你
    2021-01-30 03:15

    if (cond)
       //do something
    else (cond)
       //do something
    else (cond)
       //do something
    first
       //do something
    then
       //do something
    else (cond)
       //do something
    else
       //do something
    end
    

    FIRST and THEN blocks runs if any of 3 conditionals are evaluated to true. FIRST block runs before the conditional block and THEN runs after the conditional block has ran.

    ELSE conditional or final write following FIRST and THEN statement are independent from these blocks.

    It can read as :

    if (cond)
       first()
       //do something
       then()
    else (cond)
       first()
       //do something
       then()
    else (cond)
       first()
       //do something
       then()
    else (cond)
       //do something
    else
       //do something
    end
    
    
    function first()
       //do something
    return
    function then()
       //do something
    return
    

    These functions are just a form to read. They wouldn't create scope. It's more like a gosub/return from Basic.

    Usefulness and readability as matter of discussion.

提交回复
热议问题