rockyraw
rockyraw

Reputation: 1145

When to use 'Break' in For loop?

I have various pages, each of them contain some number of arrays,

For example:

Page 1 contains only 2 arrays:

$textual_button[1] = "I am a long character";
$textual_button[2] = "I am also a long character";

Page 2 contains 20 arrays:

$textual_button[1] = "I am a long character";
...
...
$textual_button[19] = "I am also a long character";
$textual_button[20] = "I am also a very long character";

Therefore, I want to check a certain condition in the first 15 arrays (Which may exists - like in page 2 , or may not - like at page 1, which contians just 2)

I want to apply some styling classes named small and big ,or no class at all, depending on two conditions:

  1. By default, do not set any class.

  2. If an image exists in the array, check the string length of the first 15 arrays. if all of them are shorter than 11 characters, set a big class. otherwise, set a small class.

  3. No matter if an image exists or no, if at least one array out of the first 15, has 14 or more characters, then set a small class (and overwrite big class if it was set earlier)

This is the code, I'm not sure when and how to use break:

$class = "nothing"; //default class

if (in_array(true, $image)) { // If there's any image in array :

    $class = "small"; // by default, apply small class 

        for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
            if (strlen((string)$textual_button[$x]) <= 11) {  // if all textual string are  less or equal to 11 characters , make them big
            $class = "big";  
            };
        break; // Is this necessary?
        } 
}

 // In general, no matter if there's an image in array:

for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
    if (strlen((string)$textual_button[$x]) >= 14) {  // if even one textual string is 14 or more, make all of them small.
      $class = "small";
    };
    break; // When is it necessary?
}

Upvotes: 1

Views: 79

Answers (4)

Poiz
Poiz

Reputation: 7617

Could You have meant something like this?

<?php

    $class      = "nothing"; 
    if (in_array(true, $image)) {    
        $class  = "small"; 
        for($x = 1;$x <= 15;$x++){  
            if (strlen( (string)$textual_button[$x] ) <= 11) { 
                $class = "big";
                // BREAK OUT OF THE LOOP INSIDE THE IF CLAUSE SINCE YOU HAVE REACHED YOUR GOAL
                // WHICH IS : THE LENGTH OF $textual_button[$x] IS LESS THAN OR EQUAL TO 11  
                break;
            }else{
                // NO NEED FOR AN EXTRA LOOP SINCE IT IS ESSENTIALLY THE SAME LOOP
                // JUST DO WHATEVER YOU WISH WITHIN THE ELSE CLAUSE...
                // THAT IS IF: THE LENGTH OF $textual_button[$x] IS GREATER THAN OR EQUAL TO 14  
                // AGAIN BREAK OUT OF THE LOOP INSIDE THE ELSE CLAUSE SINCE YOU HAVE REACHED YOUR GOAL
                $class = "small";   
                break;             
            }             
        }
    }

Or something like this?

    $class      = "nothing";
    if (in_array(true, $image)) {
        $class  = "small";
        for($x = 1;$x <= 15;$x++){
            if (strlen( (string)$textual_button[$x] ) <= 11) {
                $class = "big";
                // BREAK OUT OF THE LOOP INSIDE THE IF CLAUSE SINCE YOU HAVE REACHED YOUR GOAL
                // WHICH IS : THE LENGTH OF $textual_button[$x] IS LESS THAN OR EQUAL TO 11
                break;
            }
        }
    }

    for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
        if (strlen( (string)$textual_button[$x]) >= 14 ) {
            // AGAIN BREAK OUT OF THE LOOP INSIDE THE ELSE CLAUSE SINCE YOU HAVE REACHED YOUR GOAL
            // THAT IS IF: THE LENGTH OF $textual_button[$x] IS GREATER THAN OR EQUAL TO 14
            $class = "small";
            break;
        };
    }

Upvotes: 0

jeroen
jeroen

Reputation: 91762

break breaks out of the loop so in both cases, your loop will only run once, or the first iteration, and stop then.

So no, you should not use break here as you have already limited the number of iterations to 15.

break could be useful (there are other methods...) if you were looping over all elements in the array, keep a counter and then check if the counter is 15 to break out of the loop.

Note that you probably want a foreach loop anyway instead of a for loop as a for loop with 15 iterations will lead to php warnings when there are less than 15 elements in your array.

What I would personally do in your case, is slice the array to a maximum of 15 elements (less when there are less...) and to a foreach on that.

On last thing: You are overwriting your $class variable in the loop so it is probably not going to do what you expect it to. You should probably set a boolean to false if any of the elements does not meet your condition and then set the class according to that boolean after the loop.

In general, if any needs to match, you could use break but if all need to match, you always need to finish the loop.

Upvotes: 1

Gynteniuxas
Gynteniuxas

Reputation: 7103

In both of your cases, only once for loop will occur because you're breaking it without checking anything. If you want to check whether all array elements meet criteria, you should put break inside of if conditional:

for ($x = 1; $x <= 15; $x++) {
    if (strlen((string)$textual_button[$x]) <= 11) {
        $class = "big";
        break;  
    };
 }

In this case, if any element in array is shorter than 11 symbols, $class will become big and for loop will stop executing. This will be true for second for as well if you change the code similarly to first example.

Upvotes: 0

Bartosz Gajda
Bartosz Gajda

Reputation: 1167

Maybe try this

$class = "nothing"; //default class

if (in_array(true, $image)) { // If there's any image in array :

    $class = "small"; // by default, apply small class 

        for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
            if (strlen((string)$textual_button[$x]) <= 11) {  // if all textual string are  less or equal to 11 characters , make them big
            $class = "big";
            };
            else {
            $class = "small";
            break;
            }
        } 
}

 // In general, no matter if there's an image in array:

for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
    if (strlen((string)$textual_button[$x]) >= 14) {  // if even one textual string is 14 or more, make all of them small.
      $class = "small";
      break;
    };
}

Upvotes: 1

Related Questions