Solved

while loop question

Posted on 2015-02-10
7
88 Views
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.
0
Comment
Question by:austinfx
  • 3
  • 3
7 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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.  http://php.net/manual/en/control-structures.for.php
0
 

Author Comment

by:austinfx
Comment Utility
With my question using the information can you show me an example?
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 333 total points
Comment Utility
The simplest way is to put a loop inside a loop.  Since 3500 / 50 = 70...
<?php
$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

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:austinfx
Comment Utility
;)
This looks so intimidating. I'm having trouble understanding it. I know I'm very basic.
0
 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 167 total points
Comment Utility
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.
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 333 total points
Comment Utility
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.
0
 

Author Closing Comment

by:austinfx
Comment Utility
That was it thank you all for being so informative!
0

Featured Post

Easy Project Management (No User Manual Required)

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

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

11 Experts available now in Live!

Get 1:1 Help Now