Use of loop statements
Student Wang needs to travel back and forth between Beijing and Dalian repeatedly, which is a typical loop structure. Assume that Mr. Wang Si’s investment in this project requires 100 round trips to Dalian, and Mr. Wang will count each round trip. Should we write the same code a hundred times? Obviously it is impossible for programmers with extremely high IQs to handle this.
We abstracted this kind of human thinking. We define a loop structure
<?php //定义需要往返的次数,老外喜欢从0开始计数,我们也从0开始计 $count = 0; //while后面接布尔值判断,为真执行,为假停止 //$count 小于100的时候执行,也就是$count为0至99的时候执行 //如果$count不小于100了,循环停止执行后续的代码 //循环开始处 while($count < 100){ echo '我是王思总,我是第' . $count .'次出差<br />'; //每次执行让$count+1,这样的话,就不会产生$count永远小于100的情况了 $count++; //循环结束 } echo '后续代码'; ?>
We can add a special code logic diagram for the while loop: