while loop question

Posted on 2015-02-10
Last Modified: 2015-02-11
I need to run a a chunk of code for 50 times.

Then increment a variable by 50 in that code and run it again and each time add another 50 to the variable until I have run the code a total of 3500 times.
Question by:austinfx
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40602233
Typically that is done with a 'for' loop because a 'for' is done by the count.  A 'while' can too but the logic for the count is part of the 'for' loop.

Author Comment

ID: 40602247
With my question using the information can you show me an example?
LVL 83

Accepted Solution

Dave Baldwin earned 333 total points
ID: 40602256
The simplest way is to put a loop inside a loop.  Since 3500 / 50 = 70...
$x = 0;
for ($i = 1; $i <= 70; $i++) {
  for ($j = 1; $j <= 50; $j++) {
    echo $j;  // do something here
$x = $x+50;

Open in new window

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

ID: 40602260
This looks so intimidating. I'm having trouble understanding it. I know I'm very basic.
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 167 total points
ID: 40602348
X is your variable that you are adding 50 to.

The loop in defining a counter (I) that starts at the value 1. Every time the loop is executed the value of I will be incremented by 1. This is what the I++ is doing.  the middle section of the for loop line is defining how many times the loop will run. So the above will keep looping while I is less than or equal to 70. If you wanted the loop to run 3500 times you would change that number. Hope that helps.
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 333 total points
ID: 40602414
The inner loop runs 50 times each time.  The outer loop runs 70 time and each time it runs, it runs the inner loop.  70 x 50 = 3500.  $x is there only because you wanted to add 50 to something each time the inner ran.

Author Closing Comment

ID: 40603774
That was it thank you all for being so informative!

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migrating to phpbb forum from vBulletin 4.2 3 94
JSON decode 5 46
Format Date 7 28
SSL unsecure page mystery 17 46
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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