Jump to content

breaking a while loop with return


jimfog

Recommended Posts

I am using a loop to iterate through an array and break from it when an IF statement is satisfied...

In the code you will see...when the IF statement is satisfied I use return to exit from the function and from the loop...and after that I use break.

 

I think break though is redundant...you tell me for sure:

    compare_dts:function(col){                 var tfrom=$('#timesfrom').val(),ttill=$('#timestill').val();                 var i=0;                 var s=new Date(col.models[i].attributes.start);                 var e=new Date(col.models[i].attributes.end);                 while (i < col.models.length) {                 i++;                 var st=s.getTime()/1000,en=e.getTime()/1000;                   if((st==tfrom)&&(en==ttill))                   {    $('.pack_check').html('blabla...').css( "color", "red" );                      return false;                        break;//does this have to go away?                                                          }                   else                   {$('.pack_check').html('');                    return}                 }                    },

As the code currently is...there is no problem,I want to be sure anyway though.

 

The purpose(anyway) is that when this IF condition is met the functions returns false.

Link to comment
Share on other sites

Right, that loop is just going to execute the same piece of code over and over, it won't do anything different each time through the loop other than increment i. It doesn't look like there's a reason for the loop. To answer the original question, code after a return statement does not get executed.

Link to comment
Share on other sites

That loop goes through an array.This array might have 3 elements(for example)...each element is an object and 2 of the object's attributes are start and end.

 

The logic is this:

If some of these attributes meet a condition(see the if statement) then the loop must stop.

 

 

There may be a case where the condition is not met,in such a scenario the loop will just end.

 

So why do you say that there is no reason in repeating the instructions.

The loop is there to find something(that is why is there the if condition) in the array.

Link to comment
Share on other sites

Look at what it does:

while (i < col.models.length) {  i++;  var st=s.getTime()/1000,en=e.getTime()/1000;  if((st==tfrom)&&(en==ttill))  {    $('.pack_check').html('blabla...').css( "color", "red" );    return false;  }  else  {    $('.pack_check').html('');    return;  }}
The loop will continue as long as i is less than col.models.length. First, it increments i. Then it gets s.getTime and e.getTime. Then it compares those with tfrom and ttill, and either way it will return and the loop ends after the first run.But even if those return statements were gone, it would still be doing the same thing every time through the loop. The data it works with is s.getTime, e.getTime, tfrom, and ttill. None of those variables change inside the loop, the only variable that changes inside the loop is i, and you don't use i anywhere else in the loop. So you could run that loop 100 times and it's going to do the same thing 100 times: get s.getTime and e.getTime, and compare them with tfrom and ttill. None of those values changes during the loop, it will always use the same values. That's why the loop is not necessary, there's no point to doing the exact same calculations and comparisons with the same values over and over.
Link to comment
Share on other sites

Look at what it does:

while (i < col.models.length) {  i++;  var st=s.getTime()/1000,en=e.getTime()/1000;  if((st==tfrom)&&(en==ttill))  {    $('.pack_check').html('blabla...').css( "color", "red" );    return false;  }  else  {    $('.pack_check').html('');    return;  }}
The loop will continue as long as i is less than col.models.length. First, it increments i. Then it gets s.getTime and e.getTime. Then it compares those with tfrom and ttill, and either way it will return and the loop ends after the first run.But even if those return statements were gone, it would still be doing the same thing every time through the loop. The data it works with is s.getTime, e.getTime, tfrom, and ttill. None of those variables change inside the loop, the only variable that changes inside the loop is i, and you don't use i anywhere else in the loop. So you could run that loop 100 times and it's going to do the same thing 100 times: get s.getTime and e.getTime, and compare them with tfrom and ttill. None of those values changes during the loop, it will always use the same values. That's why the loop is not necessary, there's no point to doing the exact same calculations and comparisons with the same values over and over.

 

You are right in what you say...the way the loop is now it is rather useless...

 

So...take a look at this:

   while (i < col.models.length) {                 var s=new Date(col.models[i].attributes.start);                 var e=new Date(col.models[i].attributes.end);                 var st=s.getTime()/1000,en=e.getTime()/1000;                   if((st==tfrom)&&(en==ttill))                   {                                      $('.pack_check').html('bla blah').css( "color", "red" );                      return false;                                        }                    i++;                 }

Τell me what you think now..as I said before...the logic is that the loop searches through an array and when something is found(if condition) the loop stops.

 

If nothing is found then the loop ends-after it has searched through all the array.

Link to comment
Share on other sites

That looks correct, but I can't tell for sure, what are the chances that the values of st and en are exactly equal to the values of tfrom and ttill? The number has millisecond precision, if it's off by just one millisecond the condition will return false.

 

I assume after the loop you'll be returning true, otherwise the person calling the function is going to get a value that always evaluates to false.

Link to comment
Share on other sites

 

 

I assume after the loop you'll be returning true, otherwise the person calling the function is going to get a value that always evaluates to false.

Υes I fixed that...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...