return语句在无限循环 [英] Return statement in an infinite loop

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

问题描述

哪里这个return语句返回,如果它是这无限的,而(1)循环内?更重要的是,我不知道,一段时间(1)循环可能被打破???

 无效__attribute __((中断了no_auto_psv))_T3Interrupt(无效)
{
诠释计数;    IFS0bits.T3IF = 0; //清零TMR3中断标志    StopMotor();
    IEC0bits.ADIE = 0; //禁止ADC中断
    IEC0bits.CNIE = 0; //禁止CN中断
    IEC0bits.T3IE = 0; //禁止TMR3中断    T3CONbits.TON = 1; //重新启动TMR3
    计数= 0;    而(1)
    {
        如果(IFS0bits.T3IF)
        {
            IFS0bits.T3IF = 0; //清除标志
            如果(计数++盐= RESTART_COUNT)
            {
                IEC0bits.ADIE = 1; //允许ADC中断
                IEC0bits.CNIE = 1; //允许CN中断
                T3CONbits.TON = 0; //停止TMR3
                IEC0bits.T3IE = 1; //使TMR3中断
                返回;
            }
        }
    }
    返回;
}


解决方案

所有return语句将返回到哪里的函数从名为,无论它们位于函数中的位置。

举例来说,如果我写的:

  INT的main()
{
    _iT3Interrupt();    返回0;
}

然后return语句将恢复控制流回

此外,的可以退出循环(即使条件是 1 真正,或者一些相当)以下任一构造:

 突破; //退出循环返回; //退出函数,从而结束循环转到<标签外循环取代; //言自明出口();中止(); //退出程序。舒展的位,这不能不说结束循环...

和C ++中,罚球,直至达到相应的catch,从而退出功能,将展开堆栈。的C 的setjmp 的longjmp 函数也可以适用在这里,但我不知道够不够C到肯定的它们的用法,

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;
}

解决方案

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

For instance, if I wrote:

int main()
{
    _iT3Interrupt();

    return 0;
}

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

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...

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,

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

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