What if something is sometimes true and sometimes false?
One of the many uses of logic is in computer programming. Computers are ideal for using logic because they cannot think in subjective terms. For a computer everything is black or white, true or false, for sure, always. A computer cannot decide anything on its own; it needs rules and procedures to follow in order to function. The main structure in all programming languages is the conditional.
A conditional is a sentence which describes a truth table. Think of it as a sentence which summarizes the conditions under which an expression is true or false. Conditionals are a more direct way of connecting an axiom to a conclusion. Take the following for example:
If statement A is true,
then B is true
else B is false
The conditional is a structure the computer can understand and use to evaluate problems. Let’s formulate a conditional statement, starting with a truth table.
Given C = A B and A = 1 make the truth table for B and C.
In this example, B can be true or false.
We can summarize this rule table by saying, if B = 1, then C = 0, else C = 1. We would write the conditional statement for this table as follows:
If statement A is true,
do this: B = 1
then: C = 0
else: C = 1
When a computer evaluates a conditional it uses an order of operations that looks like this:
“Evaluate the expression� means
This is an algorithm that explains how to simplify a given expression. An algorithm is a specific sequence of steps to solve a problem.
Take a closer look at step 3. What does it mean to say until the expression is a number? Maybe until you cannot solve anything else? If you were to write this into computer language how would you do it? Until you cannot multiply divide, subtract, etc. How would you write it as a conditional?
If the expression is a number,
then: do step 4
else: do step 2
If you want to tell a computer with computational thinking to do this, you use a conditional to implement the loop.
This is called a loop because the chain or reasoning loops back. We use the conditional to create the loop. This is also known as a flow control statement. Loops and control statements are the most important flow control statements.
Can you predict how many steps you will have to do?
Maybe it depends on the number of operations, you can count the numbers of + and symbols. But if you cannot see the equation, you cannot predict it. Let’s try applying.
Example: 3 + 5 � 7 Can you predict how many steps we will need? How often will you have to do step 2?
With a chain of reasoning like this, start out at step one. The computer languages shows you where you are in the step process with a red dot.
We can also add 2 more steps to this system. A Start and an End command. Sometimes you can have a very complex operation, and you want to label where you start and where you end.
Some chains of reasoning have representations like this:
So it is necessary to have start and end commands. Another example could look like this.
Exercise: Compute the 10 smallest multiples of 3
There is a better solution for solving this type of problem.
The diagram would look like this:
Here is a sequence for writing a scientific journal.
A. Once upon a time, a beautiful princess fell in love with a young, handsome peasant. Although the peasant was poor, the princess wanted to marry him. When the girl’s father, the King, found out about this, he was unhappy that his daughter wanted to marry a peasant. He did not want to disappoint his daughter, so he organized a public contest for the boy. The contest established that the boy would have to choose one of two papers � one paper would have the word Marriage and one would have the word Death written on it. If he chose the paper with Marriage, he would marry the King’s daughter. But if he chose the slip of paper with Death written on it, he would be put to death.
On the evening prior to the contest, the young man overheard the King secretly talking to one of his servants.
The servant asked the King, “Sir, how can you risk this? What if the boy chooses Marriage?�
“Do not worry,� replied the King. “On both slips shall be written the word Death!�
The peasant was very clever, and quickly came up with a solution to the problem. The next day, in front of a large gathering of townspeople, the contest played out. In the evening the peasant and the princess were happily married, to the astonishment of the King, who was impressed with the young man’s wit. What solution did the peasant come up with?
Hint: You can assume that it is true that there were two slips of paper with Death written on them, and the peasant did choose one of them. What matters is how the boy used logic to prove to the crowd that he took the correct slip.