Solved

do/while loop

Posted on 2013-06-03
6
357 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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Thank you
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:imagekrazy
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now