MATLAB Answers

0

Continue a loop when entered an option

Asked by Victor Boström on 10 Apr 2019
Latest activity Answered by Guillaume
on 10 Apr 2019
In my code I want to begin the loop again when I enter 1 and quit the program when entered 0.
clc
clear
syms result guess
corrects = 0;
n = 10;
for k = 1:n
x = randi([3 10],1,1);
y = randi([3 10],1,1);
result = x * y;
guess = input(sprintf('What is %d * %d?: ', x,y));
if (result == guess)
disp('Correct!')
corrects = corrects + 1;
else
fprintf('Wrong! The correct answer is %d\n', result);
end
end
fprintf('You were right %d out of 10!\n', corrects);
option = input('Do you want to do a new round (1 = yes, 0 = no): ');
if option == '1'
%start the loop again otherwise quit
end

  1 Comment

Adam
on 10 Apr 2019
doc break;
doc continue;

Sign in to comment.

Tags

1 Answer

Answer by Guillaume
on 10 Apr 2019

Just wrap your code in a while loop:
option = 1;
while option == 1
%... your code here
option = input('Do you want to do a new round (1 = yes, 0 = no): ');
end
Any reason you're using the symbolic toolbox? As far as I can tell it's completely unneeded.

  0 Comments

Sign in to comment.