dongyou7292 2017-08-08 05:12
浏览 36
已采纳

我对php中的以下代码感到困惑

The code is as follow-

<?php
$i = 5;
while (--$i > 0 || ++$i)
{   
    print $i;
}
?>

The correct answer is 4321111........ But how could it start from 4, it should just strat from 5. In my opinion, the answer should be 55555555.......

  • 写回答

2条回答 默认 最新

  • dongshanfan1941 2017-08-08 05:17
    关注

    it starts with 4 because this statement is evaluated first (--$i > 0 )

    which is pre decrement, that means it decrements first then evaluates the while loop

    so here it is checking whether i is greater than 0 or else increment i by 1

    so it prints 4321, when i reaches 0 the --$i > 0 statement is not evaluated as i becomes 0 so the or part is evaluated which is +1

    so the result becomes 4321111111.................

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据