SRL Archive - OSR Help

Problem with the If...Then statement


oudshoorn

2012-07-20 17:12:47

Helloo everyone!

I am quite new here and recently started to get interested in scripting.
Now I've encountered my first problem which I can't manage to solve on my own, so that's why I came here.

Here is the code:


program Problem;

var
Logs: string;
Experience: extended;

procedure HereItComes;

begin
Logs:= 'Willow';
if Lowercase(Logs) = 'Willow' then
Experience:= 20;
if Lowercase(Logs) = 'Yew' then
Experience:= 30;

writeln('We are cutting ' + Logs + ' logs and we are gaining ' + FloatToStr(Experience) + ' experience.');

end;

begin
HereItComes;
end.

Playing the script will result in the script writing down that it is cutting willow logs but not that is gaining 20 experience.
Is there any way to fix this?
If you have any suggestions of doing something different I would love to hear it!
I hope my problem can be solved.

Thanks in advance,

Oudshoorn

YoHoJo

2012-07-20 17:18:38

Use a case instead

 Case Players[i].Integers[1] Of

1,2,3: Players[i].Extendeds[1]:=17.5;
4: Players[i].Extendeds[1]:=35;
5: Players[i].Extendeds[1]:=50;

End;

So like

 Case Logs Of

'Willow': Experience:=20;
'Yew;: Experience:=30;

End;

All Gud?!

Also! You are using Lowercase, and checking if it = 'Willow' that will never be true since you have a capital W in there!
It should be
if Lowercase(Logs) = 'willow' then

:]?

oudshoorn

2012-07-20 17:59:47

Thanks for the respond!
Now I know why i have this problem.

P1ng

2012-07-20 19:53:29

Yeah, your example in the OP would work fine if you just change the capital letters out of the if..thens

so
 
if Lowercase(Logs) = 'willow' then
Experience:= 20;
if Lowercase(Logs) = 'yew' then

But, the method YoHoJo outlined is a little bit more advanced but certainly alot neater and simpler to read.

riwu

2012-07-20 20:12:34

Case statements are not really advance (its in the beginner's tutorials!), its quite simple and easy to understand and its really useful.
Also the correct way of using if then for the same operation should be
if a=1 then
action1 //no semicolon since sentence is incomplete
else //without this, its going to perform the next check even if the first is true
if a=2 then
action2;
In this case it doesn't really affect much, but if used incorrectly in some cases it may perform both Action1 and Action2 when the Boolean is dynamic (if u dont have the else) since its going to check both boolean even if the first is true.