02.02.2013 Views

Scilab for Real Dummies

Scilab for Real Dummies

Scilab for Real Dummies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Programming pitfalls<br />

(4/4): the endless loop<br />

I have several times mentioned<br />

the risk of creating an endless<br />

loop, so let’s look at this little<br />

beast<br />

When you execute the script you<br />

have to crash the program to stop<br />

it. The easiest way is to press the<br />

Close button on the Console and<br />

then reload <strong>Scilab</strong><br />

Why does the loop not end?<br />

Because we die from old age<br />

be<strong>for</strong>e the variable n by chance<br />

gets exactly the value 0.5<br />

// endless_loop.sce<br />

// Demonstrates an endless loop. /<br />

// Execution ends only by crashing /<br />

// the program (click on the Close /<br />

// button (X) on the Console) /<br />

n = .1;<br />

dt = getdate();<br />

rand('seed',1000*dt(9) + dt(10));<br />

while n ~=0.5;<br />

n = rand(0,'normal');<br />

end;<br />

disp(n)<br />

Have you <strong>for</strong>gotten<br />

about seeding rand<br />

functions? If so, go back<br />

to Ex 1-3 (lotto draw)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!