Unary Minus (negation) - Trying to Understand Problem.

Hi, thanks for having this online spot to ask questions. 'am new in every way.

I've two sets of code. One which was given to me. My problem surrounds the very first instanced value of 'val' as seen below. The way it is, I'm happy with the result because it's exactly what I'd have expected. 20

1
2
3
4
5
6
7
8
9
10
11
12

#include <iostream>
using namespace std;

int main() {
	
	float val = -10.0; 
	val = -val * 2;
	cout << val;
	return 0;
}


Problem is in the following which I took apart using the example above.
The very first result of the one below comes out to -10. Something I'm finding so very hard to understand why. The negation just before val at the end of that line should automatically turn that -10 into +10, no? What am I missing???
And also, why would the first result be -10 instead of say -20? I mean, the number itself is already not what I expected not to mention the negative signing of it.

Thank you most kindly in advance.

1
2
3
4
5
6
7
8
9
10

#include <iostream>
using namespace std;

int main() {
	for (float val = -10.0; val < 100.0; val = -val * 2)
		cout << "*" << " val = " << val << endl;
	return 0;
}
Last edited on
I think it is a case of -10 * -2 = 20
Please read: http://www.cplusplus.com/doc/tutorial/control/#for

I think you might be thinking about it a little bit backwards. First thing that happens is that val is assigned the value of -10.0, then the condition val < 100.0 is checked (which in this case is true since val = -10.0). Since the condition is true, the loop continues execution. At this point, from what I understand from your post, you think that the next thing that executes is val = -val * 2? Rather, after the condition is checked, the block of code for the loop executes (in this case, it's just line 7) thus the program prints out -10. After the block of code executes, the val = -val * 2 part is executed and val now equals 20. The condition is then checked and the loop either finishes (if the condition is false) or continues with another iteration (if the condition is true) which, in your code, displays the new value of val: 20. It keeps doing this until val >= 100.

Essentially, the condition is checked before the iteration of the loop, and the increment happens after the iteration of the loop. (The initialization of val happens only once when the for loop is initially reached.)

Side note: Usually it is considered bad practice to use floating point numbers as a counter in a loop. In this case I don't see it as being an issue*, but be aware of it (it's also a good habit to get into).

Hope that helps.

* if my understanding of the floating point format is correct, it can hold the non fractional component of the number reliably. I may be wrong though, someone will hopefully chime in.
Thank you very much Danny. ;-)

I see now. The loop-expression doesn't get done on the first run through but rather the point thereafter (2nd time around).

Also just quickly created a little program to check that out. If figures with what you've taught me.

...'and thanks for the suggestion about 'floats'. Had borrowed this code from a tutorial test I was going through.

Topic archived. No new replies allowed.