while loopLOGIN

while loop

In the opening chapter of loops, we talked about the logic and syntax of loops, and passed the above training. You can easily grasp the knowledge points of loops.

while is a Boolean loop. If the value of while (Boolean judgment) is true, the code enclosed in curly brackets will be executed. If it is false, stop and execute the subsequent code.

Now, let’s add a little bit of difficulty to change the color of every other line. Interlaced color change is a performance we often use in web pages.

Requires key points to master. Let’s take a look at the effect:
2015-08-08/55c5b00fcc714

So now we want to write a table with alternating colors from 0 to 99. How should we write it? [requires silent writing]

  1. Define the initial value, output the table label and the column label in the table
<?php
//定义循环的初始值
$i=0;
echo '<table width="800" border="1">';


while($i<100){
        //输出列0-99的列了
    echo '<td>'.$i.'</td>';
        //一定要加哟,不然死循环了
        $i++;
}

echo '</table>';
?>
  1. Add the logic of row generation
<?php
$i=0;
echo '<table width="800" border="1">';

while($i<100){
    //0 - 9 为一行
        //10 -19 为一行
        //因此,每一行都能够被10求默,如为为10的时候,应该显示行开始的标签
    if($i%10 == 0){
                //为了隔行变色,每20,40,60每行的颜色不同的,因此我们又可以再进行一次取余运算
        if($i%20==0){
            echo '<tr>';
        }else{
            echo '<tr bgcolor="pink">';
        }
    }

    echo '<td>'.$i.'</td>';

    $i++;
        //同理,每一行结束是不是应该有一个</tr>结束标签呢?
    if($i%10==0){
        echo '</tr>';
    }
}
echo '</table>';
?>

The above code requires more practice and constant experimentation before you can imagine it. The above part needs to be written silently to exercise logic.

Is it possible to nest another loop (double-layer loop) inside the loop to achieve the table effect of changing colors on alternate rows? [Required to write silently]

The basic implementation logic is as follows

  1. First output the table label
  2. Through the first layer of loop and then output the row label
  3. In the first Insert a layer of loop output label
<?php
echo '<table width="800" border="1">';
$i=0;


while($i<10){
    echo '<tr>';

    $j=0;
    while($j<10){

        echo '<td>'.$j.'</td>';
        $j++;

    }
    echo '</tr>';

    $i++;

}
echo '</table>';
?>

into the above code. Do you add the judgment of interlaced color change and the normal numerical display of 0-99?


You can learn some basic algorithms after learning the loop.
This will help you have a higher success rate in interviews.

Note: Do not write an infinite loop (a loop without exit conditions)

whie(1){
    echo 1111.'<br />';
}
Next Section
<?php $i=0; echo '<table width="800" border="1">'; while($i<100){ //0 - 9 为一行 //10 -19 为一行 //因此,每一行都能够被10求默,如为为10的时候,应该显示行开始的标签 if($i%10 == 0){ //为了隔行变色,每20,40,60每行的颜色是不同的,因此我们又可以再进行一次取余运算 if($i%20==0){ echo '<tr>'; }else{ echo '<tr bgcolor="pink">'; } } echo '<td>'.$i.'</td>'; $i++; //同理,每一行结束是不是应该有一个</tr>结束标签呢? if($i%10==0){ echo '</tr>'; } } echo '</table>'; ?>
submitReset Code
ChapterCourseware