variable addition to loop

Hi There,

I am having this problem with these for loops. Whenever I add "ydisplacement2" to the loop, some results become gibrish. ydisplacement2 will eventually change value through a different loop. These are the loops:


1
2
3
4
5
6
7
8
float ydisplacement2 = 0.0, n, n1

for (n = 3 + ydisplacement2; n <= 70; n = n + 5)

 { for (n1 = 4.0 ; n1 <= 39; n1 = n1 + 5) {


}}



Any help will be appreciated.
1
2
3
4
5
6
7
8
9
float ydisplacement2 = 0.0, n, n1 //need a semicolon ;

for (n = 3 + ydisplacement2; n <= 70; n = n + 5) {
//                            can be  n += 5     but whatever
	for (n1 = 4.0 ; n1 <= 39; n1 = n1 + 5) {
	//                        n1 += 5
		//... more code here I assume?
	}
}

sanehatter wrote:
... some results become gibrish.
Can you be more specific?
Last edited on
closed account (zb0S216C)
When you say: some results become gibrish, are you referring to output of any kind?
Mathhead200: yes there is more code inside and thanks for the semicolon I have it in the actual code.

Framework and Mathhead200: by gibrish I mean the results (output) alternate from actual real numbers to some "1.#Qnn.."
I don't see anything in your code that looks like it would cause that. Can you post a bit more?
Ok just let me be a bit more specific; is the syntax for both loops correct?
Last edited on
Sure. Although, usually a for-loop's variable is an int or some other enumerated data type. Is there a reason you're using float here?
Yes I am using float because ydisplacement2 is a float and both n, n1 may become floats at some point.
closed account (zb0S216C)
The only difference between a float and an int in a for loop is precision. It doesn't matter which numerical type you use.
If your not careful you can get weird floating point precision errors. If you only want int's you should use int. However I doubt that's where the problem is. Your for-loops look fine.
Thank you Mathhead200 and Framework.
Topic archived. No new replies allowed.