Interesting 'for' loop

Hi,

Is the following 'for' loop valid in javascript :-

for(var n=0; length; n++)

Cheers
Nura

Comments

  • Nura,

    I'd have to say that the for loop "for(var n=0; length; n++)"
    probably is NOT valid. Two reasons for this are:
    1. varibles (like "n") should be declared before use.
    For example:
    var n;
    for (n = 0; n < length; n++)


    2. Where did "length" come from? What is it compared to?
    If you're comparing the length of a string or something, try this:
    var n;
    var mystring = new String("Hello");

    for (n = 0; n < mystring.length; n++)

    Jesse

    : Hi,
    :
    : Is the following 'for' loop valid in javascript :-
    :
    : for(var n=0; length; n++)
    :
    : Cheers
    : Nura
    :

  • Hi,

    Actually, it is legal to declare local variables in javascript this way:

    for(var n=0; n<=10; n++)
    {
    document.write(" " , n );
    }

    Why would you declare a counter variable with a global scope?

    Lillu

    : Nura,
    :
    : I'd have to say that the for loop "for(var n=0; length; n++)"
    : probably is NOT valid. Two reasons for this are:
    : 1. varibles (like "n") should be declared before use.
    : For example:
    : var n;
    : for (n = 0; n < length; n++)
    :
    :
    : 2. Where did "length" come from? What is it compared to?
    : If you're comparing the length of a string or something, try this:
    : var n;
    : var mystring = new String("Hello");
    :
    : for (n = 0; n < mystring.length; n++)
    :
    : Jesse <slicer69@hotmail.com>
    :
    : : Hi,
    : :
    : : Is the following 'for' loop valid in javascript :-
    : :
    : : for(var n=0; length; n++)
    : :
    : : Cheers
    : : Nura
    : :
    :
    :

  • Hi,

    The statement

    "for(var n=0; length; n++)"

    is all the more valid by itself.

    Javascript's "for- statement" expects a valid boolean condition as the second argument. Remember any variable not declared or "undefined" or numeric value "zero" is treated as "false", hence the above for-statement block would never get executed. On the contrary, if the variable "length", is declared and has a value other than zero or null, the for-block would get into an infinite loop.

    :)heers,
    Vanith



    : Hi,
    :
    : Actually, it is legal to declare local variables in javascript this way:
    :
    : for(var n=0; n<=10; n++)
    : {
    : document.write(" " , n );
    : }
    :
    : Why would you declare a counter variable with a global scope?
    :
    : Lillu
    :
    : : Nura,
    : :
    : : I'd have to say that the for loop "for(var n=0; length; n++)"
    : : probably is NOT valid. Two reasons for this are:
    : : 1. varibles (like "n") should be declared before use.
    : : For example:
    : : var n;
    : : for (n = 0; n < length; n++)
    : :
    : :
    : : 2. Where did "length" come from? What is it compared to?
    : : If you're comparing the length of a string or something, try this:
    : : var n;
    : : var mystring = new String("Hello");
    : :
    : : for (n = 0; n < mystring.length; n++)
    : :
    : : Jesse <slicer69@hotmail.com>
    : :
    : : : Hi,
    : : :
    : : : Is the following 'for' loop valid in javascript :-
    : : :
    : : : for(var n=0; length; n++)
    : : :
    : : : Cheers
    : : : Nura
    : : :
    : :
    : :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories