@tinaHR,
Ok, working backwards,
About bool and test
I changed variable type to bool. How come its still working being an int type? does that have some other uses?
|
It isn't working as an integer, and you never used "test" as an integer. The keywords "false" and "true" are both BOOL types, not integers. There is automatic conversion available in some cases, where "false" is zero, but for bool's any non-zero evaluates to "true".
If you have any confusion about "test" and it's nature as a bool ask, because I'm not sure why you though it "still working being an int type" - because it isn't. In code analysis tools and high warning levels I'd expect some situations where "false" and "true" are used with integers there should be warnings.
Put another way, despite the "colloquial" reading of the various "if()" clauses, what is tested by "if" statements is a bool. For example, if ( a ) where "a" is an integer is NOT testing "a"'s value, it is testing the boolean test of a for zero or not zero. Likewise, if ( a < b ) doesn't really care about a or b - conceptually the compiler does test for "a"'s value compared to "b"'s value, but the RESULT of that comparison is "true" or "false" - becoming a BOOL, not an integer.
About "if" inside a loop
Every "if" statement does impose a performance penalty, but it is very small. In high performance programming we do think about such things, and take care to test before loops. In such work we actively limit the work inside interior loops.
Yet, that's only for interior loops (we often have loops inside loops, so we're hardly ever concerned about the outer loop level).
However, setting that theory aside for a later study, the performance impact of such an "if" inside a loop is in the region of 100 hundred millionth of a second, on modest hardware. Many machines could run that test a billion times per second or more.
Lazy initialization, the pattern used for this "initial" assignment of min/max, has a cost in this way. When performance is less important that convenience, the cost is minimal.
About the test of have_value_minmax
You have that working, and it's ok, but when you say.
if min and max received first value, have_value_minmax is set to true; |
you're saying that backwards from what you wrote. Indeed, your written text is the truth, and the code is inverted. You're setting it to false.
In this formation, the name would be more accurate "does_no_have_value_minmax", because when
that is true, you have not set minmax, and when it is false you have...you "don't have no value" is how that reads, which sounds odd.
Indeed, it is more natural to initialize "have_value_minmax" to
false, which is saying "there is no minmax set yet".
Then, the test itself is inverted:
1 2 3 4 5 6 7
|
if ( !have_value_minmax ) // the test is inverted now, NOT 'has minmax'
{
min = a;
max = a;
have_value_minmax = true; // now we do have minmax
}
| |
This expresses how you've worded this in text, and it just seems more natural to the mind.
Otherwise you have that all correct, because in reality it works either way, to check for "true" and set to "false" (basically the logic of "does this need initialization") or check "false" and set to "true" (saying "did we initialize yet").
For all of us in this study, our early months, maybe a year or two, we just happy to get something working, and we tend to think in terms of just getting code to work.
Later, we begin to think in terms of expression, in terms of writing what we think - as if we think in code. It develops as practices like this are repeated, so the time it takes to transform from "just getting the code working" to the "expression of thought" is related to how much practice one performs.