Subscribe to Penguin-man the computer guy

Showing posts with label philosophy. Show all posts
Showing posts with label philosophy. Show all posts

Wednesday, May 15, 2013

Gaining a basic understanding of the Logic of If and If-else statements

Next to while statements, if statements are one of the most important statements that can be made. As we noted in the last post, while statements and if statements control the flow of logic in a statement. This of course is equally important in language as it is in code. This is all well and good, but what do we mean by controlling the flow of logic in a sentence? Well, what I mean is that good logic flows in specific ways, and instead of a linear flow we can control that flow with while loops and if statements. Consider the following linear logic flow.

A ---> B ---> C ---> D

Here, premises A, B, and C all lead to D... but what if I wanted to lead to either D or E? To accomplish that, I would need to use a control structure that would make the logic flow to D if some condition were met, and to E if some other condition were met. Ideally, this might be an If-Else statement. Before showing pseudo code, lets examine our previous example altered to let us


A ---> B ---> C if( condition = true ) ---> D
\           
                                   \-------->   else ---> E

Note how if the condition is not true (~ condition ), then the logic automatically flows to the else. This makes the else the default position of the logic flow. In pseudo code, this is how it looks:

1     if( condition )
2     {
3        statements;
4     }
5     
6     else
7     {
8        statements;
9     }

The other case is a series of if statements.

1       if( condition )
2       {
3          statement_1;
4       }
5     
6       if( condition )
7       {
8          statement_2;
9       }
10     
11     if( condition )
12     {
13      statement_3;
14     }

Note that in the case of  a series of statements and no else at the end, there is no default behavior. Default behavior in a program is the result of else statements, and so if a program has a default behavior, then that should be written as an else. This makes the following syntax the best format for writing a program:


1     if( condition )
2     {
3        optional behavior; 
4     }
5     
6     else
7     {
8        default behavior;
9     }

Conclusion

If and if-else statements are a relatively simple logical structure. Implementing them in code is not very hard to do, but there simplicity belies the power that they bring to life and code.






Monday, May 13, 2013

Understanding the Logic of Looping Structures

One of the subjects that gets taught very early in computer programming is the idea of a Logical loop. Unfortunately, it seems that some people find it difficult to see that looping structures are a very important part of understanding the universe, and in this post, we will try to examine the how and why of looping structures in order to see if we can understand them not just as a method of controlling logic flow in a coded program, but as a way of understanding language, behaviors, neurological processes, etc. I hate to be grandiose and pretend that this is an all important concept, but this in fact is a quite profound idea.

Our first question will be a simple one: What is a logic loop? I think that showing this by some kind of pseudo code is the easiest way to begin. Don't worry if you find this a difficult idea at first... we will attempt to break it down.

while (some_condition = true )
{
     // some action is done.
}

This code snippet (which uses C/C++ syntax) shows that the condition in the parenthesis governs if, and how many times, the statements inside the brackets are done. Before executing the code inside the brackets the very first time, a while loop checks that the condition is true. If the condition is true, then the code between the brackets is executed. When the last statement of code between the brackets is executed, the condition is checked again. If the statement is true, the code continues to be executed starting once again with the first line of code between the brackets. If the condition is not true, the loop is exited. To continue this thought, lets look at two loops where we specify the conditions of the code. Once again, this snippet of pseudo code will uses C/C++ like syntax, however it will not compile. We are using pseudo code for logical clarity, not  real code. We are also not going to specify a data type for our variables, and will be using line numbers so we can follow the ideas in more clarity.

1       x = 5;
2
3       while( x > 0 )
4       {
5           print "Hello John";
6           x = x -1;
7       }

8   
9       // next line of code

Here in this example we first create a variable, 'x', in line 1, and then set it equal to five. We then use the  variable 'x' as the condition of the loop. Remember the syntax for the while statement:

1      while (some_condition = true )

In our example above, the third line is while( x > 0 ). We see that the condition ( x > 0 ) must equal true. If x <= 0, the condition is not true. But, because x = 5 and 5 > 0, then the condition evaluates to true. The loop executes until the condition is false. To see how to control amount of times the loop executes, consider how the below code will work:

