View Single Post
  #4  
Old 08-08-2010, 03:29 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
This might help with the for loop.


Quote:
Originally Posted by cbk1994 View Post
Let's say you have something like
PHP Code:
for (temp.010++) {
  echo(
i);
  
sleep(1);

It's going to output this in RC, with a one second delay between each number.

Quote:
0
1
2
3
4
5
6
7
8
9
What it does is loop through something a certain number of times.

PHP Code:
function countTo(numberdelay) {
  for (
temp.0number++) {
    echo(
i);
    
sleep(delay);
  }

would let you count to any number.

PHP Code:
temp.0;

for (
temp.05++) {
  
+= int(random(15));
  
sleep(0.5);

Here's what this would be doing in English.

Quote:
SET v TO 0
START FOR LOOP
SET n TO 0
SET v to 2
SLEEP .5 SECONDS
SET n TO 1
SET v to 3
SLEEP .5 SECONDS
SET n to 2
SET v to 6
SLEEP .5 SECONDS

[and all the way to 4]
Here's the basic structure.

PHP Code:
for (variable startvariable (inequalitymax/minvariable change) {
  
// code to be executed

so

PHP Code:
for (temp.30--) {
  echo(
x);

would say that x starts at 3. It would then execute the code in the middle by outputting '3'. Then, x is decreased to 2. It checks if 2 is more than 0, which it is, so it keeps going and executes the code. x now equals 1, which is more than 0, so it goes again. x now equals 0, which is not more than 0, so it breaks out of the for loop.


Another, different type of for loop is like a foreach loop in other languages. It's done like this:

PHP Code:
for (variable : array) {
  echo(
variable);

Arrays are defined like this

PHP Code:
temp.array = {"one""two""three""hello""world"}; 
which means you can also do

PHP Code:
for (variable : {"one""two""three""hello""world"}) {
  echo(
variable);

however, you need to specify a variable

PHP Code:
for (temp.number : array) {
  echo(
number);

would output

Quote:
one
two
three
hello
world
Be sure not to get the two types of for loops mixed up. The for each loop does the same thing as

PHP Code:
temp.array = {"one""two""three""hello""world"};

for (
temp.0< array.size(); ++) {
  
temp.variable = array[i];

__________________
Reply With Quote