Useful alternative control structures?

后端 未结 28 974
礼貌的吻别
礼貌的吻别 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 02:53

    How about

    alternate {
        statement 1,
        statement 2,
        [statement 3,...]
    }
    

    for cycling through the available statements on each successive pass.

    Edit: trivial examples

    table_row_color = alternate(RED, GREEN, BLUE);
    
    player_color = alternate(color_list); // cycles through list items
    
    alternate(
        led_on(),
        led_off()
    );
    

    Edit 2: In the third example above the syntax is maybe a bit confusing as it looks like a function. In fact, only one statement is evaluated on each pass, not both. A better syntax might be something like

    alternate {
        led_on();
    }
    then {
        led_off();
    }
    

    Or something to that effect. However I do like the idea that the result of which ever is called can be used if desired (as in the color examples).

提交回复
热议问题