1       while( 1 = 1 )
2       {
3           print "Hello John";
4       }

5   
6       // next line of code

I think the above is obvious... 1 will always equal one no matter what so this is an INFINITE loop... it will loop as a long as the program is running. Remember that an infinite loop could also take this form:

1       x = 5;
2
3       while( x > 0 )
4       {
5           print "Hello John";
6       }
7       

8       // next line of code

Do you notice what is missing that would control this statement? The expression to increment the variable 'x'. This expression is found on line 6 of the original code and is ( x = x - 1; ). Here we see that for every loop, the variable x becomes one less than its initial value. (Note, the syntax of C++ allows a shorthand version of the statement  x = x - 1 to be written x = x--;. The name C++ comes because the x = x-- is the decremented form and x = x++; is the increment form).

The website http://www.cplusplus.com/ has some great resources in there tutorials describing control structures in depth. I recommend the following page if you want to better understand the specific syntax for loops in C/C++ http://www.cplusplus.com/doc/tutorial/control/. There are three kinds of loops; while loops, the do-while loop, and the for-loop. Similar control structures are if statements, if-else statements, and switch statements. Below is the basic syntax we showed before for a while loop:

1       x = 5;
2
3       while( x > 0 )
4       {
5           print "Hello John";
6           x = x - 1;
7       }

8       // next line of code

This is the basic syntax for a do-while loop:

1       x = 5;
2
3       do
4       {
5           print "Hello John";
6           x = x - 1;  
7       

8        }while( x > 0 );
9       
10      // next line of code

And finally, is the basic syntax for a for loop:

1       for(  x = 5;  x > 0;  x = x - 1)
2       {
3           print "Hello John";
4       }

5       // next line of code

Notice how the for-loop takes all the statements that control the loop and places them in the same line. While this is more complex to learn, it is ultimately less lines of code and more elegant.

What do we use Loops for?

Why do we need loops? What are they used for? Well, when we program, we are emulating reality, and that is where the value of loops is found. Lets take my favorite example that can be expressed either as code or in a sentence. In a sentence "while I pull Sally's hair, Sally will scream." Below the same in code form.


1       while( I pull Sally's hair )
2       {
3           Sally screams;
4       }


This is similar to the statement "while the car has gas and the engine is turned on, the cars engine will run. Here again, there is a condition ("while the car has gas and the engine is turned on") and a result ("the cars engine will run"). Note though that the condition has an and... this makes a more interesting loop structure. Note that && is the computer code way of expression a logical and.


1       while( ( car has gas ) && (the engine is turned on) )
2       {
3           The engine will keep running;
4       }



Here we see that we have two statements separated by an and. Remember the truth table for an and statement of two variables which we will call p and q in respect to convention:

       p   &   q    Truth value

       T         T            T
       T         F            F
       F         T            F
       F         F            F

This table shows us why a logical and is so special. Only in one case, both premises are true, is the statement true. Thus, when a while statement contains an a logical AND, the statement is quite narrowly defined. Less narrowly defined is the or statement:


       p   OR   q    Truth value

       T           T            T
       T           F            T
       F           T            T
       F           F            F

In a logical or, the statement is true unless both premises are false. That means the difference between an or statement verses an and statement is huge.

Conclusion

In conclusion, we need to look at the ways in which we may encounter loops. In reality we see loops all the time. Consider the following:

"While it is cold, I wear a jacket."

In the above, identify that the action, "I wear a jacket", will be terminated when the condition, "while it is cold", is negated. In logic, a ~ is the symbol used to show negation. So,

 ~(action) --> ~(condition)  which could also be written   NOT(action) --> NOT(condition)               

This means that we can expect that if I am not wearing a jacket, then the weather is NOT cold. This is a simple, but valuable insight. Also note that the use of a while here is appropriate, as I repeatedly don my jacket as long as the weather is cold.

Ultimately, anytime we see a process frequently occurs in reality, then we can expect to see it equally frequently in code. In fact, when coding, anytime a task needs repetition until some condition is either met, or not met, the logic of that task is a loop. This makes understanding loops of primary importance to both programmers and philosophers.