Rezozo
2012-09-29 02:51:24
Welcome to my second TuT, a sequel to my first!
*Note - This is a continuation of my previous thread: http://villavu.com/forum/showthread.php?p=1106349 , If you have not been through that, please do so now.
We will now be covering the topic of:
Using different types of variables.
Making and Using arrays.
Making and using a loop.
1) Using different types of variables:
If you remember, we already discussed how to output a string variable. Now lets move on to the numbers.
When outputting a number, such as an integer or real number, you use this:
Now notice how I don't use the '' for a number. It is only necessary for a char or string.
Declaring a real doesn't work in Simba, and Char is in the same format as with String.
2) Declaring and using an array -
First of all, what is an array. It is a variable that sets a value in sectors/dimensions. Say for example, you have a 1X1 array. That holds only 1 value. But a 5X1 array will hold a list of values. Now consider a 5X5 array. This goes 5 in one way, 5 in another. Like two dimensions. Same way a 5X3X8 array is a three dimensional array.
Lets declare an array. The normal setup is:
To actually assign values to an array, you must use this format:
When outputting a value out of your array, you use this
3) - Loops! My favorite!
Loops, literally, loop parts of your script, making do things as many times as you want.
One of the most common loops is the For-Do loop.
Its structure is:
This makes, for value of the integer y (must be declared), from 1 to 10 (10 times) do something (whatever is in the Begin End.) This changes the value of y, every time the loop goes on.
Now lets take all this knowledge and put it together!
Lets make a program that fills a one dimensional array with 7 string values each saying "Hello world!", and repeats the output on separate lines 7 times.
What we get is:
Now I realize that was a little bit tricky, but if you think about it hard enough, you realize you have to put the value of y (as it changes from 1 to 7) in as the placer of the array X.
If however, you thought even harder, you might've come up with this:
Which does the exact same thing, in a more efficient fashion.
This concludes the TuT no.2 for pascal! i will probably be making a no. 3 if there is a demand for it. Though I think you can understand most RS scripting TuTs by now!
If it is requested enough, the next one would be about 2-Dimensional arrays, other types of loops and using inputs with points on the screen. (Not graphical points, but co ordinate points :) )
If you have any questions or comments, please leave them below. This is to be a good/clean thread. No Spam please!
*Note - This is a continuation of my previous thread: http://villavu.com/forum/showthread.php?p=1106349 , If you have not been through that, please do so now.
We will now be covering the topic of:
Using different types of variables.
Making and Using arrays.
Making and using a loop.
1) Using different types of variables:
If you remember, we already discussed how to output a string variable. Now lets move on to the numbers.
When outputting a number, such as an integer or real number, you use this:
program TuT;
Var
x:integer;
begin
x:= 5;
Writeln(x);
end. Now notice how I don't use the '' for a number. It is only necessary for a char or string.
Declaring a real doesn't work in Simba, and Char is in the same format as with String.
2) Declaring and using an array -
First of all, what is an array. It is a variable that sets a value in sectors/dimensions. Say for example, you have a 1X1 array. That holds only 1 value. But a 5X1 array will hold a list of values. Now consider a 5X5 array. This goes 5 in one way, 5 in another. Like two dimensions. Same way a 5X3X8 array is a three dimensional array.
Lets declare an array. The normal setup is:
program TuT;
Var
Arr : Array[0..4] of Integer;
begin
end. That is declaring Arr as an array, carrying 5 values, which are integers. Notice how I start the array with an index of zero. Though this doesn't actually change the script, zero is used in almost all programming languages as the start of an array. (Mainly because zero is the first possible whole number, can't have a -1 in a dimension.To actually assign values to an array, you must use this format:
program TuT;
Var
Arr : Array[0..4] of Integer;
begin
Arr[4] := 10;
Writeln(Arr[4]);
end.When outputting a value out of your array, you use this
Writeln(Arr[5]); outputting the fifth digit of the 1 dimensional array Arr.3) - Loops! My favorite!
Loops, literally, loop parts of your script, making do things as many times as you want.
One of the most common loops is the For-Do loop.
Its structure is:
For i:= 1 to 10 do
Begin
End;This makes, for value of the integer y (must be declared), from 1 to 10 (10 times) do something (whatever is in the Begin End.) This changes the value of y, every time the loop goes on.
Now lets take all this knowledge and put it together!
Lets make a program that fills a one dimensional array with 7 string values each saying "Hello world!", and repeats the output on separate lines 7 times.
What we get is:
program TuT;
Var
StrArr : Array[0..6] of String;
i : Integer;
begin
StrArr[1] :='Hello World';
StrArr[2] :='Hello World';
StrArr[3] :='Hello World';
StrArr[4] :='Hello World';
StrArr[5] :='Hello World';
StrArr[6] :='Hello World';
StrArr[7] :='Hello World';
For i:= 1=0 to 6 do
Begin
Writeln(StrArr[i]);
End;
end.Now I realize that was a little bit tricky, but if you think about it hard enough, you realize you have to put the value of y (as it changes from 1 to 7) in as the placer of the array X.
If however, you thought even harder, you might've come up with this:
program TuT;
Var
StrArray : Array[0..6] of String;
i : Integer;
begin
For i:= 0 to 6 do
Begin
StrArr[i]:= 'Hello World';
Writeln(StrArray[i]);
End;
end.Which does the exact same thing, in a more efficient fashion.
This concludes the TuT no.2 for pascal! i will probably be making a no. 3 if there is a demand for it. Though I think you can understand most RS scripting TuTs by now!
If it is requested enough, the next one would be about 2-Dimensional arrays, other types of loops and using inputs with points on the screen. (Not graphical points, but co ordinate points :) )
If you have any questions or comments, please leave them below. This is to be a good/clean thread. No Spam please!