There has to be a more efficient way to make this MATLAB loop, right?

I wrote a program that is basically a jumble. It scrambles a word, and the user has ten guesses to get it right. The loop ends when the user hits ten guesses, or gets it right. If works perfectly now, but I can’t help but thinking there is a better way to do it. Here’s the code:


%Now we enter the loop, which will end when the user either correctly
%identifies the word, or has made ten incorrect guesses
loop_end = 0;
while loop_end == 0
    
    %We display the number of guesses the user has taken
    fprintf('You have used %d guesses
', numguess)
    
    %We increment the number of guesses by 1
    numguess = numguess + 1;
    
    %We then prompt the user for their next guess
    fprintf('
Guess #%d', numguess)
    guess = input(': ', 's');
    
    %We use strcmp to see if the guess matches the correct word. A 1 will
    %be stored in correct_guess if they do, a 0 will be stored if they
    %don't
    correct_guess = strcmp(guess, correct_word);
    
    %We then use an if statement. If correct_word is 1, a message is
    %displayed, and the loop will end.
    if correct_guess == 1
        fprintf('Congratulations! That''s correct! You win!
')
        
        %we also change the loop_end variable to 1, so that the loop will
        %end
        loop_end = 1;
        
        %If correct_word is a 0, a message is displayed saying it is wrong
    else
        fprintf('I''m sorry, that''s incorrect.
')
    end
    
    %We now check to see if the user has guessed 10 times. If they have,
    %loop_end is changed to 1 and the loop will end.
    if numguess == 10
        loop_end = 1;
    end
end

As you can see, I use the variable loop_end to tell the loop when to end. I know there must be a way to simply put the conditions I need to end the loop in the while statement itself using the or function (||) but I couldn’t get it to work. It just seems like this won’t be considered to be the best way to do this.

I have never used MATLAB but a quick look at http://www.math.ufl.edu/help/matlab-tutorial/matlab-tutorial.html suggests something like:



while numguess < 10

  % Blah blah blah
  if correct_guess == 1
      % print result etc.
   
      % break out of loop
      break;   
   else
      % print 'try again' message
   end
end



Ahh, I did not know about the break command, I assumed that once you were in a loop, it could only be exited by the initial conditions.

Thanks.

Reading this again it seems likely that your earlier attempts may have failed because you were using ‘or’ rather than ‘and’. A ‘while <expression>’ loop will continue until <expression> evaluates to ‘false’. With a loop like ‘while <term1> | <term2>’ the compound expression ‘<term1> | <term2>’ evaluates to true as long as EITHER <term1> or <term2> are true, i.e. the loop will not terminate until you have had 10 goes AND you get the correct answer. If you code ‘while <term1> & <term2>’ the loop will continue only until one or other is false, which is what you want.

Note that you can use a FOR loop:



for guessCount = 0:9
  % do some stuff
  if correctGuess == 1
      % print result & done
      break
      end   
end