← Back

Activity 2.1 Logic Crash Course


Introduction

Computers aren’t limited to performing arithmetic calculations. We are able to program computers to make decisions using logical expressions. When a computer can make decisions, it’s able to determine different outcomes in a program. Here are a few example scenarios:

These examples all use instances of decision making. Think of it like a computer being able to answer yes or no, correct or incorrect, or more specifically, True or False. Programming languages have a data type called a boolean that represents the values true and false. The study of computing logical expressions is known as boolean logic. Logical expressions in programming result in either a True or False answer. Let’s revisit the examples above:

Just as math has arithmetic operators (+ - * /), C# has various categories of boolean operators: equality operators, comparison operators, and logical operators. We won’t cover every logical operator in this lesson, but there is more to come.

Here is a brief rundown of the various operators: equality ==, inequality !=, greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), and not (!). Logic expressions can be constructed using different data types combined with these operators. The result will be either true or false. Here’s an example:

1000 == 1000

Is 1000 equal to 1000? It is, so the result is the value true. You can see a number of different examples in the tables below. These are known as truth tables.

Equality (==)Result
100 == 100true
100 == 99false
"abc" == "abc"true
"ABC" == "abc"false
true == truetrue
true == falsefalse
Inequality (!=)Result
100 != 100false
100 != 99true
"abc" != "abc"false
"ABC" != "abc" true
true != truefalse
true != falsetrue
Greater than/Less than (>,<)Result
100 > 100false
100 > 99true
0 < 100true
0 < -1false
Greater than or equal/Less than or equal (>=,<=)Result
100 >= 100true
100 <= 99false
Not (!)Result
!truefalse
!falsetrue
!(100 > 100)true

Skills to Practice


Instructions

Setup

  1. Create a new C# console application. Name the project 2.1 Logic.
  2. Click Create.

Code

string name = "Mr. Mortimer";
int age = 30;
double bankAccount = 150;
bool isATeacher = true;
bool isAMillionaire = false;

Console.WriteLine($"Is my name Mr. Mortimer? {name == "Mr. Mortimer"}");
Console.WriteLine($"Is my name Mr. Mertens? {name == "Mr. Mertens"}");
Console.WriteLine($"My name is not Mr. Merriman: {name != "Mr. Merriman"}");

Console.WriteLine($"Am I old enough to drive? {age >= 16}");
Console.WriteLine($"Do I have enough to buy a Dodge Challenger? {bankAccount > 31000}");

Console.WriteLine($"{name} is a teacher: {isATeacher == true}");
Console.WriteLine($"{name} is a millionaire: {isAMillionaire}");
Console.WriteLine($"So, {name} isn't a millionaire? {!isAMillionaire}");

Debug

Is my name Mr. Mortimer? True
Is my name Mr. Mertens? False
My name is not Mr. Merriman: True
Am I old enough to drive? True
Do I have enough to buy a Dodge Challenger? False
Mr. Mortimer is a teacher: True
Mr. Mortimer is a millionaire: False
So, Mr. Mortimer isn't a millionaire? True

Tips, Tricks, and Reflection