"Return" and "continue" functions!

Hello MATLAB experts :)
I have not got the concept of "return" and "continue" functions in MATLAB. I went through the help but the examples were not so comprehensive for me. Please could you kindly explain them with a nice example per each?
Have a nice WE,
Mehdi

 採用された回答

Dr. Seis
Dr. Seis 2012 年 10 月 19 日
編集済み: Dr. Seis 2012 年 10 月 19 日

66 投票

Continue example:
function VALUE = somefunction
while SOME_CONDITION
DO_THIS;
if SOME_OTHER_CONDITION
continue;
end
DO_THAT
end
VALUE = SOMETHING;
If SOME_OTHER_CONDITION is true, then continue will essentially skip any remaining statements (i.e., DO_THIS will be executed, but DO_THAT will be skipped) in the loop and re-enter the loop provided SOME_CONDITION is still true. If SOME_OTHER_CONDITION is false, then continue will not be encountered and will execute both DO_THIS and DO_THAT for that loop iteration.
Return example:
function VALUE = somefunction
while SOME_CONDITION
DO_THIS;
if SOME_OTHER_CONDITION
VALUE = SOMETHING;
return;
end
DO_THAT
end
DO_SOMETHING_ELSE;
If SOME_OTHER_CONDITION is true, then return will not only skip any remaining statements (i.e., DO_THIS will be executed, but DO_THAT will be skipped) but it will also completely exit the loop. return will also exit the function (skipping DO_SOMETHING_ELSE) and return VALUE. If SOME_OTHER_CONDITION is false, then return will not be encountered and will execute both DO_THIS and DO_THAT for that loop iteration.

8 件のコメント

Sean de Wolski
Sean de Wolski 2012 年 10 月 19 日
Nice explanation.
shayan hajipour
shayan hajipour 2017 年 7 月 31 日
Nice answer, Thank you!
kamal wisal
kamal wisal 2017 年 11 月 20 日
very nice and precise example. thank you.
Abdelazzem Atyia
Abdelazzem Atyia 2020 年 3 月 18 日
great explanin thank you indeed
Caleb Pan
Caleb Pan 2020 年 4 月 22 日
nice indeed
Edward Saliba
Edward Saliba 2021 年 3 月 2 日
excellent answer
burak ergocmen
burak ergocmen 2022 年 3 月 4 日
very good
Erfanandoaut
Erfanandoaut 2022 年 7 月 27 日
Wonderful

サインインしてコメントする。

その他の回答 (1 件)

Danupon Subanapong
Danupon Subanapong 2018 年 11 月 16 日

0 投票

Very nice explanation, but I have a question. Please see below.
1) Using continue
function VALUE = somefunction
while SOME_CONDITION
DO_THIS;
if SOME_OTHER_CONDITION
continue;
end
DO_THAT
end
VALUE = SOMETHING;
2) No continue just if and ifelse
function VALUE = somefunction
while SOME_CONDITION
DO_THIS;
if ~SOME_OTHER_CONDITION
DO_THAT
else
end
end
VALUE = SOMETHING;
Are these two methods giving the same result?

2 件のコメント

Jingang Wang
Jingang Wang 2018 年 12 月 4 日
I think so.
darova
darova 2020 年 4 月 21 日
No
function VALUE = somefunction
while SOME_CONDITION
DO_THIS;
if SOME_OTHER_CONDITION
continue;
end
DO_THAT1
DO_THAT2
DO_THAT3
end
VALUE = SOMETHING;
Ii SOME_OTHER_CONDITION is true then continue skips iteration (all DO_THAT will not be executed)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

M G
2012 年 10 月 19 日

コメント済み:

2022 年 7 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by