SRL Archive - Help

What's wrong with this procedure?


KeepBotting

2012-07-03 14:52:39

Here's my procedure.

procedure SailCheck;
begin
if FindBlackChatMessage('coins and board the ship') then
WriteLn('successfully sailed');
if not FindBlackChatMessage ('coins and board the ship') then
WriteLn('something went wrong');
SailBoat; { repeats the previous procedure }
end;

However, when I run it, it seems to return both true and false for the FindBlackChatMessage. In any case, it runs both messages.

Wat do?

Runaway

2012-07-03 14:55:50

Well, you've got a space between the second FindBlackChatMessage and ('coins and board the ship'). Also, you mine as well use an else statement!


procedure SailCheck;
begin
if FindBlackChatMessage('coins and board the ship') then
WriteLn('successfully sailed')
else
begin
WriteLn('something went wrong');
SailBoat;
end;
end;

KeepBotting

2012-07-03 14:56:51

From: Runaway
Well, you've got a space between the second FindBlackChatMessage and ('coins and board the ship'). Also, you mine as well use an else statement!


procedure SailCheck;
begin
if FindBlackChatMessage('coins and board the ship') then
WriteLn('successfully sailed')
else
begin
WriteLn('something went wrong');
SailBoat;
end;
Ooh, thanks! :D I had tried to use an else statement before, but I neglected to use another begin and end with it.

Runaway

2012-07-03 15:00:53

No problem :)

You need a begin/end if you're using more than one procedure/statement after the if..then or the else. And if you use more than one procedure/statement between the if..then and the else, you use begin..end else instead of begin..end; Example (other than what I posted above):


procedure SailCheck;
begin
if FindBlackChatMessage('coins and board the ship') then
begin
WriteLn('successfully sailed');
WaitSailing;
end else
WriteLn('something went wrong');
end;

PS: I forgot the second end; in the first post!

KeepBotting

2012-07-03 15:09:25

From: Runaway
No problem :)

You need a begin/end if you're using more than one procedure/statement after the if..then or the else. And if you use more than one procedure/statement between the if..then and the else, you use begin..end else instead of begin..end; Example (other than what I posted above):


procedure SailCheck;
begin
if FindBlackChatMessage('coins and board the ship') then
begin
WriteLn('successfully sailed');
WaitSailing;
end else
WriteLn('something went wrong');
end;

PS: I forgot the second end; in the first post!
Interesting. Because when I use something like if FindObj(blah, blah, blah, blah) then and there is no begin/end or an else, but it still works.

Recursive

2012-07-03 15:16:02

Depends on what you are returning, sometimes it might return both results but you may have not been checking for both so you think it returned just one.

Runaway

2012-07-03 15:18:42

From: KeepBotting
Interesting. Because when I use something like if FindObj(blah, blah, blah, blah) then and there is no begin/end or an else, but it still works.


This is because an if..then will apply to the line after it. For example:


if This then
That;
Writeln('Hopefully we did that!');

^ it will write "Hopefully we did that!" no matter what; but it will only do That; if This is true.

KeepBotting

2012-07-03 15:57:52

From: Runaway
This is because an if..then will apply to the line after it. For example:


if This then
That;
Writeln('Hopefully we did that!');

^ it will write "Hopefully we did that!" no matter what; but it will only do That; if This is true.
That's interesting…when I first started programming, I used the BASIC language (DOS ftw!?!?!?) and it didn't follow that rule.

Runaway

2012-07-03 16:09:53

From: KeepBotting
That's interesting…when I first started programming, I used the BASIC language (DOS ftw!?!?!?) and it didn't follow that rule.


Well I'm not sure about other languages, but pascal follows the typical "semi-colon" rules. a simple if..then statement is followed by a single semi-colon, so it counts as a single line; no matter the spacing between it. What I posted above is the same as:


if This then That;
Writeln('Hopefully we did that!');

So if you want to involve multiple procedures/statements within that if..then, you must use a begin/end since:


if This then That; Writeln('Doing that!');

counts as two lines. Using the begin/end is what allows you to link multiple procedures to the single if..then:


if This then begin That; Writeln('Doing that!'); end;

:)

Tniffoc

2012-07-03 16:15:11

The easiest way to think about it is to think of begins and ends as a way to box things together. An if statement will only do the thing on the next line EVER. The begins and ends will just package a bunch of lines together so that the if statement sees it as one line. Thats how I think about it anyways, but maybe I'm just screwed up.