BHX wrote: |
---|
With if statements, the {} are only needed if you have more than one line to execute. if and for loops execute the very next line (so long as you don't do if(); /* or */ for(); |
Even though that is technically correct, it gives the wrong impression.
But it is still IMO a very good idea to use braces anyway, it will save you one day when you add more code later.
Not sure whether this was your point: Your statement reinforces the bad outcome, but the code shows how it can go bad - so there is a bit of conflict there.
IMO, better to make a statement that reinforces the behaviour that you do want, then show code to show how it operates.
I wonder if the
OP has cottoned on to what happens when a value is entered that is in between the limits of each else if: 94.45, 89.45, 85.45 etc.
I am also against
using namespace std;
and
do
loops. The latter can always be written as a while loop, albeit at the expense of 1 more variable, and that would be less error prone.
Btw, if one needs to do a null statement (rarely) then it should be in braces and commented, here is an excerpt From K&R C programming, which I added the braces & comment:
1 2 3 4 5 6 7
|
/* strcpy: copy t to s; pointer version 2 */
void strcpy(char *s, char *t)
{
while ((*s++ = *t++) != '\0') {
; /* null statement */
}
}
|
All these things I mention might seem a bit pedantic, but I mention them because they are good practise, and should aid in preventing silly but hard to see / find errors.
Hope all is well :+)