0

I am trying to familarise myself with for loop as I only understand the basics. I am trying to simplify the code below

                    $round1 = $max / 2;
                    $round2 = $round1 + ($max / 2 / 2);
                    $round3 = $round2 + ($max / 2 / 2 / 2);
                    $round4 = $round3 + ($max / 2 / 2 / 2 / 2);
                    $round5 ...

With this:-

                    $round = array($max/2);

                    for ($i=1;$i<$max;$i++) {
                        $round[] = $round[$i -1] + $max/pow(2, $i + 1);
                    }   

And now for the next code:-

                    if($matchno <= $round[0]) {
                        $scores_round1.= '['.$score1.', '.$score2.'],'; 
                    }
                    if($matchno > $round[0] AND $matchno <= $round[1]) {
                        $scores_round2.= '['.$score1.', '.$score2.'],'; 
                    }       
                    if($matchno > $round[1] AND $matchno <= $round[2]) {
                        $scores_round3.= '['.$score1.', '.$score2.'],'; 
                    }
                    if($matchno > $round[2] AND $matchno <= $round[3]) {
                        $scores_round4.= '['.$score1.', '.$score2.'],'; 
                    }

Can the above be used in a for loop to avoid using if() ?

Thanks for help

1
  • $round[] = $max - ($max - $round[$i-1]) / 2; based on $max - $round[] being $max/pow(2, i) Commented Aug 13, 2013 at 12:53

3 Answers 3

1

You can check for round1 and for the rest:

 for ($i=1;$i<$max;$i++) {
         if($matchno>$round[$i] AND $matchno <= $round[$i+1])
            ${'scores_round'.$i}.='['.$score1.', '.$score2.'],'; 
     }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks both, I had to combine both answers to get it working right. Thanks!!
0
for($i=0;$i< count($round); $i++){
    if($match < $round[$i]){
        ${"scores_round".$i}.= '['.$score1.', '.$score2.'],';
        break;
        }
}

Comments

0

By watching the if statements, we notice some things: First of all, there is no *else if* statement. That means that all if statement checks must be executed. Additionally, there is a check whether the $matchno is less than $round[0], without any check for greater than in this if statement (the first one). Another point is that $scores_roundX starts with X=1 and not 0. Obviously, you will have to use one if inside the loop. So, we are going to form loop code making some small tricks:

for($i = -1; $i < count($round)-1 ; ++$i){
    if(($i = -1 OR $matchno > $round[$i]) AND ($matchno <= $round[$i+1])){
        ${"scores_round".$i+2} .= '['.$score1.', '.$score2.'],';
    }
}
  • We will initialize THE $i with -1, to use it for the first statement execution.
  • We will put as for statement the $ to be less than the count of the array minus 1 (because we index using $i+1 in our if statement, inside the loop).
  • We will perform greater than check only if the $i is not -1, and this will happen on the second check (second if in initial code). Here, we also use the partial evaluation feature of the language, and that means that in the OR sub-statement, if the first part comes as true, the second one is not ecaluated.
  • We will make $i+2 at the $scores_round forming, because we start from -1 on our for loop.

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.