-
June 17th, 2011, 05:03 PM
#1
Code
So I need to make it start from 1 and go up by odds so like 1 3 5 7
and I need to make it stop before it reaches the integer value
cout << "Loop 3: ";
for(i=1; i>=integer; i+= 2)
{
cout << i << " ";
}
it works but doesnt stop
-
June 17th, 2011, 05:08 PM
#2
Re: Code
Well, if you know the "the integer value" to stop if it has reached then what is the problem to check your i variable?
Victor Nijegorodov
-
June 17th, 2011, 11:17 PM
#3
Re: Code
...I need to make it stop before it reaches the integer value
Then why you are using i>=integer condition to end the for loop instead of using i < integer (or i <= integer)?
Last edited by Armen; June 17th, 2011 at 11:25 PM.
-
June 17th, 2011, 11:24 PM
#4
Re: Code
Originally Posted by Lilcrew
for(i=1; i>=integer; i+= 2)
{
cout << i << " ";
}
Until you understand how a for-loop works I suggest you use a while-loop instead. This is equivalent to the for-loop above.
Code:
i=1; // first parameter of for-loop
while (i>=integer) // second parameter
{
cout << i << " ";
i+= 2; // third parameter
}
As you can see you're looping WHILE i>=integer. Is that what you want?
Last edited by nuzzle; June 17th, 2011 at 11:47 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|