This blog is going to be an introduction to logic. Now, the term logic can be used for a lot of different things, but in computer science it is specifically talking about things being true or false.
When we claim something, we are making what is known as a statement. A statement is a claim that is either true or false, but not both. This is a little different than statement in computer science which tells the computer to do something. The statement in logic is often called an expression in computer science.
So let’s say we write a program, and we ask for the user’s age. Let’s say that the user’s age is 72. Well, we can make a statement about this age. This user’s age is above 21. This is an example of a logical statement. When we convert this English into something the computer can understand, we would write age > 21. Now, we have an expression. Expressions evaluate to some value. In the context of logic, this expression evaluates to true or false. Age > 21 evaluates to true, but age < 50 evaluates to false.
We’ve discussed the bool data type, which can store either true or false. This is the same concept. A bool variable can either hold true or false, but not both.
The whole concept of statements being evaluated as true or false is the foundation to what is known as boolean logic.
Boolean logic is foundational to everything we are going to be discussing in the next videos. Boolean logic is actually very useful for many subjects including computer engineering and discrete math. In computer science, boolean logic is the foundation to understanding branching. Branching is when we make a program that can do two separate things depending on the truth value of a statement.
Let’s say we are making some app where you have to be at least 13 years of age to use it. Well, with logic, we can write a program that will grant access if the person’s age is 13 or more, and decline access if the person is younger.
We can also create what are known as compound conditionals. This is when we combine multiple statements with what are known as logical operators. One of the most common logical operators is the AND operator, which is symbolized with two ampersands (&&).
So if we were making an app that required you to be a female above the age of 13, we could check the truth value of two statements (age is greater than 12 and sex is female).
It is often helpful to visualize logic. The way we do this is with what is known as a truth table. With a truth table we combine all possibilities of input and see what the output will be.
This is a very simple example, but you can see that the output is always either true or false:
|Age greater than 12||Sex is female||Age greater than 12 and sex is female|
In the upcoming videos, we will be discussing how logic is used inside of programming to create control flow statements. A control flow statement is something that guides the path of our program. We started off writing programs that were very linear. They take an input, calculate something, output the data to console, and then end.
Now, we are going to start writing programs that branch into different paths, have cool loops, and programs that can actually ask the user if they want to restart, or do something again (depending on the situation) rather than just abruptly ending.
This is when programming gets fun! The benefit here is that I am going to try to teach the subject in a way that can be applied to other sciences such as math or engineering.
This is one of my favorite subjects because it actually helps you think about life and teaches you to truly evaluate things. I think this is important for everyone to have some experience with as it will help protect them from being brain washed in real life.
Ready to learn more? Read the next blog on if statements!