C/C++: goto into the for loop

前端 未结 7 1595
一向
一向 2021-02-13 21:14

I have a bit unusual situation - I want to use goto statement to jump into the loop, not to jump out from it.

There are strong reasons to do so - this c

7条回答
  •  名媛妹妹
    2021-02-13 21:32

    The initialisation part of the for loop will not occur, which makes it somewhat redundant. You need to initialise i before the goto.

    int i = 0 ;
    if( not_a_first_call )
        goto request_handler;
    for( ; i

    However, this is really not a good idea!

    The code is flawed in any case, the return statment circumvents the loop. As it stands it is equivalent to:

    int i = 0 ;
    
    if( not_a_first_call )
        \\...process_data...
    
    i++ ;
    if( i < n )
    {
        *data_to_request = i;
    }
    

    In the end, if you think you need to do this then your design is flawed, and from the fragment posted your logic also.

提交回复
热议问题