一路for循环题

WBOY
Release: 2016-06-13 12:48:44
Original
865 people have browsed it

一道for循环题
  今天看到了一道题,觉得比较有意思

<br />
$n = 20;<br />
for($i = 0; $i < $n; $i--) {<br />
    var_dump('-');<br />
}<br />
Copy after login

说是,上面那段代码修改或是增加一处,循环体内执行二十次,明眼人都看出来,上述代码是死循环的,下面是几个方法
1、
<br />
$n =20;<br />
for ($i = 0; -$i < $n; $i --) {<br />
    var_dump('-');<br />
<br />
}<br />
Copy after login

2、
<br />
$n = 20;<br />
for ($i = 0; $i < $n; $i ++) {<br />
    var_dump('-');<br />
}<br />
Copy after login

3、
<br />
$n =20;<br />
for ($i = 0; $i < $n; $n --) {<br />
    var_dump('-');<br />
<br />
}<br />
Copy after login

不过听说还有好多解法,希望知道的朋友能够列举出来,开开眼界!!!


------解决方案--------------------
<br />
$n =20; for ($i = 0; $n < $i; $n --) {<br />
     var_dump('-');   <br />
}<br />
Copy after login


------解决方案--------------------
$n =20; <br />
for($i = 0; $i*($i+$n) < $n; $i--) {<br />
    var_dump('-');<br />
}
Copy after login

------解决方案--------------------
$n =20; <br />
for($i = 0; $i < $n*($i+$n); $i--) {<br />
    var_dump('-');<br />
}
Copy after login


这些都可以id,会是改或加都可以,那就可以运用很多公式放在条件那里的
------解决方案--------------------
$n =20;
for ($i = 0; -$i < $n; $i --) {
var_dump('-');

}
------解决方案--------------------
<br />
$n = 20; <br />
for($i = 0; ($i*$i) < $n; $i--) <br />
{     <br />
    var_dump('-');<br />
}<br />
Copy after login

<br />
$n = 20; <br />
for($i = 0; abs($i) < $n; $i--) <br />
{     <br />
    var_dump('-');<br />
}<br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template