for语句中有多个条件 [英] Multiple conditions in for statement

查看:318
本文介绍了for语句中有多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在函数中有这个代码,但是当它运行时,它会长时间暂停,然后它说:

I have this code in a function, but when it runs it does a long pause and then it says:

$floating point exception



我假设这是由于for循环中的多个条件,知道为什么它是错的。任何想法?

I am assuming this is due to multiple conditions in the for loop, but I don't know why it is wrong. Any ideas?

int i,j,number=5;
for (i = 2; (i < number || j==1); i++)
{
    if (number%i==0)
    {
        j = 1;
    }
}


推荐答案

strong>浮点异常 - 这意味着有一个算术错误。

floating point exception - This means there's an arithmetic error.

看起来你想用j停止循环, (因为一旦你得到 j == 1 条件总是 true )。

然后发生的是你通过整数值回到 0 并获取异常,循环 i

It looks like you're trying to stop the loop with j, but what you're actually doing is continuing the loop forever (because once you get j==1 the or condition is always true).
What then happens is you loop i through all the Integer values back to 0 and get the exception.

我想你想做的是:

for (i = 2; (i < number); i++)
{
    if (number%i==0)
    {
        j = 1;
        break;
    }
}

这篇关于for语句中有多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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