Thread: A question about while loop

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    A question about while loop

    I thought I understood while and for loops quite well. However, recently I saw a program which had a while loop which only contained:

    while (1)
    Can anyone please help me understand as to what condition is being tested here for being false of true? Thanks a lot.

    Arooj
    Last edited by Arooj; 02-16-2003 at 09:36 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    while(1) == while(true)

    I have barely used it, actually come to think of it, I don't think I ever have. But it could be used to show infinite loops.

    1 == true

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    15
    Thanks Alpha, if it is an infinite loop, then how does the program terminate. Actually I had to write a program which keeps on taking input until it encounters 2 successive instances of zeros. This is how I wrote it. Tell me if this is the correc way.

    void ()
    {
    int num1, num2;
    cout << "Enter a digit: ";
    cin >> num1;
    cout << "Enter a digit: ";
    cin >> num2;
    while (1) {
    if (num1 == 0 && num2 == 0)
    break;
    cout << "Enter a digit: ";
    cin >> num1;
    if (num1 == 0 && num2 == 0)
    break;
    cout << "Enter a digit: ";
    cin >> num2;
    }

    }

    Can I instead write:
    while (num1 != 0 && num2 != 0)
    instead? I hope I made my problem clear. Thanks
    arj

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    then it will loop while num1 and num2 does not equal 0...so it will loop forever until num1 and num2 equal to zero...

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
        int num1,num2;
        cin >> num1 >> num2;
        while(num1!=0 && num2!=0) {
            num1 = num2;
            cin >> num2;
            if(!cin.good()) {
                cerr << "invalid input (expecting digit)" << endl;
                break; // or throw something
            }
    //      do something with num1 & num2
        }
    The previous code would fail on 5 0 0, assuming you wanted to stop on those consecutive zeros.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    15
    Thanks a lot. Actually program only checks 2 separate inputs to make sure both are not zeros. Thanks again.
    Arooj
    arj

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM