for loop help plz

How can i get a for loop to display a character, say "~", a certain number of times? thats all it needs to do. thanks
closed account (3bfGNwbp)
1
2
for(int i = 0; i != 10; ++i) // outputs '~' 10 times
       std::cout << "~";
1
2
for(int i = 0; i != 10; ++i) // outputs '~' 10 times
       std::cout << "~";


should be this

1
2
for(int i = 0; i < 10; ++i) // outputs '~' 10 times
       std::cout << "~";


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.
@TheIdeasMan

I agree, I use this myself though I've seen the former syntax in some books, but why is this discouraged? Also, when we're working with iterators, we do this for instance

1
2
std::vector<int> myvector;
for (std::vector<int>::iterator it=myvector.begin(); it != myvector.end(); it++ );


because we cannot compare pointers, but it's still used and doesn't cause a problem. So, the integer case shouldn't be any different, no?

Last edited on
Ok,

I guess I have always used the standard idiom, I haven't seen a reason to change it.

I have a vague notion that it is to do with changing the value of the index inside the loop.

1
2
3
4
5
 
for(int i = 0; i != 10; ++i)   {// outputs '~' 10 times
       std::cout << "~";
       i = 11;   //never ending loop
}


1
2
3
4
5
 
for(int i = 0; i < 10; i++)   {// outputs '~' 10 times
       std::cout << "~";
       i = 11;   // loop stops early
}


This could be quite hard to find in a bigger example.

With the vector, it is OK because the iterator incrementing is tightly controlled.
closed account (j2NvC542)
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.

Are you sure? I always use the prefix increment and during the first loop I always start with i == 0, as declared. To my understanding, the last element of the for-head is executed after the body.
yeah, ++i and i++ will achieve the same thing in the last parameter of a for loop, which is unexpected, I always assumed it was run at the start not the end
Last edited on
ok, I was wrong about the increment - it only matters when there is assignment as well. This is from K&R C programming pp43,44



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.


My only lame excuse is that I have always used the std idiom, and haven't seen a reason to change it.

I should be more careful with what I say - do more checking.
Thanks for all the answers guys, I am new to this tho. Can i change the i to any variable I want? Also is the "std ::" in front of cout needed?? I use microsoft visual and and have never used this before a cout....
closed account (j2NvC542)
The std:: in front of cout is not necessary if you are using namespace std;.
Please, please take this opportunity to learn what a namespace is. Sticking using namespace std; at the top of any serious1 code is often frowned upon.


1. I'm not going to define this precisely, but rest assured, if someone ever hands me a library with a header that has 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.
Another excellent footnote Moschops. :D

I do this for std namespace things:

1
2
3
4
5
using std::cout;
using std::cin;
using std::endl;

//etc for any std thing - vector, list etc 


This is my preference, I would rather put 20 using statements at the top of the file, instead of 200 std:: every where.

Has anyone had any problems doing this?
Yes masta Moschops!!, please dont beat me masta!! lol jk


Well I am in a beginner class and we HAVE been putting namespace at the top of the code. Thats the way we have to do it. What about the "i" can i change it?
I wouldn't expect a tutor to mark you down if you use std:: or do what I do, especially if you can explain why.

With the variable i in the for loop, I would change i != 10 to i < 10, I am pretty sure I was right about that part. As for ++i or i++ it doesn't matter, as per my quote from K&R.

Edit:

IF you want to use a different variable name go ahead.
Last edited on
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.


That's not serious code so it's fine. :)
Topic archived. No new replies allowed.