Sunday, September 14, 2008

The "for loop"

The for is a very versatile and powerful construct.The general format of the for statement

for(initialization;terminating condition;iteration){
// some code block
}

like all other statements if there is only one statement being repeated you can also avoid the curly braces.This structure is the one that java started with, but in jdk1.5 this has been extended further.Before going there I will first briefly describe about this because although the for loop is extended this format still remains to be quite popular.Let us start with each component of the loop statement:

initialization : This will typically contain the control variable(s) which will be acting as a counter to the loop we want.This is executed first by the compiler hence the control variables are also generally initialized at this point to limit their scope to the loop only.
Terminating condition :
This is executed next and contains or rather sets at which point the loop must be exited. The condition on evaluation should return a boolean value to simply state weather the loop should proceed for another round or exit.
Iteration : At this point we will be altering(altering may be incrementing or decrementing or both) the control variable(s).This will occur after the loop has been executed once after the terminating condition has returned a true and there is no force exit in the loop.

Each block except the termination condition may consist of a single or multiple comma(,) separated statements.For example :

for(int i=0,j=13;i==j;i++,j--){
// some code block
}

is a valid for implementation while the following is an invalid statement and results in a compile time error

for(int i=0,j=0;i<3,j<3;i++,j++){
// some code block
}

Although I mentioned during the description of each of the individual components in the 'for loop' the need for each to act w.r.t the loop process is not strictly necessary i.e. the following are all valid examples of 'for' implementation as the components in a for loop are completely independent of each other

for( ; ; ){
// some code block
}

for(int a=0;b<7;a++){
b=a-2;
}

for( ;a<10; ){
// this is a for loop which acts like a while
}

From jdk1.5 though the for loop has been extended to cater to the needs of the arrays and collection objects. For which the syntax has been modified to accept only two instead of three components.The generalized form of the extended for loop for arrays is


for(declaration : expression){
// some code
}


The declaration should be a newly declared block variable that is compatible with the elements of the array that you will be accessing while the expression must evaluate to the array you will be looping through.The following examples should help

class test
{
public static void main(String[] args)
{
String sArray[] = {"hello","world"};
int towDiArray[][] = {{1,2,3}, {4,5,6}, {7,8,9}};
Long LArray[] = {4L, 5L, 6L};
//Legal extended array usages
for(String s : sArray){
System.out.println(s);
}
for(long l1 : getlongArray());
for(long l1 : LArray);
for(int n[] : towDiArray);
for(int n : towDiArray[2]);
}

public static long[] getlongArray(){
long lArray[] = {11L, 12L, 13L};
return lArray;
}
}

The expression can be a array variable or a function that returns an instance of the array and the arrays are not restricted to the predefined but also customized ones can be used.The declaration though cannot be a variable that is already defined but one that is freshly declared i.e.

int i;
int intArray[] = {1,2,3,4,5};
for(i : intArray);

throws and exception like

test.java:6: not a statement
for(i : intArray[2]);
^
test.java:6: ';' expected
for(i : intArray[2]);
^
test.java:7: illegal start of expression
}

Regarding the usage of the extended for loop for collections I will be dealing with it in the next post

Saturday, September 13, 2008

Dragging a html element with javascript


<HTML>
<HEAD>
<TITLE> Drag n Drop </TITLE>

<style>

.dragme{
position:relative;
border:2px solid #FFFFFF;
width:75px;
font-weight: bold;
font-size:16px;
}

</style>

The style .dragme creates a container around the element which we would like to set as a movable entity in our page except for the position attribute the remaining all are optional and have been added to enhace the visual impact

<script language="JavaScript1.2">
var isIE=document.all;
var isNetscape=document.getElementById&&!document.all;

the above two variable are defined to decide which browser we are working on because the way page elements are accessed differs from IE and Netscape style browsers, both the variable above return an object if it is true else return false.
Eg : if you are in a isIE browser the 'isIE' equals an object and isNetscape is false.

var isdrag=false;
var x,y;
var dragObj;

onMouseMove(Event e) is called when the mouse moves. In order to enable dragging we set a isDrag variable which is set as true when mouse is pressed an reset when it is released.

function onMoveMouse(e)
{
if (isdrag)
{
dragObj.style.left = isNetscape ? tx + e.clientX - x : tx + event.clientX - x;
dragObj.style.top = isNetscape ? ty + e.clientY - y : ty + event.clientY - y;
return false;
}
}

function onMouseDown(e)
{
var dragObj = isNetscape ? e.target : event.srcElement;
var topelement = isNetscape ? "HTML" : "BODY";

while (dragObj.tagName != topelement && dragObj.className != "dragme")
{
dragObj = isNetscape ? dragObj.parentNode : dragObj.parentElement;
}

if (dragObj.className=="dragme")
{
isdrag = true;
dragObj = dragObj;
tx = parseInt(dragObj.style.left+0);
ty = parseInt(dragObj.style.top+0);
x = isNetscape ? e.clientX : event.clientX;
y = isNetscape ? e.clientY : event.clientY;
document.onmousemove=onMoveMouse;
return false;
}
}

document.onmousedown=onMouseDown;
document.onmouseup=new Function("isdrag=false");

</script>


</HEAD>

<BODY>
<div id="div1" class="dragme">Drag this </div>
</BODY>
</HTML>

Friday, September 12, 2008

The prologue

This is just something that I have wanted to be doing for quite sometime, you know especially when I am in search of some information or something that I did do sometime back or something really good with my job. I know most of you must have felt the same many times.... and the result of that is 'THIS'. Here I will be trying to get as much as I can (of course only technical) that will help us make our programming that bit better and that bit more beautiful.
I am also planning to request a few of my friends to write here so that I too get to know something I missed ;)