PDA

View Full Version : A lesson on If-then's, For, While, and Cases!



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!

Cazax
05-23-2008, 05:29 PM
You can use If Then without a begin so the script just do the next line. If (LoggedIn = True) Then you can just use If (LoggrdIn) Then

Drakan
05-23-2008, 05:36 PM
I know I already have that in there :p Oh wait... I forgot to put that? I could have sworn i had it in there r0fL thanks!

EDIT: Wait yes i do. Look i put more emphasis on it now :p Its the last box of the if statements.

EDIT2: And look i already said u can have one line XD

kor
05-23-2008, 08:14 PM
i've been looking for this, thanks.

EDIT: 600'th post Ftw :)

bullzeye95
05-23-2008, 08:40 PM
Nice. You should add that you don't have to have begin and ends in while or for loops either.

edggy
05-24-2008, 12:00 AM
Thanks for the nice tutorial
now everything makes sense to me
thank you

Drakan
05-25-2008, 06:13 PM
No problem glad to help :)

triplell
05-30-2008, 02:25 AM
Very Helpful. Helps for user who are familiar with syntax of other programming languages and aren't familiar with pascal.

shrubie1
05-31-2008, 08:24 AM
hmmm. ill try and put some new stuff in my powerchopper im making for my first script>.<

sapetto
08-22-2008, 04:09 PM
Nice now i can make my script :p

P1nky
08-23-2008, 02:02 AM
yeah i really like this tut

really good for failsafes