Thread: What is happening in the == with functions and loops?

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    What is happening in the == with functions and loops?

    This is really confusing. I just started learning c++ from the PDF i bough from the cprogramming website. Il give you the codes first then explain. These are two different codes so im not redeclaring the string.

    Code:
    string password;
           do
           {
    
               cout << "enter password" << endl;
               cin >> password;
           }
           while (password == "foobar");
           {
                cout << "Correct Password";
           }
    //////////

    Code:
     string password;
            while ( 1 )
                {
                    cout << "Please enter your password: ";
                    cin >> password;
                    if ( password == "foobar" )
                        {
                            break;
                        }
                }
            cout << "Welcome, you got the password right";

    Ok so what im confused about here is in the Do-While Loop its asking for the password that is "foobar" but when i type it in, the block does not execute, it keeps asking for the password. But in the loop below with the if statement, if i type in "foobar" the block does execute. I dont get why one executes, but the other doesnt when they are both written with ==

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I think your understanding of the "do-while" loop is slightly off. Loops

    Code:
    do
    {
        // something
    }
    while (true);
    As long as the argument for "while" is true (which it always is if you enter the correct password), "something" will keep executing.

    Try changing the argument in the "while" loop so that it becomes false when the correct password is entered, hence breaking the loop and executing the next line of code.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    I did forget to add that, for the top code (do while loop), if I did (password != "foobar) the code block executes when I type in foobar. But why does that work, when it looks like its asking for a password that is not foobar.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're not completely understanding the logic behind that loop.

    Code:
    string password;
    do
    {
        cout << "enter password" << endl;  // prompt user for the password
        cin >> password;
    }
    while (password != "foobar");
    // "while" password *is not equal* to "foobar" (condition true)
    //    go back and promp user for password
    // if password *is equal* to "foobar" (condition false, which means
    //    "correct" in this context), you no longer need to prompt the user
    //    for input - you break the loop and continue with your code
    
    // not part of the "do-while" loop
    cout << "Correct Password";
    I think the confusion lies in the fact that this arrangement requires a "false if correct" condition, which is counter-intuitive. The logic can be structured differently so that the code continues when "true is correct" - but what you have now works well.
    Last edited by Matticus; 06-12-2012 at 10:13 AM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This is perhaps one place where Pascal has a slight advantage over C/C++.

    In Pascal it's a Repeat..Until loop, meaning that the condition would be "until password = "foobar";

    In C/C++ the condition is reversed since the English words used have the reverse meaning (while vs until).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by iMalc View Post
    In C/C++ the condition is reversed since the English words used have the reverse meaning (while vs until).
    How is the while loop's meaning different from its English meaning ?
    I've always interpreted it as
    Code:
    while (something_is_true)/* you can continue doing the following, until you wish to take a break*/
     { }
    Is that use invalid in conventional english ?

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    How is the while loop's meaning different from its English meaning ?
    It isn't. You misunderstood what he said.

    Code:
    The "while" in `do {something();} while(condition);' is a different
    beast from the "until" in `repeat {something();} until(!condition);'.
    The condition is reversed because of the meanings of "while" and "until" and not because "while" has a different meaning in code and in English.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C functions using loops
    By Noobpoint in forum C Programming
    Replies: 19
    Last Post: 02-13-2012, 10:31 PM
  2. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 01:30 PM
  3. Loops in functions?
    By AmbliKai in forum C Programming
    Replies: 16
    Last Post: 11-29-2007, 06:33 AM
  4. new to Void functions and loops
    By Loraine13 in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 10:00 AM
  5. Loops OR Functions!! HELP WITH THIS CODE!!!!!!!!!!
    By WIshIwasGooD in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2001, 12:49 PM