'Program is exitting 'while' loop even though conditions are not met

Simple excercise im trying out. The block of code shown tests if Username is taken and the Password strength.

Program should exit 'while' loop if both the variables equal true:

(while(boolUserCheck == false && boolPwdCheck == false){})

Program is exitting loop eventhough boolUserCheck == true and boolPwdCheck == false. In my test, i create a Username that is free (ie. boolUserCheck = true) and i create a weak password (ie. boolPwdCheck = false).

NOTE: I haven't tried the case where boolUserCheck == false and boolPwdCheck == false because i haven't finished the program so im not actually storing the user data anywhere to check taken Usernames.

I've checked that i used equal operators(==) where needed and not assignment operator(=). I used AND condition because BOTH need to be TRUE to exit. Return values are what i expected and so they enter their respective 'if' statements.

while(boolUserCheck == false && boolPwdCheck == false){
    std::cout << "Username: "; gets(name);
    std::cout << "Password: "; gets(pwd);

    boolUserCheck = UserCheck(name);   // Function that checks if Username is free (returns true)
    if(boolUserCheck == false){
        std::cout << "Username taken."; std::cin.get();
    }
    else{
        boolPwdCheck = PwdCheck(pwd);   // Function that checks password strength (returns false)
        if(boolPwdCheck == false){      // Therefore enters the 'if' statement
            std::cout << "Password must contain upper and lower case and special characters: !@#$%^&*<>()";
        }
    }
    //   I added this to check the values, which are: true and false respectively
    //   And yet program exits while loop
    std::cout << "\nboolUserCheck: " << boolUserCheck << "\nboolPwdCheck: "  << boolPwdCheck;
    std::cin.get();
}


Solution 1:[1]

I used AND condition because BOTH need to be TRUE to exit

TL;DR: Exiting condition is both need to be true, meaning the running condition must be that, but inverted to boolUserChecked == false || boolPwdChecked == false


Here's the long way

Let's check all our possible combinations using your condition
boolUserCheck boolPwdCheck Result
true true false
true false false
false true false
false false true

This states that the loop is only running if both are false, but the condition you're looking for is:

both need to be true to exit

meaning inversely if either is false, you need to keep running.
That, in program logic, is: boolUserChecked == true && boolPwdChecked == true to exit and !(boolUserChecked == true && boolPwdChecked == true) to keep running.
The condition inside your while loop decides whether it should keep running, thus you need the latter of them.
Using arithmetic you can rewrite that, without going into too much detail, arriving at boolUserChecked == false || boolPwdChecked == false

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tidemor