无限循环中的返回语句 [英] Return statement in an infinite loop

查看:21
本文介绍了无限循环中的返回语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个 return 语句在这个无限的 while(1) 循环中,它会返回到哪里?更重要的是,我不知道 while(1) 循环会被破坏???

Where does this return statement return to if it is inside this infinite while(1) loop? More importantly, I had no idea that a while(1) loop could be broken???

void __attribute__((interrupt, no_auto_psv)) _T3Interrupt(void)
{
int count;

    IFS0bits.T3IF = 0;          // clear Tmr3 interrupt flag

    StopMotor();
    IEC0bits.ADIE = 0;          // disable ADC interrupt
    IEC0bits.CNIE = 0;          // disable CN interrupt
    IEC0bits.T3IE = 0;          // disable Tmr3 interrupt

    T3CONbits.TON = 1;          // restart tmr3
    count = 0;

    while (1)
    {
        if (IFS0bits.T3IF)
        {
            IFS0bits.T3IF = 0;  // clear flag
            if (count++ >= RESTART_COUNT)
            {
                IEC0bits.ADIE = 1;          // enable ADC interrupt
                IEC0bits.CNIE = 1;          // enable CN interrupt
                T3CONbits.TON = 0;          // stop tmr3
                IEC0bits.T3IE = 1;          // enable Tmr3 interrupt
                return;
            }
        }
    }
    return;
}

推荐答案

所有的 return 语句都会返回到函数被调用的地方,不管它们在函数中的什么位置.

All return statements will return to wherever the function was called from, regardless of where they are located within the function.

例如,如果我写:

int main()
{
    _iT3Interrupt();

}

那么_iT3Interrupt中的return语句会将控制流恢复到main.

Then the return statement in _iT3Interrupt will revert control flow back to main.

此外,any 循环可以通过以下任何一种方式退出(即使条件是 1true 或其他等价物)构造:

Also, any loop can be exited (even if the condition is 1, true, or some equivalent) with any of the following constructs:

break; //exits the loop

return; //exits the function, thus ending the loop

goto <label-outside-loop>; //self-explanatory

exit(); abort(); //exits the program. Bit of a stretch to say that this ends the loop...

而在 C++ 中,throw 会展开堆栈,直到它到达相应的 catch,从而退出函数.C setjmplongjmp 函数在这里也可能适用,但我对 C 的了解还不够,无法确定它们的用法.

And in C++, throw, which will unwind the stack until it reaches a corresponding catch, thus exiting the function. The C setjmp and longjmp functions may also be applicable here, but I don't know enough C to be certain of their usage.

这篇关于无限循环中的返回语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