Ritchie met me as I was just about to enter the resistance base.
“Hi Teo, where have you been?” he asked.
“I've just had a little walk around,” I said. “Can you tell me one thing,
please? Why didn't you mention anything about ‘if’ statements?”
“How do you know about ‘if’ statements?” Ritchie asked.
“It’s not important how I know,” I said. “I know about ‘if’ and I know about
‘else’, so now I can give my program a choice!”
“Ok... first things first. Did you find the key on level 5 of the Grand
Tower?”
“Yes, I did. Key 42.” I smiled proudly. This was the first task I’d solved
while working for Ritchie.
“Well done! Good job! You’ll be getting a virus for this!” Ritchie smiled
back and handed me a white memory card. It probably had the virus on it.
“If you already know ifs,” he said, “take this and try to learn bools. I
have a Noname program running on one of the computers in B wing.”
The room had a table, a chair, and a screen with a keyboard attached. The
text on the screen read, “Enter 'Hi' to start a conversation.”
“Hi,” I entered.
“Hi, who are you?” Noname replied.
“I'm Teo. Don't you remember me?”
“No, I don't. I am Noname build 15542. You are not in my memory. But I have
a task from the user Ritchie. I must tell you about bool type.”
I had no idea what "build 15542" meant, but I was keen to find out more
about bool type.
“Okay, go ahead,” I said. “Tell me about bool type!”
“The C# programming language defines different standard types,” the machine
began. “Do you know any of them?”
“I know
int
and
string
,” I said.
“There are more. One of them is ‘
bool
.’ The ‘
bool
’ type can store only two values:
true
or
false
.
To create a variable of type bool, do the same thing you did with
int
or
string
. First write the type name, ‘
bool
,’ then the variable name and then, probably, the initial value of the
variable.
Like this:
bool firstVariable = true;
bool secondVariable = false;
bool thirdVariable;
“I should mention here that bool is short for Boolean. You know Boolean,
don’t you? Boolean logic, Boolean algebra, etc. You must have heard
something about it.” Noname added.
“Yes, I have heard something. It's quite easy to understand, but where and
how do I use it in my code?” I asked.
“Well, you could use bool variables as a condition in an ‘
if
’ expression like this, for example.”
bool theDayIsNice = true;
if (theDayIsNice)
{
Console.WriteLine("Such a nice day!");
}
// Outputs: Such a nice day!
“Hmm... so it means that ‘
if
’
statements take an expression of bool type as a condition
? I can create any
bool
variable
x
, and then do
‘if
(x) {...}
’?”
“Yes, Teo, exactly.
If the
bool
variable has the value ‘
true
,’ your code follows the ‘
if
’ branch. If the
bool
variable has the value ‘
false
,’ your code follows the ‘
else
’ branch.
Now, try and get your slow, delicate, human brain around this example:
bool trueVariable = true;
if (trueVariable)
{
Console.WriteLine("Inside if");
}
else
{
Console.WriteLine("Inside else");
}
// Outputs: Inside if
bool falseVariable = false;
if (falseVariable)
{
Console.WriteLine("Inside if");
}
else
{
Console.WriteLine("Inside else");
}
// Outputs: Inside else
“Try it yourself!
“That's not all you can do with the Boolean type,” Noname added. “You can
also perform operations on bool variables. Here’s the list of actions:
“Why would I need them, Noname?” I asked.
“Let's say you have two
bool
variables:
isMachine
and
isDangerous
. You want to write a program that outputs ‘shoot’ only if your enemy is a
machine and is dangerous. In this case, you can use an
&&
(AND) operator:
bool isMachine = true;
bool isDangerous = true;
if (isMachine && isDangerous)
{
Console.WriteLine("Shoot!");
}
else
{
Console.WriteLine("Don't shoot");
}
// Outputs: Shoot!
bool isMachine = true;
bool isDangerous = false;
if (isMachine && isDangerous)
{
Console.WriteLine("Shoot!");
}
else
{
Console.WriteLine("Don't shoot");
}
// Outputs: Don't shoot
“Do you understand how this works?” Noname asked. “Your
if
code branch will only execute if isMachine
AND
isDangerous are
true
. In all other cases, the
else
branch will execute. We can say:
if both values around the AND operator are true, then the result is true.
This table will help you understand the
&&
(AND) operator:
Value 1 | Operation | Value 2 | Result |
---|---|---|---|
true | && | true | true |
true | && | false | false |
false | && | true | false |
false | && | false | false |
“Nice table, Noname. I like it!”
“To be honest, I don't care whether you like it or not. The important thing
is do you understand it?”
“Actually, there is just one thing I don't understand. If I take 2 bools and
perform an AND operation on them, what do I get back? I mean, what type does
that value have?
“Ah! Good question, Teo. When you perform any of the operations on bools
that I showed you earlier—like AND, OR, XOR, or NOT—the resulting value is a
bool type. Look at this:
bool haveEggs = true;
bool haveSugar = true;
bool haveLemons = true;
bool canCookCake = haveEggs && haveSugar; /* Put the result of && operation to the canCookCake variable */
bool canCookLemonade = haveLemons && haveSugar; /* Put the result of && operation to the canCookLemonade variable */
if (canCookCake && canCookLemonade)
{
Console.WriteLine("Tasty lunch!");
}
else
{
Console.WriteLine("Not tasty lunch :(");
}
// Outputs: Tasty lunch!
“Try to solve some problems so that I can be sure you understand the &&
operator.
“Well done, Teo! Your human brain is not quite as slow as I expected,”
Noname said.
“I guess I should say thank you. Do you know what "being polite" means,
Noname?”
“No, Teo, the politeness module wasn’t included in my current build because
it makes no sense. But something that makes perfect sense is the OR
operator. And I'm going to teach it to you now. Take a look at this
||
(OR) operator table.”
Value 1 | Operation | Value 2 | Result |
---|---|---|---|
true | || | true | true |
true | || | false | true |
false | || | true | true |
false | || | false | false |
I looked at the OR operator table. It was almost the exact opposite of the
AND operator table: in all combinations, except false false, it returns
true; and it only returns false in false false. So, to help you remember the
rule for the OR operator, you could say:
if at least one of the values is true, the result is true.
Fortunately, this is easy to understand. It’s the same in real life—when we
say ‘Alex
AND
Kristy, come here,’ we’re only satisfied if they
both
show up together. But if we say ‘Alex
OR
Kristy, come here,’ we’re happy if at least
one of them
appears. The same goes for programming... this picture might help.”
“Try to solve these tasks with the
||
operator,” Noname said.
“Not bad! But there’s one more operator,” Noname said.
“Really?” I replied. “I guessed two:
!
(NOT) and
^
(XOR).”
“Yes, you’re quite right, but we’re not going to talk about XOR. It’s not so
widely used and probably won’t help you much in missions for Ritchie. Let's
focus on the NOT operator; you can always read about
XOR
in other sources outside our resistance base.
“It’s very easy to use !(NOT).
NOT reverses the value of bool variables: !true equals false; !false
equals true.
That's all there is to it!”
“Take a look at the table for the
!
(NOT) operator.”
Operation | Value | Result |
---|---|---|
! | true | false |
! | false | true |
Here’s how you use it in the code:
bool isHuman = true;
if (!isHuman)
{
Console.WriteLine("Shoot!");
}
else
{
Console.WriteLine("Don't shoot");
}
// Outputs: Don't shoot
“The code above executes the if branch
if
!isHuman is
true
. Which we can read as ‘NOT isHuman is
true
.’ Which is the same as ‘isHuman is false.’ In our case, the value of
isHuman is
true
, so we don't shoot because we don't want to shoot people.”
“Noname, this is getting complicated. Could you explain it again, please?” I
asked.
“I could,” said Noname, “but I’m not going to. If you didn't understand me
the first time (don’t worry, this is absolutely normal for you humans), read
my explanations once again.
“Solve these tasks:
“One last thing: you can combine different conditions in if expressions . To do this, take each of your conditions in the ( parenthesis ) . You can leave out the parentheses if part of your condition is a variable. Look at these examples.”
bool isMachine = false;
int age = 45;
if (!isMachine && (age > 35)) // Same as ((!isMachine) && (age > 35))
{
Console.WriteLine("Do you want to be a new president?");
}
else
{
Console.WriteLine("Next, please");
}
// Outputs: Do you want to be a new president?
int height = 175;
int age = 45;
if ((height > 180) && (age > 35))
{
Console.WriteLine("Do you want to be a new president?");
}
else
{
Console.WriteLine("You are not tall or old enough");
}
// Outputs: You are not tall or old enough
bool meatLover = true;
int money = 10;
if (!meatLover || (money < 15)) // Same as ((!meatLover) || (money < 15))
{
Console.WriteLine("You better buy vegetables.");
}
else
{
Console.WriteLine("Meat!!!");
}
// Outputs: You better buy vegetables.
“Do you understand?” Noname asked.
“Let me see.
It’s possible to combine different conditions by putting
||
or
&&
between them. Each condition should be in parentheses unless it is a
variable name.
Correct?” I said.
“Yes, human, you are correct. Now solve these tasks:
I was exhausted after talking to Noname. Nothing was particularly hard, but it took a lot of effort to understand all of it. At the same time, I had a gut feeling that something was missing—some crucial component was not yet in place... But which one could it be?