Author Topic: Why won't this work?  (Read 2425 times)

I'm trying to make it echo
1
2
3
4
5
6
7
8
9
10
by using this script in console :
function count(%y){ %y=0; if(%y != 10){echo(%y++);}}
but for some reason it only echoes:
1
why won't this script work? Any help please?

function test(%y)
{
for(%i=0;%i<%y;%i++)
echo(%y);
}
Fixed :)

for(statement used on start; statement check for end; statement used when finished)
{
     your work
}
:)
« Last Edit: January 24, 2012, 06:39:17 PM by Brian Smithers »

function test(%y)
{
for(%i=0;%i<%y;%y++)
echo(%y);
}
Written on my iPad c:
This makes no sense to me? line 2 makes no sense. I don't understand, it could be easier if you explained.

function test(%y)
{
for(%i=0;%i<%y;%y++)
echo(%y);
}
Written on my iPad c:
Wouldn't this only echo once?

resulting in you needing to enter it 10 times to get the desired result?

and @OP, you don't actually alter the value, you echo it with a +1, and since the value is 0, it will always echo 1
« Last Edit: January 23, 2012, 12:20:47 AM by soba »

Wouldn't this only echo once?

resulting in you needing to enter it 10 times to get the desired result?
No, the echo is run on every loop.  The for loop runs for an infinite number of times until %i>=%y then it stops.  So test(%y) would count up to whatever number you put in and echo each time it counts.

No, the echo is run on every loop.  The for loop runs for an infinite number of times until %i>=%y then it stops.  So test(%y) would count up to whatever number you put in and echo each time it counts.
I see

EDIT: then that loop never ends


Fixed I think. ._.
%i++ and echo(%i); you big silly willy

The reason the code in the OP does not work is mainly because it does not loop at all

function count(%y)
{
   %y=0;
   if(%y != 10)
   {
      echo(%y++);
   }
}

First of all, the %y=0; would negate the point of having a %y argument
then it tests for if y is equal to ten, since it just got set to zero, this will of course be false.
The it will echo y incremented by one, which is 0+1 = 1
But then it hits the end of the function.  To do this in the format you might be looking for...

Code: [Select]
function count(%y)
{
      if(%y < 10)
      {
            echo(%y++);
            count(%y);
      }
}

Having it call another count will make it loop back and call the function again, causing %y to again be incremented and echoed, which is exactly what you were trying to do.
That way, if you enter count(0); into the console, it should work correctly.

Another method you might have been trying would be

function count(%y)
{
      while(%y<10)
      {
            echo(%y++);
      }
}

which will run all the code inside the while loop until the test (%y<10) is no longer true

But that last example wouldn't modify the variable
Don't you need a %y++ on its own?

Fixed:
Code: [Select]
function Count(%end)
{
for(%count = 1; %count <= %end; %count++)
echo(%count);
}

function test(%y)
{
for(%i=0;%i<%y;%i++)
echo(%y);
}
his example is stuff, but you have to insert the y variable. test(10); would say ten 10 times, becaus he made it echo %y instead of %i. in that scenario it would be the same as:
for(%i = 0; %i < 10; %i++)
{
    echo(10);
}


for statements work like such

for(%variable = value; %variable (less than, more than, equal to, not equal to, etc) number; modify %variable)

inside the {}s for a for statement is repeated until the condition in the middle is met, in this case until %i is as big as 10.

for(i is zero, i is less than 10, increase i by one)
{
    echo(i)
}

A for loop is a simplified while loop.

Code: [Select]
%var = 0;

while(%var < 100) {
echo("Loop #" @ %var);
%var++;
}

This was very standard practice. Since for loops were conceived, this extremely common loop type can be reduced to:

Code: [Select]
for(%var = 0; %var < 100; %var++) {
echo("Loop #" @ %var);
}

You can put anything you want in it, though.

Code: [Select]
for(openDoor(); getOnFloor(); walkDinosaur()) { }
That code will call openDoor(), check to see if getOnFloor() returns a boolean value of true, and for every time it does, calls walkDinosaur().


Think I'm kidding? Run this code.

Code: [Select]
function openDoor() { echo("Open the door!"); }
function getOnFloor() { echo("Get on the floor!"); return getRandom(0, 10) != 10; }
function walkDinosaur() { echo("Everybody walk the dinosaur!"); }
for(openDoor(); getOnFloor(); walkDinosaur()) { }


Output:
Code: [Select]
Open the door!
Get on the floor!
Everybody walk the dinosaur!
Get on the floor!
Everybody walk the dinosaur!
Get on the floor!
Everybody walk the dinosaur!
Get on the floor!