CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Code

  1. #1
    Join Date
    Jun 2011
    Posts
    10

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  3. #3
    Join Date
    May 2000
    Location
    Armenia
    Posts
    201

    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.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Code

    Quote Originally Posted by Lilcrew View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured