While loop not working

1 view (last 30 days)
NAMRATA SINGH
NAMRATA SINGH on 24 Nov 2016
Commented: Jan on 24 Nov 2016
Hello everyone,
I am trying to work on this part on it stops at second section with no error message.
while j <9
if j<1
i = 'E';
set(figure('units','normalized','outerposition',[0 0 1 1], 'color', [1 1 1]))
axis off
text(.45, .6, 'E', 'fontname', 'Times', 'fontsize', 200)
pause
LetterOne = input('What letter did you see on the screen?', 's')
LetterOne = upper(LetterOne)
if ~isempty(LetterOne)
LetterOne(LetterOne == ' ') = [];
disp('Press a key to continue')
else disp('Thank you participating in the vision test.')
disp('Your vision is 20/200');
return
end
clf, close
end
if j<2
i= 'F P';
set(figure('units','normalized','outerposition',[0 0 1 1], 'color', [1 1 1]))
axis off
text(.40, .6, 'F P', 'fontname', 'Times', 'fontsize', 100)
pause
LetterTwo = input('What letter did you see on the screen?', 's')
LetterTwo = upper(LetterTwo)
if ~isempty(LetterTwo)
LetterTwo(LetterTwo == ' ') = [];
disp('Press a key to continue')
else disp('Thank you participating in the vision test.')
disp('Your vision is 20/100')
return
end
clf, close
end
if j<3
i = 'T O Z';
set(figure('units','normalized','outerposition',[0 0 1 1], 'color', [1 1 1]))
axis off
text(.45, .6, 'T O Z', 'fontname', 'Times', 'fontsize', 70)
pause
LetterThree = input('What letter did you see on the screen?', 's')
LetterThree = upper(LetterThree)
if ~isempty(LetterThree)
LetterThree(LetterThree == ' ') = [];
disp('Press a key to continue')
else disp('Thank you participating in the vision test.')
disp('Your vision is 20/70');
return
end
end

Answers (1)

Daniel kiracofe
Daniel kiracofe on 24 Nov 2016
I see two issues with your code:
1) By virtue of the fact that you have "while j <9" at the top, this implies that you expect that j will be changing somewhere inside the loop. But, as far as I can tell, j never changes. Therefore your loop is likely to execute an infinite number of time, or zero, depending on the initial value of j. You probably want to have a "j=j+1" somewhere in your loop, or maybe use "for j=1:9" instead.
2) You said the code stops at the second part, so I'm guessing that j starts out < 9, and that you expected it to continue. You have a "return" in the middle of the loop. Guess what return does? It stops the execution of the code right then and returns to the calling subroutine. If you want the code to keep going, just delete the return statement.
  1 Comment
Jan
Jan on 24 Nov 2016
When "j" is not defined anywhere, it is the imaginary unit.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!