duanbi3786 2013-02-02 14:53
浏览 28

两个while循环在一个页面中

can I have two while loops with same condition but different if statements in one page?

For instance:

$qtytoAdd = $rec_qty[$mat_key];

while($qtytoAdd > 0){
    if(($remBalance - $qtytoAdd) >= 0) {
    //do something
    $qtytoAdd = 0
    }else{
    //do something
    $qtytoAdd = $qtytoAdd - $remBalance
    }
}

And something like this:

while($qtytoAdd > 0){
    if(($reqRemBal- $qtytoAdd) >= 0) {
    //do something
    $qtytoAdd = 0
    }else{
    //do something
    $qtytoAdd = $qtytoAdd - $reqRemBal
    }
}

The $qtytoAdd variable is actually defined at beginning before the loops. What I'm trying to do is run both while loops at the same time with both $qtytoAdd of each while loops having the same values at first.

  • 写回答

2条回答 默认 最新

  • douju6850 2013-02-02 14:56
    关注

    Sure but if you have them one after each then to break the first one, you would either need to force a break or make $qtytoAdd != 0

    If it is the latter than it would cause the second loop to not be executed.

    If you explain what you are trying to do, there should be a more elegant way to do it.


    I think what you are actually looking for is just this:

    $qtytoAdd = $rec_qty[$mat_key];
    
    while($qtytoAdd > 0){
        if(($remBalance - $qtytoAdd) >= 0) {
        //do something
        $qtytoAdd = 0
        }else{
        //do something
        $qtytoAdd = $qtytoAdd - $remBalance
        }
        //second block
        if(($reqRemBal- $qtytoAdd) >= 0) {
        //do something
        $qtytoAdd = 0
        }else{
        //do something
        $qtytoAdd = $qtytoAdd - $reqRemBal
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私