0

I have a for loop and inside of it I have a if statement something like this:

  $output = "";
  $limit = 550;
  for ($i = 1; $i <= $limit; $i++) {
    if($i < 10){
     $output .= my_function($i*1);
    }elseif($i < 20){
     $output .= my_function($i*2);
    }elseif($i < 30){
     $output .= my_function($i*3);
    }
    //elseif 30 => 550
  }

Problem is I find it very tedious to have to continue this elseif statement down to 550. Is there any way to do this without writing 55 elseif statements.

3
  • $output = my_function($i * ceil(($i+1)/10));? Apart from that: Is $output used in the loop or just outside? Commented Aug 21, 2011 at 14:30
  • Ok and the i in the function parameters should be $i right? Commented Aug 21, 2011 at 14:34
  • ugg yeah im kind of all over the place Commented Aug 21, 2011 at 14:51

7 Answers 7

9

Your thinking is exactly right. Having to repeat that much code is a sure sign that something is off.

In this case, the solution is pretty simple. You just want to use integer division to knock off the ones digit, which you don't care about. You also need to adjust by one since you're checking less than and not less than or equal.

$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
     $temp = (int) ($i / 10) + 1;
     $output = my_function($i*$temp);
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for actually giving an explanation with your answer. We need to see more like this!
0
for ($i = 1;$i <= 550;$i++) {
  $multiplier = (int) ($i / 10) + 1;
  $output = my_function($i * $multiplier); 
}

Comments

0

My first thought is that you could use:

  $output = "";
  $c = 0;
  $limit = 550;
  for ($i = 1; $i <= $limit; $i++) {
    if (($i + 1) % 10 == 0) {
        $c++;
        $output = my_function($i*$c);
    }
  }

1 Comment

That works, but is hard to read and tends to fail. I think recalculating $c in the loop is preferable to blowing up the state space with it.
0

How about

$output = "";   
$limit = 550;   

for ($i = 1; $i <= $limit; $i++) {     
    $num = ($i / 10) + 1      
    $output = my_function($i*$num);
} 

1 Comment

php's default division is floating-point. Therefore, num will be 1.1 in the first iteration, not 1.
0

Try this:

$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++)
{
    $output = my_function($i * (1 + (int)($i / 10)));
}

Comments

0

The solution should be as simple as the following:

$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++)
{
    $num = ceil($i/10);
    $output = my_function($num, $num-1);
}

hmm the original code seems to have changed since I started typing this. The my_function function now only has 1 input when it originally had 2.

Comments

0
for ($i = 1; $i <= $limit; $i++) {
     $output = my_function(i*floor($i/10+1));
}

Point is not really about this question:
You reassign output on every loop iteration, but only last will be vailable after loop? Need you $output[]= or $output.= ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.