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!