0

I am trying to use multiple if/else statements within a Repeat Until loop (Pascal.) I get an error saying the compiler expects the Until statement after the first If clause is done.

How can I nest the other if/else statements into the loop?

program adventureTime;
uses crt;  (** for debug purposes. it allows the use of the readkey function **)

var
   playerName, inputAnswer : string;
   gameCounter : integer;

begin
    (** INTRODUCTION **)
    gameCounter := 0;

    repeat
            writeln('Hello adventurer. What is thy name? ');
            readln(playerName);

            writeln('It is nice to meet you ', playerName, '.');
            writeln('Are you ready for an adventure? (yes/no/maybe)');

            readln(inputAnswer);

            if (inputAnswer = 'no') then
                    writeln;
                    writeln('Wrong answer! Try again!');
                    writeln;
                    gameCounter := 0;

            else if (inputAnswer = 'yes') then
                    writeln;
                    writeln('Great! Get ready for the adventure of a lifetime!');
                    writeln;
                    gameCounter := 2;

            else if (inputAnswer = 'maybe') then
                    writeln;
                    writeln('Make up your mind, fool!');
                    writeln;
                    gameCounter := 0;

            else
                    writeln;
                    writeln('That was not one of the options!');
                    writeln;
                    gameCounter := 0;

    until gameCounter <> 0;


    writeln('out of bounds');
    readkey;

end.
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
qmesa
  • 5
  • 1
  • 3
  • See [this answer](http://stackoverflow.com/a/28221465/62576) for a tutorial about if and begin/end. – Ken White Jul 25 '16 at 00:49
  • Possible duplicate of [Proper structure syntax for Pascal if then begin end and ; (in Inno Setup)](http://stackoverflow.com/questions/28221394/proper-structure-syntax-for-pascal-if-then-begin-end-and-in-inno-setup) – Ken White Jul 25 '16 at 00:52
  • Thank you, Ken! I was able to solve my problem by combining the tutorial you send with the other answer on this post. – qmesa Jul 25 '16 at 00:54
  • Dave- I wanted to dive into an older language to see what it was like in the old days. So far, Pascal has been a really fun ride. – qmesa Jul 25 '16 at 00:55
  • @DaveNewton; Then apparently you're out of touch. FreePascal is extremely popular, and Delphi (which is based on Object Pascal) now supports cross-platform development for OSX, iOS, Android, and Win32/64, generics, closures, and other modern language features. – Ken White Jul 25 '16 at 00:56
  • @KenWhite I'm a lot of things, but I doubt "out of touch" is one of them. Out of touch with Pascal, definitely-and thankfully. – Dave Newton Jul 25 '16 at 03:44
  • FWIW: an `if-then` clause is followed by one statement, and an optional `else` with a corresponding second statement. Now, a statement can be a single statement (usually a single line), or a so called "compound statement", which is made up of `begin`, followed by zero or more single statements, and an `end`. So a `begin-end` block counts as one statement, although a compound one. This is quite similar as in C, C++, Java, C# etc, where the delimiters are not `begin` and `end`, but `{`and `}` instead. – Rudy Velthuis Jul 27 '16 at 18:03

1 Answers1

0

not sure, but doesn't an if else statement in pascal look like this:

if ... then
begin
.
.
.
end 
else if...
user6454491
  • 158
  • 9
  • Looking at the freepascal documentation (http://www.freepascal.org/docs-html/current/ref/refsu58.html#x163-18500013.2.3) it is not clear to me if that is the case. The documentation suggests (in the example at the buttom) that the end clause does not need to be implemented for every single if/else clause. I was hoping to implement a repeat loop that "wrapped" itself around all the if else clauses (as not to use the end clauses for every single if/else statement). – qmesa Jul 25 '16 at 00:41
  • 1
    I was able to solve the problem by using your comment and Ken's tutorial link. By starting and ending the separate if/else statements, the syntax was correct and the code compiled without a hitch. Thank you! – qmesa Jul 25 '16 at 00:56
  • glad to be able to help you! It seems that you can only omit "begin" and "end" when executing just a single statement after checking the condition. – user6454491 Jul 25 '16 at 01:03
  • @qmesa: you can be sure that you need begin..end. In Pascal, indentation plays no part in the syntax, you need to turn such indented blocks into a so called "compound statement". – Rudy Velthuis Jul 25 '16 at 19:46