imagekrazy
asked on
do/while loop
Hi, I am trying a do/ while loop
and i want to know if the logic is right, here is the code
<?php
// do-while loop below
do{
$chicks = rand(0,1,2,3,4);
$chicksCount ++;
if($chicks){
echo"how many chicks in a bar?";
}
else {
echo "how many dudes?";
}while($chicks);
$longHair ="girls";
$shortHair="dudes";
echo"there are {$chicks}{$longHair}or just{$shortHair}";
?>
and i want to know if the logic is right, here is the code
<?php
// do-while loop below
do{
$chicks = rand(0,1,2,3,4);
$chicksCount ++;
if($chicks){
echo"how many chicks in a bar?";
}
else {
echo "how many dudes?";
}while($chicks);
$longHair ="girls";
$shortHair="dudes";
echo"there are {$chicks}{$longHair}or just{$shortHair}";
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The do/while is correct in terms of it will run however some comments on the code
1. chicksCount is never used - so incrementing in the loop is not really achieving anything
2. You will only ever have one instance of how many dudes - as this can only happen when chicks is 0 which terminates the loop
3. You are assigning chicks a random value but this is only used for terminating the loop. Your use of rand(0,1,2,3,4) is understandable only if you want a 1 in 5 chance of it selecting a 0.
4. The last echo of the code will always output
there are {0}{girls} or just{dudes}
I am guessing you wanted to put chickCount in where you currently have chicks
i.e.
So in answer to your question - is the logic right - we can't answer that without knowing what it is you are trying to achieve.
1. chicksCount is never used - so incrementing in the loop is not really achieving anything
2. You will only ever have one instance of how many dudes - as this can only happen when chicks is 0 which terminates the loop
3. You are assigning chicks a random value but this is only used for terminating the loop. Your use of rand(0,1,2,3,4) is understandable only if you want a 1 in 5 chance of it selecting a 0.
4. The last echo of the code will always output
there are {0}{girls} or just{dudes}
I am guessing you wanted to put chickCount in where you currently have chicks
i.e.
<?php
// do-while loop below
do{
$chicks = rand(0,1,2,3,4);
$chicksCount ++;
if($chicks){
echo"how many chicks in a bar?";
}
else {
echo "how many dudes?";
}while($chicks);
$longHair ="girls";
$shortHair="dudes";
echo"there are {$chicksCount}{$longHair}or just{$shortHair}";
?>
The while loop version could be written aswhile(rand(0,1,2,3,4)) {
$chicksCount++;
echo"how many chicks in a bar?";
}
echo"how many dudes?";
echo"there are {$chicksCount}{girls} or just{dudes}";
Not sure what the purpose of longhair / shorthair is - so in above example have just put in the constants in the output.So in answer to your question - is the logic right - we can't answer that without knowing what it is you are trying to achieve.
ASKER
Thank you also, I don't know how to assign points to you after I already submitted the points to the first question.
Thanks for the points (and buy that book -- a month of study will put you at least a year ajead of trial and error learning, I promise), ~Ray
Thank you also, I don't know how to assign points to you after I already submitted the points to the first question.
Don't worry - I submitted after you had closed the question.
ASKER