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?
10 3254
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?
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.
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.
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.
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.
Ganon11, thanks - I saw your reply only after I'd submitted mine!
So no simpler way, then?
Why do you have to do it with floats?
Why do you have to do it with floats?
It's the accuracy range I'm working in.
It's a numerical methods program that I'm actually writing, and I have to calculate function values at x starting with 0.0 and incrementing by 0.1 until x is 1.2, using the function value at each step to compute the next one. I have to repeat this process over the same interval with increment 0.01. The values I have to store are at 0.0, 0.2, etc, so I somehow have to isolate them in the middle of the computations.
The code fragment is:
while (x <= 1.2) {
if (x == 0.2*save) {
A[save][col] = y;
save++;
}
if (x != 1.2) {
derv = der(x,y);
y = y + h*derv;
x = x + h;
}
}
What about multiplying everything by 10 and converting to int? You can always divide back later.
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?
As I said, you can write your own short function to do this. Out of curiosity, I wrote a function to do this (it only took me 3 lines of code), which would make your code look like: - x = 0.1;
-
while (x < 1.3) {
-
if (dequals(x, 0.9) || dequals(x, 0.5)) {
-
cout << "Helloooooo!!!" << endl;
-
}
-
x = x + 0.1;
-
cout << x << endl;
-
}
I called it dequals for double equals. It is a bool returning function, and returns true if the first argument is within EPS of the other, where EPS is a constant I defined to be about 0.000000001
[quote=Ganon11]
Ah, I see ... will give it a bash.
All right, that seems to be working ... The accuracy isn't great, though, so that's the next thing I have to tackle, but this sorts the question on the if statement. Thanks! Bev
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David |
last post by:
Hi,
Quick question.
If I have a recordset value in RS("ProductName"), is it possible to
test for a part string in this value ?
I would be looking for all products starting 'Blue' and with...
|
by: James E Koehler |
last post by:
I can't get the WHILE statement to work in MySQL.
The version of MySQL that I am using is:
Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32)
running on Windows MX.
Here is the relevant...
|
by: ale.of.ginger |
last post by:
Greetings! I am trying to make a multiplayer (no AI, 2 person) game of
tic tac toe in Python. So far it has been pretty simple. My only
concern is with the win checking to see if a person has...
|
by: candy |
last post by:
hi all,
Is there is any way in the C language by which I can get the
address of a statement? For eg,consider the following simple program:
1. #include<stdio.h>
2.
3. int main(void){
4. ...
|
by: PeterZ |
last post by:
Hi,
Back to basics!
My understanding is that the only way to exit a For-Next loop prematurely is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch...
|
by: Steve |
last post by:
Hi All
In VB6 I used to catch exceptions in a goto errtrap call then resume if I
could handle the problem and continue within the routine or function
I am not sure how to do this in VB 2005...
|
by: Rui Maciel |
last post by:
I've been delving into finite state machines and at this time it seems that
the best way to implement them is through an intense use of the goto
statement. Yet, everyone plus their granmother is...
|
by: rn5a |
last post by:
In a ASP applicatiuon, the FOrm has a textbox & a select list where
the admin can select multiple options. Basically the admin has to
enter the name of a new coach in the textbox & select the...
|
by: nico3334 |
last post by:
Hi,
I have an IF..Then statement that I am using to determine which procedure to Call. Within the "else" section, I want to use a For..Next coding to loop through 3 different "call procedures". ...
|
by: rebecky via AccessMonster.com |
last post by:
Can anyone help with this statement? I get a syntax error message.....not
good at this at all.
Thanks
DoCmd.RunSQL "Update JOTable" _
& "INNER JOIN ON JOTable.JOID = .
JOID " _
& "Set...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |