Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Why are inverted commas (;) (;;) used inside of the parenthesis ?

javascript

Best Answer DeadLine, 10 June 2014 - 06:58 AM

In this example, you don't need the first expression before the ';' because you already made a variable to control the loop. The second ';' expression simply checks if the variable 'i' is smaller than 'len' and if it is smaller it will continue. The third ';' expression doesn't do anything. The reason why the third expression doesn't do anything is because you're manually increasing the variable 'i''s value so you don't need the third expression to be anything at all.

var i = 0;
len = cars.length;
for (; i < len; ) {
    text += cars[i] + "<br>";
    i++;
}

Source: http://www.w3schools...js_loop_for.asp

 

This example is just an infinite loop:

for(;;) {
        alert("Loop");
    }
Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 B13

B13

    CC Regular

  • Member
  • PipPipPip
  • 28 posts

Posted 10 June 2014 - 06:44 AM

Why are inverted commas (;) (;;) used inside of the parenthesis ?  for (;;)  and  for(;inputInvalid;) mean in the code? Why those are used ?
 

 

inputInvalid = true;
for(;;)
{
    if(!inputInvalid)
    {
        break;
    }
    //ask user for input
    invalidInput = checkValidInput();
}

 


OR

 

inputInvalid = true;
for(;inputInvalid;)
{        
    //ask user for input
    invalidInput = checkValidInput();
}

:confused:


#2 DeadLine

DeadLine

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 10 June 2014 - 06:58 AM   Best Answer

In this example, you don't need the first expression before the ';' because you already made a variable to control the loop. The second ';' expression simply checks if the variable 'i' is smaller than 'len' and if it is smaller it will continue. The third ';' expression doesn't do anything. The reason why the third expression doesn't do anything is because you're manually increasing the variable 'i''s value so you don't need the third expression to be anything at all.

var i = 0;
len = cars.length;
for (; i < len; ) {
    text += cars[i] + "<br>";
    i++;
}

Source: http://www.w3schools...js_loop_for.asp

 

This example is just an infinite loop:

for(;;) {
        alert("Loop");
    }

Edited by DeadLine, 10 June 2014 - 07:05 AM.


#3 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 10 June 2014 - 07:47 AM

for(;;) is an infinite loop.

 

think of a for loop int the following way:

 

for( <variable initialization>; <end criteria>; <variable manipulation>)

 

In the first one, there isn't anything in the for loop, thus is runs forever.

 

In the second one, there is only end criteria, which would be a boolean value.

 

That's the explanation, both are horrendous examples of loops.  I wouldn't recommend either of those ways.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 10 June 2014 - 08:09 AM

The syntax of a for loop is:

for ([initialization];[test];[loop update])
{
  //body
}

initialization, test, and loop update are all optional, and are skipped when writing an endless loop.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download