if while循环中的语句 [英] if statement within while loop

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

问题描述

我想在while循环的每次重复时测试一个条件(使用if),但是一旦条件第一次完成,它就会在while循环的其余部分被绕过。

例如。

x = 0.1;

而(x <1.3){

if(x == 0.9 || x == 0.5){

cout<< " Helloooooo !!!" << endl;

}

x = x + 0.1;


}

打印Helloooooo !!!"只有一次,x = 0.5,而不是x = 0.9。


有没有办法让它每次评估条件?

I would like to test a condition (using "if") on each recurrence of a while loop, but once the condition has been fulfilled the first time, it is bypassed for the rest of the while loop.
eg.
x = 0.1;
while (x < 1.3) {
if (x == 0.9 || x == 0.5) {
cout << "Helloooooo!!!" << endl;
}
x = x + 0.1;

}
It prints "Helloooooo!!!" once only, at x = 0.5, not again at x = 0.9.

Is there a way to get it to evaluate the condition every time?

推荐答案


我想在while循环的每次重复时测试一个条件(使用if),但是一旦满足条件,第一次,它在while循环的其余部分被绕过。

例如。

x = 0.1;

而(x <1.3){

if(x == 0.9 || x == 0.5){

cout<< " Helloooooo !!!" << endl;

}

x = x + 0.1;


}

打印Helloooooo !!!"只有一次,x = 0.5,而不是x = 0.9。


有没有办法让它每次都能评估一下情况?
I would like to test a condition (using "if") on each recurrence of a while loop, but once the condition has been fulfilled the first time, it is bypassed for the rest of the while loop.
eg.
x = 0.1;
while (x < 1.3) {
if (x == 0.9 || x == 0.5) {
cout << "Helloooooo!!!" << endl;
}
x = x + 0.1;

}
It prints "Helloooooo!!!" once only, at x = 0.5, not again at x = 0.9.

Is there a way to get it to evaluate the condition every time?



这可能与你使用浮点数来循环而不是整数这一事实有关。在某些系统中,当你以浮点值递增时,你会得到类似于x = x + 0.0999999999的东西,它不是0.1,但仍然非常接近。尝试使用整数并告诉我它是如何工作的。

This probably has something to do with the fact that you are using floats to do your looping instead of ints. In some systems when you increment by a float value you get something akin to x = x + 0.0999999999 which is not exactly 0.1 but still pretty close. Try doing it with ints and let me know how it works.


嗯......我得到了相同的结果。我认为这是由于舍入错误 - 你的x大约为0.9,但很可能实际上是0.8999999999992或0.900000000001或其他一些荒谬接近0.9的分数。因为这不是0.9,所以if语句不会执行。


当试图比较双(或浮点)值时,你应该检查数字是否接近到你的目标 - 关闭在0.000000001或一些适当的小值。您可以为此编写自己的函数。
Hmm...I''m getting the same result. I think this is due to roundoff error - your x is about 0.9, but most likely it is actually 0.8999999999992 or 0.900000000001 or some other fraction absurdly close to 0.9. Since this isn''t exactly 0.9, the if statement doesn''t execute.

When trying to compare double (or float) values, you should instead check if the number is close to your target - close being within 0.000000001 or some appropriately small value. You can write your own function for this.



这可能与您使用浮点数进行循环这一事实有关而不是整数。在某些系统中,当你以浮点值递增时,你会得到类似于x = x + 0.0999999999的东西,它不是0.1,但仍然非常接近。尝试使用整数,让我知道它是如何工作的。
This probably has something to do with the fact that you are using floats to do your looping instead of ints. In some systems when you increment by a float value you get something akin to x = x + 0.0999999999 which is not exactly 0.1 but still pretty close. Try doing it with ints and let me know how it works.



谢谢!是的,它适用于整数。我必须用浮子来做。我刚试过这段代码:


x = 0.1;

而(x <1.3){

if((x <0.905&& x> 0.895)||(x> 0.495&& x< 0.505)){

cout<< " Helloooooo !!!" << endl;

}

x = x + 0.1;

cout<< x<<结束;

}


它完成了这项工作,但它看起来有点笨拙......?你有任何想法,或者是否有其他方法可以完全采取?


给出一些背景:我实际在做什么(编写这段代码只是为了隔离我遇到的问题是,例如,计算x的值增加0.1,但是只使用if和if存储数组中的特定值。循环以获取我想要存储的值。

Thanks for that! Yes, it works with integers. I have to do it with floats though. I''ve just tried it with this code:

x = 0.1;
while (x < 1.3) {
if ((x < 0.905 && x > 0.895) || (x > 0.495 && x < 0.505)) {
cout << "Helloooooo!!!" << endl;
}
x = x + 0.1;
cout << x << endl;
}

and it does the job, but it seems a bit clumsy ... ? Do you have any ideas, or is there maybe another approach to take altogether?

To give some context: what I''m actually doing (having written this bit of code simply to isolate the problem I was having) is computing values of y for values of x incrementing by 0.1, for example, but storing only particular ones in an array, using the "if" loop to pick up the values I want to store.


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

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