Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Jan 2016
    Posts
    2

    confused about the !(is not) operator

    First of all, sorry for my poor english - i'm from europe...
    My question refers to the !(is not) operator.
    During the C#-101 lesson about arrays Nelson is using it in the following context:
    Code:
            bool hasEnteredMoney = false;
    
            while(!hasEnteredMoney)
            {
               .......
            }
    He explains that the while(!hasEnteredMoney) loop is the equivalent to (has entered Money == false).
    Now, i'm a little bit confused because the statement (!hasEnteredMoney) suggests that (hasEnteredMoney = false) is not(!) true - which is false(double negation)...
    Whereas (hasEnteredMoney == false) is checking if the values of the two operands are equal - which is true because hasEnteredMoney is set to false
    I hope i made myself clear enough and somebody out there can solve my dilemma...
    Thank you very much in advance for answering my rookie-question!

  2. #2
    Join Date
    May 2011
    Posts
    95
    I think I see where you are confused. Consider this snippet

    Code:
    if (true)
    {
        Console.WriteLine("Hello");
    }
    if (false)
    {
        Console.WriteLine("Will never print");
    }
    the expression inside of the '()' part of an if statement will evaluate to either true or false. If it evaluates to true, we enter the if statement's scope. If it evaluates to false, we skip the scope (the lines of code in between '{}')

    So, consider this:

    Code:
    bool hasMoney = true;
    
    // hasMoney is true, so this statement evaluates to true and we execute the line of code in this if statement's scope.
    if (hasMoney) 
    {
        Console.WriteLine("I have money");
    }
    
    hasMoney = false;
    
    // Now hasMoney is false, which means this statement evaluates to false, and we do not enter this scope.
    if (hasMoney)
    {
        // This line will not be executed
        Console.WriteLine("I have money");
    }
    
    // What if I wanted to print a specific line of text when I don't have money?
    
    // '!' reverses a boolean expression. We want to print that we don't have money when the statement
    // "I do not have money" is true. So, take haveMoney and reverse it with '!' and now the fact that I do not have
    // money is true.
    
    // This line evaluates to true, and so we enter this scope and execute the code inside.
    if (!hasMoney)
    {
        // This line will be executed
        Console.WriteLine("I do not have money");
    }
    
    // I have money is now a false statement, so this expression in the '()' will evaluate to false.
    if (hasMoney)
    {
        // This line will be executed
        Console.WriteLine("I do not have money");
    }
    The same applies for a while statement.

    I hope that cleared things up for you!

  3. #3
    Join Date
    Jan 2016
    Posts
    2

    Thumbs up

    Quote Originally Posted by frostbytes89 View Post
    I think I see where you are confused. Consider this snippet

    Code:
    if (true)
    {
        Console.WriteLine("Hello");
    }
    if (false)
    {
        Console.WriteLine("Will never print");
    }
    the expression inside of the '()' part of an if statement will evaluate to either true or false. If it evaluates to true, we enter the if statement's scope. If it evaluates to false, we skip the scope (the lines of code in between '{}')

    So, consider this:

    Code:
    bool hasMoney = true;
    
    // hasMoney is true, so this statement evaluates to true and we execute the line of code in this if statement's scope.
    if (hasMoney) 
    {
        Console.WriteLine("I have money");
    }
    
    hasMoney = false;
    
    // Now hasMoney is false, which means this statement evaluates to false, and we do not enter this scope.
    if (hasMoney)
    {
        // This line will not be executed
        Console.WriteLine("I have money");
    }
    
    // What if I wanted to print a specific line of text when I don't have money?
    
    // '!' reverses a boolean expression. We want to print that we don't have money when the statement
    // "I do not have money" is true. So, take haveMoney and reverse it with '!' and now the fact that I do not have
    // money is true.
    
    // This line evaluates to true, and so we enter this scope and execute the code inside.
    if (!hasMoney)
    {
        // This line will be executed
        Console.WriteLine("I do not have money");
    }
    
    // I have money is now a false statement, so this expression in the '()' will evaluate to false.
    if (hasMoney)
    {
        // This line will be executed
        Console.WriteLine("I do not have money");
    }
    The same applies for a while statement.

    I hope that cleared things up for you!

    Dear frostbytes89!
    A very late "Thank you!" to you!

  4. #4
    Join Date
    Dec 2016
    Posts
    0

  5. #5
    Join Date
    Apr 2017
    Posts
    0
    I had the same question, so thank you for posting!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •