invoking a matlab program without using "return" and "go to"

조회 수: 2 (최근 30일)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2021년 5월 25일
답변: Walter Roberson 2021년 5월 25일
Hi everyone
I need to stop a program before it reaches to the end of the script but it is not allowed to use retun and go to. I know that "error" command is available but it spits out "error in line ..." in command window and it is not allowed either. Sorry it seems weird but it is an assignmnet with restricting instructions.
Thanks for the time
  댓글 수: 1
Rik
Rik 2021년 5월 25일
Can you post the assignment? The requirements seem very strange, as Jan pointed out in his answer.

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2021년 5월 25일
Who forbids useful commands?!? Is it an anti-homework to teach bad programming styles?
function myTest
for unused = 1
disp('Hello');
break
disp('Hello again'); % Not reached
end
end
Alternatives to the break in a pseudo-loop:
exit (kills the complete Matlab sessions, but "stops the program"), quit (the same), try/catch/end (to let the code stop by error() without displaying a message. Or:
function myTest
s = input('Please type in the command: ', 's');
eval(s);
disp('Hello again'); % Not reached
end
So just type in 'return'... (evil)
  댓글 수: 1
Stephen23
Stephen23 2021년 5월 25일
Ha... or just type in "format this hard-drive, reboot" (very evil)

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2021년 5월 25일
Use break
t = 0;
for k = 1:2000
if isprime(k) & isprime(k-5)
break
end
t = t + 1;
end

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by