Loops/Nested: Difference between revisions

(add E example)
(→‎{{header|Perl}}: ++ octave)
Line 231:
until.20
</lang>
 
=={{header|Octave}}==
Octave has no way of exiting nested loop; so we need a control variable, or we can use the trick of embedding the loops into a function and use the <tt>return</tt> statement. (The search for "exactly 20" is changed into a search for "almost 20")
 
<lang octave>function search_almost_twenty()
% create a 100x100 matrix...
m = unifrnd(0,20, 100,100);
for i = 1:100
for j = 1:100
disp( m(i,j) )
if ( abs(m(i,j) - 20) < 1e-2 )
return
endif
endfor
endfor
endfunction
search_almost_twenty()
 
% avoiding function, we need a control variable.
m = unifrnd(0,20, 100,100);
innerloopbreak = false;
for i = 1:100
for j = 1:100
disp( m(i,j) )
if ( abs(m(i,j) - 20) < 1e-2 )
innerloopbreak = true;
break;
endif
endfor
if ( innerloopbreak )
break;
endif
endfor</lang>
 
=={{header|Perl}}==