Recent

Author Topic: How to go back (and forward) to a given part of a program?  (Read 1519 times)

JamesSci

  • New Member
  • *
  • Posts: 30
How to go back (and forward) to a given part of a program?
« on: March 24, 2020, 12:06:22 am »
Hi!

I'm writing a Pascal number guessing game so that I can have fun and guess numbers until the pandemic ends (and as a beginners exercise).

Here's my code so far:

Code: Pascal  [Select][+][-]
  1. program GuessMyNumber;
  2.  
  3. var
  4.   MyNumber, TheirGuess: integer;
  5.  
  6. begin
  7.   Randomize;
  8.   MyNumber := random(100);
  9.  
  10.   { >>START<< }
  11.  
  12.   WriteLn('Guess my number:');
  13.   ReadLn(TheirGuess);
  14.  
  15.   if TheirGuess = MyNumber then
  16.   begin
  17.     WriteLn('Congratulations, you guessed my number right!');
  18.     ReadLn;
  19.   end
  20.   else
  21.   if TheirGuess > MyNumber then
  22.   begin
  23.     WriteLn('Too high!');
  24.     ReadLn;
  25.     { Return to START }
  26.   end
  27.   else
  28.   if TheirGuess < MyNumber then
  29.   begin
  30.     WriteLn('Too low!');
  31.     ReadLn;
  32.     { Return to START }
  33.   end;
  34. end.

I'm wondering if, as demonstrated by the comments, there's a way to return to a given point in the program? Any general beginner tips on how I could make this program a little less verbose is very much appreciated too.

Thanks for your help!
« Last Edit: March 24, 2020, 12:14:05 am by JamesSci »

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: How to go back (and forward) to a given part of a program?
« Reply #1 on: March 24, 2020, 12:43:10 am »
I'm a bit bored tonight so here is a small change of your code.

Code: Pascal  [Select][+][-]
  1. program GuessMyNumber;
  2. uses math;
  3.  
  4. const
  5.   Messages : array[TValueRelationship] of string = ( 'Your value is lower.',
  6.                                                      'Congrats. You guessed right!',
  7.                                                      'Your value is higher.');
  8.  
  9. var
  10.   MyNumber, TheirGuess: integer;
  11.   Comp : TValueRelationship;
  12. begin
  13.   Randomize;
  14.   MyNumber := random(100);
  15.  
  16.   { >>START<< }
  17.   repeat
  18.     WriteLn('Guess my number or -1 to exit :');
  19.     ReadLn(TheirGuess);
  20.     comp := CompareValue(TheirGuess, MyNumber); //try to change the order of the parameters and see what happens.
  21.     WriteLn(Messages[Comp]);
  22.     if Comp = 0 then begin
  23.       MyNumber := Random(100);
  24.       WriteLn('I have a new number for you.');
  25.     end;
  26.   until (TheirGuess = -1);
  27.   WriteLn('Press enter to exit');
  28.   ReadLn;
  29. end.
  30.  

JamesSci

  • New Member
  • *
  • Posts: 30
Re: How to go back (and forward) to a given part of a program?
« Reply #2 on: March 24, 2020, 12:57:51 am »
I'm a bit bored tonight so here is a small change of your code.

Code: Pascal  [Select][+][-]
  1. program GuessMyNumber;
  2. uses math;
  3.  
  4. const
  5.   Messages : array[TValueRelationship] of string = ( 'Your value is lower.',
  6.                                                      'Congrats. You guessed right!',
  7.                                                      'Your value is higher.');
  8.  
  9. var
  10.   MyNumber, TheirGuess: integer;
  11.   Comp : TValueRelationship;
  12. begin
  13.   Randomize;
  14.   MyNumber := random(100);
  15.  
  16.   { >>START<< }
  17.   repeat
  18.     WriteLn('Guess my number or -1 to exit :');
  19.     ReadLn(TheirGuess);
  20.     comp := CompareValue(TheirGuess, MyNumber); //try to change the order of the parameters and see what happens.
  21.     WriteLn(Messages[Comp]);
  22.     if Comp = 0 then begin
  23.       MyNumber := Random(100);
  24.       WriteLn('I have a new number for you.');
  25.     end;
  26.   until (TheirGuess = -1);
  27.   WriteLn('Press enter to exit');
  28.   ReadLn;
  29. end.
  30.  

Thank you, that looks like a better way of doing it!

jamie

  • Hero Member
  • *****
  • Posts: 7543
Re: How to go back (and forward) to a given part of a program?
« Reply #3 on: March 24, 2020, 01:31:21 am »
You can also use a WHILE LOOP...
Var
 Exiting :Boolean= False;
Begin
While Not Exiting do

 begin
 
   Your code

   Exiting := True; // do this when you want to exit the loop.
 End;
End;

etc.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018