473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

if statement within while loop

12
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?
Apr 6 '07 #1
10 3254
RedSon
5,000 Expert 4TB
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.
Apr 6 '07 #2
Ganon11
3,652 Expert 2GB
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.
Apr 6 '07 #3
BevG
12
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.
Apr 6 '07 #4
BevG
12
Ganon11, thanks - I saw your reply only after I'd submitted mine!
So no simpler way, then?
Apr 6 '07 #5
RedSon
5,000 Expert 4TB
Why do you have to do it with floats?
Apr 6 '07 #6
BevG
12
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;
}
}
Apr 6 '07 #7
Rabbit
12,516 Expert Mod 8TB
What about multiplying everything by 10 and converting to int? You can always divide back later.
Apr 6 '07 #8
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1.         x = 0.1;
  2.         while (x < 1.3) {
  3.                 if (dequals(x, 0.9) || dequals(x, 0.5)) {
  4.                    cout << "Helloooooo!!!" << endl;
  5.                 }
  6.                 x = x + 0.1;
  7.                 cout << x << endl;
  8.         }
I called it dequals for doubleequals. 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
Apr 6 '07 #9
BevG
12
[quote=Ganon11]
Ah, I see ... will give it a bash.
Apr 6 '07 #10
BevG
12
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
Apr 6 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
4
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...
10
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...
81
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. ...
13
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...
4
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...
67
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...
2
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...
3
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". ...
16
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
1
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)...
1
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...
1
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....
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.