Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

do/while loop

Posted on 2013-06-03
6
Medium Priority
?
380 Views
Last Modified: 2013-06-03
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}";
    ?>
0
Comment
Question by:imagekrazy
  • 2
  • 2
  • 2
6 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39216337
This book will help you get a foundation in PHP.
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0672329166/

Just a guess, but I think most professional programmers would probably use a while() loop instead of the do/while construct.

<?php
$chicks = rand(0,1,2,3,4);
while ($chicks> 0)
{
    echo "$chicks CHICKS REMAIN";
    $chicks--;
}
echo "CHICKS ALL GONE";

Open in new window

0
 

Author Closing Comment

by:imagekrazy
ID: 39216427
Thank you
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39216453
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.
<?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}";
?> 

Open in new window

The while loop version could be written as
while(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}";

Open in new window

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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:imagekrazy
ID: 39216488
Thank you also, I don't know how to assign points to you after I already submitted the points to the first question.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39216549
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
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39216589
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question