The code you have written makes no sense.
To exit the for loop when you find your guess, add a break
below the guessed := true.
Why are you repeating the same logic infinitely using one condition that will always be true, and one condition that might easily be never true? I'm guessing you've shown part of your code inside that repeat loop, and not all of it. In that case, a good convention on stackoverflow for code samples is to put in something like: do_something_here();
in the place where you really have 100 more lines you didn't want to show here.
It would be great if you showed the var declarations so we can see your types.
Your code would make sense again if:
- You break from the for loop.
- You only need to do the While loop if you do something variant in each case, such as ask the user to input some new input.
here's some code that does something at least useful:
(1) it will ask you to enter something, and repeat until you enter something that matches an element in the hard coded list.
(2) It will terminate, given certain inputs from you, unlike your code which may never terminate. It also doesn't waste time in the for loop on the other elements once it finds a match.
(3) It shows trivially that you should validate the inputs, or you will have exceptions. What if you entered nothing and just hit enter in your code? You would get an endless loop.
procedure Demo;
var
guess:Char;
guesses:Array of Char;
i: Integer;
guessed:Boolean;
begin
repeat
setup_guesses(guesses); // not shown
Write ('Guess a letter: ');
Readln (guess);
Guess := UpCase(Guess);
if (Ord(Guess)>='A') and (Ord(Guess)<='Z') then begin
guessed := false;
for i := Low(guesses) to High(Guesses) do // why hard code 1..20???
begin
if guess = guesses[i] then
begin
guessed := true;
break;
end;
end;
end;
until (guessed = true);
if guessed then
WriteLn('An element in guesses matched your input')
else
WriteLn('No element in guesses array matches your input')
end;
break
if you setguessed
to true. It makes no sense to continue the search. – jpfollenius