Drakan
05-23-2008, 05:19 PM
Hey and welcome to my first tutorial! In this tutorial I will discuss If-Then statements, For statements, While Statement, and cases! Please rate this tutorial and give feedback!
If, then's
If-Then statements are probably the most simple of them all. They are very self explanatory (if something then do this.) An example of an if statement:
if(LoggedIn=true)then
begin
DoSomething;
end;
That is the basic structure of an if statement. You can also use things like >, <, =.
if(2 > 1)then
begin
Writeln('2 is indeed greater than 1.');
end;
Now you want your code to be as efficient as possible. Lets say you are going to do one thing if you are not logged in. Lets say if you arent logged in then Exit; There are some shortcuts to writing this. Instead of:
if(LoggedIn = false)then
begin
Exit;
end;
you could write
if not(LoggedIn)then exit;
Nice one line =]. now saying if not(Something) is like saying if(Something = false)
if not(1>2)then Writeln('1 is not greater than 2.');
If you are going to execute more than one line in an if statement you must do this:
if(Something)then //notice how i dont use Something=true! You can jsut say if(Something)then !!
begin
FirstThing;
SecondThing;
end;
Dont forget the begin and end ;)
For Statements
For statements are good for many uses. One that I use often is actually for progress reports. Looky here:
var i: integer;
begin
for i := 0 to HowManyPlayers-1 do
begin
Writeln('Player '+inttostr(i)+' is '+Players[i].Name);
end;
end;
Lets walk through this:
var i: integer;: This is declaring i and an integer.
for i := 0 to HowManyPlayers-1 do: This says: i will = 0 to HowManyPlayers minus 1. Meaning it will first = 0 then do the stuff. Then it will = 1 then do the stuff, and so on.
Lets say you have HowManyPlayers set to 3. The script would print out:
Player 0 is Ricky
Player 1 is Bobby
Player 2 is Freddy
While Statements
While statements are fairly easy too!
While(Something)do
begin
Something;
end;
Example:
While(IsMining)do
begin
Wait(100);
AntiBan;
AntiRandoms;
end;
What this means is: While isMining = true then it will Wait(100) then will do some antiban, then some antiRandoms and will repeat until isMining = false.
While statements don't require being's and end's either! watch this!
While(True) do This;
=]
Cases!
Cases are very good ways to shorten up if statements. Lets say you had this procedure:
procedure Test;
var
i: integer;
begin
i := Random(5);
if(i = 0)then
begin
DoThis;
DoThat;
end;
if(i = 1)then
begin
DoThis;
DoThat;
end;
if(i = 2)then
begin
DoThis;
DoThat;
end;
if(i = 3)then
begin
DoThis;
DoThat;
end;
if(i = 4)then
begin
DoThis;
DoThat;
end;
end;
This could easily be shortened into this!:
procedure Test;
begin
case Random(5) of
0: DoThis;
1: DoThis;
2: DoThis;
3: DoThis;
4: DoThis;
end;
end;
ooo look at the efficiency! So prettttttyyyy... :p
Cases can take more than just an integer form:
procedure Test(theOre: String);
begin
case Lowercase(theOre) of
'copper': MineCopper;
'tin': MineTin;
'iron': MineIron;
end;
end;
procedure Test(Members: boolean);
begin
case Members of
True: DoMemebersStuff;
False: DoF2pStuff;
end;
end;
There are many more, but these are the basic ones.
Thats all for this tutorial! Please post any suggestions!
If, then's
If-Then statements are probably the most simple of them all. They are very self explanatory (if something then do this.) An example of an if statement:
if(LoggedIn=true)then
begin
DoSomething;
end;
That is the basic structure of an if statement. You can also use things like >, <, =.
if(2 > 1)then
begin
Writeln('2 is indeed greater than 1.');
end;
Now you want your code to be as efficient as possible. Lets say you are going to do one thing if you are not logged in. Lets say if you arent logged in then Exit; There are some shortcuts to writing this. Instead of:
if(LoggedIn = false)then
begin
Exit;
end;
you could write
if not(LoggedIn)then exit;
Nice one line =]. now saying if not(Something) is like saying if(Something = false)
if not(1>2)then Writeln('1 is not greater than 2.');
If you are going to execute more than one line in an if statement you must do this:
if(Something)then //notice how i dont use Something=true! You can jsut say if(Something)then !!
begin
FirstThing;
SecondThing;
end;
Dont forget the begin and end ;)
For Statements
For statements are good for many uses. One that I use often is actually for progress reports. Looky here:
var i: integer;
begin
for i := 0 to HowManyPlayers-1 do
begin
Writeln('Player '+inttostr(i)+' is '+Players[i].Name);
end;
end;
Lets walk through this:
var i: integer;: This is declaring i and an integer.
for i := 0 to HowManyPlayers-1 do: This says: i will = 0 to HowManyPlayers minus 1. Meaning it will first = 0 then do the stuff. Then it will = 1 then do the stuff, and so on.
Lets say you have HowManyPlayers set to 3. The script would print out:
Player 0 is Ricky
Player 1 is Bobby
Player 2 is Freddy
While Statements
While statements are fairly easy too!
While(Something)do
begin
Something;
end;
Example:
While(IsMining)do
begin
Wait(100);
AntiBan;
AntiRandoms;
end;
What this means is: While isMining = true then it will Wait(100) then will do some antiban, then some antiRandoms and will repeat until isMining = false.
While statements don't require being's and end's either! watch this!
While(True) do This;
=]
Cases!
Cases are very good ways to shorten up if statements. Lets say you had this procedure:
procedure Test;
var
i: integer;
begin
i := Random(5);
if(i = 0)then
begin
DoThis;
DoThat;
end;
if(i = 1)then
begin
DoThis;
DoThat;
end;
if(i = 2)then
begin
DoThis;
DoThat;
end;
if(i = 3)then
begin
DoThis;
DoThat;
end;
if(i = 4)then
begin
DoThis;
DoThat;
end;
end;
This could easily be shortened into this!:
procedure Test;
begin
case Random(5) of
0: DoThis;
1: DoThis;
2: DoThis;
3: DoThis;
4: DoThis;
end;
end;
ooo look at the efficiency! So prettttttyyyy... :p
Cases can take more than just an integer form:
procedure Test(theOre: String);
begin
case Lowercase(theOre) of
'copper': MineCopper;
'tin': MineTin;
'iron': MineIron;
end;
end;
procedure Test(Members: boolean);
begin
case Members of
True: DoMemebersStuff;
False: DoF2pStuff;
end;
end;
There are many more, but these are the basic ones.
Thats all for this tutorial! Please post any suggestions!