Help with tutorial on C++

This is a discussion on Help with tutorial on C++ within the C++ Programming forums, part of the General Programming Boards category; Ok so i'm trying to now figure our while, for, do. Its not explaning one thing: Can while be done ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    25

    Help with tutorial on C++

    Ok so i'm trying to now figure our while, for, do.
    Its not explaning one thing:
    Can while be done "while" for is happening?
    How can you use all 3 in 1 code.
    Heres what i tried to write:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int x=1;
    
      
      for (int x =1;x>=0||x<=10; x=x+2)
      { cout<<x<<endl;
    }
    cin.get();
    while (x==9) {cout<<x-9; }
      do {
        cout<<"Learning to count!\n";
      } while ( x !=0 || x !=1 ||x !=2 );
      cin.get();
    }

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    25
    Anyone, i can't understand.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    How can you use all 3 in 1 code.
    Your example is such an example, but the problem is that all your loops are infinite loops (once they start running, they never terminate), so actually running your code could pose a problem.

    Incidentally, you should format your code to make it more readable, e.g.,
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x = 1;
    
        for (int x = 1; x >= 0 || x <= 10; x = x + 2)
        {
            cout << x << endl;
        }
    
        cin.get();
    
        while (x == 9)
        {
            cout << x - 9;
        }
    
        do
        {
            cout << "Learning to count!\n";
        } while (x != 0 || x != 1 || x != 2);
    
        cin.get();
    }
    Incidentally, I note that you have two variables named x in main(). One is in the for loop.
    Last edited by laserlight; 06-20-2008 at 01:31 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Can while be done "while" for is happening?
    If i understand your question correctly, you want to enter a while loop inside a for loop, this is known as nested loops, and the answer is yes, it can be done

    Code:
    for(unsigned int i = 0; i < 10; i++)
    {
         unsigned int j = 0;
         while(j < 10)
         {
              j++;
         }
    }
    This means, that each time the for loop, loops 1 time, the while loop is looped 10 times (from 0 to 9).

    Is this what you meant?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    25
    Yes You Are A Genius!!!!!!!!!!!!!

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    you can also do a nested for loop inside with j.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 06:38 PM
  2. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 01:49 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21