Lesson Five � Computational Thinking: Nested conditionals and loops

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.

A B  B C = ( A   B)
1 1
0

In this example, B can be true or false.

Case 1: B = 1 Case 2: B = 0
C = A   B C = A   B
C = 1   1 C = 1   0
C = 1  0 C = 1  1
C = 0 C = 1

B C
1 0
0 1

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

  1. Write the given expression
  2. Find the first operation that applies
    1. Multiply
    2. Divide
    3. Add
    4. Subtract
  3. Keep on doing this until the expression is a number
  4. Underline the result

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?

Like this:
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.

Statement

  1. ……�
  2. ……�
  3. ……�
  4. ……�

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.

  1. Write the given expression:
    3 + 5 � 7 = ?
  2. Find the first operation that applies:
    The first one is multiply. Does this apply? Yes!
    3 + 35 = ?
  3. Keep on doing this until the expression is a number. Is �3 + 35� a number? No, there is an operation left to do. Now loop back to step 2!
  4. Find the next operation that applies.
    Does multiply apply? No.
    Does divide apply? No.
    Does add apply? Yes.
    = 38
    Now you can move on to step 4.
  5. Underline the result
    38

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

  1. Write x = 3
  2. Add 3 (Why do you add 3?)
  3. Keep on doing this until � what do you think? We cannot always be sure.

There is a better solution for solving this type of problem.

  1. x = 3
  2. For I = 1,2,3�10 do the following: I  3
  3. End

The diagram would look like this:

  1. li = l
  2. Compute 3i
  3. Add 1 to i
  4. If i is less than or equal to 10, then go to Step 2. Else go to Step 5.
  5. End

Here is a sequence for writing a scientific journal.

  1. Write the title
  2. If the title is a new method applied to a well-known system or
    the title is a well-known method applied to a new system then go to Step 3,
    else go to Step 1.
  3. Write the first sentence of the abstract.

Lesson 5 Exercise

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.