|
|
|
|
|
|
|
|
|
|
also, be careful with ++i, if i is going to be used within the loop. The prefix increment changes the value before it enters the body of the loop. |
The unusual aspect is that ++ and -- may be used either as prefix operators (before the variable, as in ++n), or postfix operators (after the variable: n++). In both cases, the effect is to increment n. But the expression ++n increments n before its value is used, while n++ increments n after its value has been used. This means that in a context where the value is being used, not just the effect, ++n and n++ are different. If n is 5, then x = n++; sets x to 5, but x = ++n; sets x to 6. In both cases, n becomes 6. The increment and decrement operators can only be applied to variables; an expression like (i+j)++ is illegal. In a context where no value is wanted, just the incrementing effect, as in if (c == '\n') nl++; prefix and postfix are the same. |
using namespace std;
.
using namespace std;
at the top of any serious1 code is often frowned upon.using namespace std;
at the top, I will beat said coder to death with a mid-eighties IBM Model M keyboard as a service to humanity.
|
|
i
is just a convention for a loop variable, short for index (or increment?). You can use any legal C++ variable name your little heart desires. Of course following conventions tends to make your code more understandable to others (including Moschops who I hear beats coders with antiquated hardware so...).
Well I am in a beginner class and we HAVE been putting namespace at the top of the code. |