Useful alternative control structures?

后端 未结 28 1329
礼貌的吻别
礼貌的吻别 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条回答
  •  旧时难觅i
    2021-01-30 03:10

    Labeled loops are something I find myself missing sometimes from mainstream languages. e.g.,

    int i, j;
    for outer ( i = 0; i < M; ++i )
        for ( j = 0; j < N; ++j )
            if ( l1[ i ] == l2[ j ] )
               break outer;
    

    Yes, I can usually simulate this with a goto, but an equivalent for continue would require you to move the increment to the end of loop body after the label, hurting the readability. You can also do this by setting a flag in the inner loop and checking it at each iteration of the outer loop, but it always looks clumsy.

    (Bonus: I'd sometimes like to have a redo to go along with continue and break. It would return to the start of the loop without evaluating the increment.)

提交回复
热议问题