C # simple IF OR question

Sorry to ask about this, as it seemed to me that I know the answer, I want to exit the program if the username is more than 4 characters or the username is not an account called a student. However, this is even if the username is only 3 characters and is not a student, I am still in the Application.Exit application. What am I doing wrong?

if (userName.Length > 4 | userName != "student")
{
    Application.Exit();
}

Shame on me: - (

+5
source share
5 answers

While you should use ||instead |, they will give the same result in this situation. Although responding to other answers, changing |to ||will not solve your problem.

, , , . student, student, 4 .

, 3 , , .

, , , :

if (userName.Length > 4 && userName != "student")
{
    Application.Exit();
}
+15

OR (||) OR (|)

, . , , :

  • userName , .

  • userName , a > 4 ( ).

:

if(username.Length > 4 && userName != "student")
{
    Application.Exit();
}

, , , , .

+6
 if (userName.Length > 4 || userName.ToLower() != "student")
 {
     Application.Exit();
 }

.

0

, , , char. , , if (userName != "student") , , .

0

, if, . .. if

  • .
  • "student"

username = "ABC" "", . , userName = "student";

Here you should use the AND operator than just the OR operator.

if (userName.Length > 4 & userName != "student")
  {
   Application.Exit();                
  }

You can also achieve the same result using the && operator. and the operator is the same as the && operator. when X = false and Y is true; Y will not be evaluated at all. Since X is already false. this method also causes a short circuit.

0
source

All Articles