Exit from several, nested while or for loops with one command

Hello Is there an easy way to quit nested while loops with a command?
For example:
while condition1 = true
...
while condition2 = true
...
while condition3 = true
...
if condition4 = true
leave all while loops and continue with 'command1';
end
end
end
end
command1;
I know that in nested loops, 'break' exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.

1 comentario

Jan
Jan el 14 de En. de 2013
Editada: Jan el 15 de En. de 2013
I've seen the "goto" tag! Evil.
Be sure to read the related: Loren about a vectorized GOTO in Matlab

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 14 de En. de 2013
Editada: Jan el 14 de En. de 2013
doRun = true;
while condition1 && doRun
...
while condition2 && doRun
...
while condition3 && doRun
...
if condition4 == true
leave all while loops and continue with 'command1';
doRun = false;
end
end
end
end
Command1;
Alternative:
% Call subfunction:
myLoops
command1;
...
function myLoops
while condition1 == true
...
while condition2 == true
...
while condition3 == true
...
if condition4 == true
return;
end
end
end
end

4 comentarios

Thanks a lot! In other programming languages there exists the command "goto". Why isn't there a similar command in Matlab? I can imagine a lot of cases, where it could be useful. Is there any reason for not adding "goto" commands into the Matlab commands library?
Yes, there are very good reasons not to implement a GOTO:
  1. The effort for debugging grows exponentially with the number of GOTOs.
  2. GOTOs invite programmers to write buggy code. E.g. it is a reliable strategy to close a file in the same function and same indentation level as it was opened. With an intermediate GOTO the idea of an underlying program stream gets meaningless and replaced by a bunch of spaghettis of different length.
  3. The code can be processed more efficiently, if the structure is clear and exactly defined. E.g. Matlab's JIT accelerator can increase the speed of loops substabtially. GOTOs makes it much harder until impossible to determine the structure and you e.g. jump out of loops or inside loops from everywhere.
This has been discussed repeatedly in the last 30 years, so you can ask your favorite search engine for further details.
When you really want to insert free jumps in a programming language, think in categories of the COMEFROM command, which reflects more accuratewhat happens in human brains than GOTO.
Very interesting! I never knew that the effort for debugging grows exponentially with the number of GOTOs. I cannot find any informations on that in the internet(?) You have any link?
On this site, it is said, that GOTO can be useful for quitting nested loops, but should be used always with care: http://www.dotnettoad.com/index.php?/archives/20-when-not-to-use-the-goto-keyword.html
Jan
Jan el 15 de En. de 2013
Editada: Jan el 15 de En. de 2013
Another rule is, that professionally written software (educated and experienced programmers, bug control in a team work, unit tests, bug tracking, version control, costs > 1$ per line) contains about 1 bug per 1000 lines of code. Fixing a bug in a certain line affects three other lines also, which partially require further fixes afterwards.
Debugging a code seriously demands for a coverage report. A bunch of GOTOs increase the effort to find a valid set of inputs, which is appropriate to run each part of the code. The software for controlling the Tornado battle plane has been debugged to 99% only, while the last 1% has been performed during low-level flights over inhabitted areas in the densly settled middle Europe.
In consequence "GOTO should be used with care" means, that avoiding it is a good programming practice. Or in other words: When you avoid GOTO (and the rest of the axis of evil: EVAL and GLOBAL), you will find another way to implement bugs.
My favorite link about GOTO is:
Searching this forum will be helpful also:

Iniciar sesión para comentar.

Más respuestas (1)

Apoorva Srivastava
Apoorva Srivastava el 14 de Jun. de 2019
Editada: Apoorva Srivastava el 14 de Jun. de 2019
If you want to stop the program completely but not close MATLAB, you can just use return. (Note: It can be used even without a function!)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 14 de En. de 2013

Editada:

el 14 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by