Jing Chu
Jing Chu

Reputation: 25

Why my PHP loop does not work? Or do I have to add 'if'?

$money = 100000; // the original money I have
$a = 0; // $a defines how many times I can spend the money

// When the money I have is more than 50,000, I spend 5% of them as the pass-door fee.
while ($money > 50000) {
    $money= $money * 0.95; // The money left after each spending
    $a++; // How many times I can spend the money.
}

echo $a.'<br>'; //$a=14    So far I have passed the door 14 times.
echo $money.'<br>'; //$money=48767.497911553    So far I have 48767.497911553 money left.

My question is why the following codes do not work?

// When the money I have is equal to or less than 50,000, I spend 5,000 as the pass-door fee.
while($money<=50000) {
    $money = $money - 5000; // The money left after each spending
    $a++; // How many times I can spend the money.
}

echo $a.'<br>';
echo $money. '<br>';

Upvotes: 0

Views: 100

Answers (4)

Lelio Faieta
Lelio Faieta

Reputation: 6684

First I check I have still money:

$money = 100000; //the original money I have
$a= 0 ; // $a defines  how many times I can spend the money
$b = 0; //defines how many turns I have done;
while($money>0){
    if($money>50000){ //First option - i loose 5% of my money
        $money = $money * 0.95;
        $b = $b+1;
    }else{ //I already know that I have money
        if($money>5000){ //I can do another turn
            $money = $money - 5000;
            $b = $b + 1;
        }else{ //I have less then the money I need to do another turn
            echo "You made $b turns and now you have only $money left";
            $money = 0; //I reset the money so I get out of the while loop
            exit(); //i go out of the loop since I have nothing more to spend
        }
    }
}

With these figures the result of this code will be:

You made 23 turns and now you have only 3767.497911553 left

I didn't get if you also want to simulate how many times left you have when you do one turn. This would lead to a different setup of the program.

Upvotes: 2

souki
souki

Reputation: 1385

The problem is your second loop condition :

while($money<=50000) 

This will always be true, since you're decreasing the money you have.

So it should be while ($money >= 5000) since you must have at least 5000 to pay the fee

Upvotes: 1

N.K
N.K

Reputation: 1690

In your second loop you pass as long as you have less than 50,000 and since you only decrease your amount of money, you'll stay in there for ever, i guess you want to stop it once you reach 0$ or less, so you could do that:

while($money >= 0 && $money<=50000)

In this case the loop will stop decreasing your money as soon as you reach 0 or below. If you want to loop until you can't pay the fee anymore just do:

while($money >= 5000 && $money<=50000)

Upvotes: 2

S&#233;bastien S.
S&#233;bastien S.

Reputation: 1544

while($money<=50000)

Your money will never be bigger than 50000 in your second loop because you just decrease it $money=$money-5000; so you enter in an infinite loop.

Upvotes: 1

Related Questions