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 )
1 while( 1 = 1 )
2 {
3 print "Hello John";
4 }
5
6 // next line of code
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).
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
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?
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."
"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)
~(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.