Monday, July 30, 2007

Programming Languages Short-Circuit

Have you heard about the thing as Short-Circuit in Programming? Sounds strange since it can be only applied to electrical concepts right? It is also considered as a fault in an electrical device. But the term is used often in programming languages way of evaluating logical expressions or conditional statements. Without an understanding about Short-Circuits in Programming languages, sometimes mysterious things do happen and you simply say that there's a bug in C! Also, without this knowledge about short-circuits in your program's logic could lead to a disaster. Programs crashing in the middle of it's execution. So, let's join hands and conquer the idea behind Short-Circuit in Programming Language

Here's a typical program written in C:

#include<stdio.h>
#include<conio.h>


int main(){
int i;
clrscr();
char str[8] = "monopono"; //assign our string with this text..
i = 0;
while((str[i] != 0x0) && (i < 8)){ //the 0x0 means null and is usually found
//after at the end of a string.
printf("%d %c ", i, str[i]); //simply output the variables values..
i++;
}
getch();
return 0;
}

ok, let's try to analyze this very complicated program. Let's jump directly to the looping statement, our while statement.. So this can read this way, Repeat the following statements WHILE i is less than 8 AND the current character being pointed to by i in our string str is not yet null or 0x0.. Are you with me? If you cannot understand it, read it again..

Considering the conditions given ,((str[i] != 0x0) && (i < 8)), our loop will simply iterate the statements inside if our i is less than 8 AND at the same time we haven't found a null character yet in our string.. Ok the crucial part is when the loop has iterated 8 times already. Can you guess what's the value of the variable i once the loop has finished? Hope you guessed it right, it's 8! If you don't agree with me, try it on..

So that's the danger, the value 8 itself! Take note we alloted 8 characters for our string right? And our C language starts counting from zero, especially in arrays such as our variable declaration of our string str[8] above. So that means the last character of our string should be referenced as str[7] since the first character is str[0], agree?

Ok back to the loop, let's assume that the loop has repeated 8 times already, so the current value for our i is 7, so replacing, the condition now looks like this,

((str[7] != 0x0) && (7 < 8))

still with me? So the condition for our loop is still true, the str[7] contains the character 'o' and IT IS NOT EQUAL to 0x0 right? And our variable i is still 7 which is still LESS THAN 8, so that means, we'll have another loop remaining and the loop will simply execute the statements inside including the last part the i++ and this time our variable i will have a value of 8 since it's been incremented by 1. Now is the moment of truth, variable i having a value of 8 and our next execution goes to our conditional statement,

((str[i] != 0x0) && (i < 8))

so replacing i with 8

((str[8] != 0x0) && (8 < 8))

That's the danger in every programming languages especially the C or C++ since there's no bound-checking being implemented.. So in this case our string str[8] is pointing to the 9th character which we have not defined in our variable declaration, that means we are accessing areas of memory which are not meant to be accessed by our program. In some case, repeatedly running your programs like this might crash your computer depending on the OS you're using. Mine before I was using Win 98 and I had terrible experiences about this error.

So what's the trick? That's where Short-Circuit comes in, you simply swap the two logical expressions like this,

((8 < 8) && (str[8] != 0x0))

Short-Circuit will save you from this undocumented error. Typically, programming languages evaluate logical expressions from left to right and once they find out that a single logical expression has become the traitor among other logical expressions, which means it's the only ONE which has not satisfied a certain condition, immediately the evaluation of other logical expressions will be postponed. In our case, once the (8 < 8) has been evaluated which has now become false, and since we are having the && operator which means every condition must be true for the our loop to stay alive, the program flow will immediately cut the evaluation of the remaining logical expression which is the (str[8] != 0x0) and the whole loop will come to an end finally.

I hope you have added this simple information to your plethora of programming theories. If you have questions on this post, kindly post a comment and I will really
and truly appreciate it.

Till the next stuff, see ya!

0 comments: