Like Tree1Likes
  • 1 Post By phantomotap

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

This is a discussion on What is happening in the == with functions and loops? within the C++ Programming forums, part of the General Programming Boards category; This is really confusing. I just started learning c++ from the PDF i bough from the cprogramming website. Il give ...

  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
    2,706
    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
    2,706
    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 11:13 AM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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 ?
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    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.
    manasij7479 likes this.

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, 01:49 PM

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