Chapter4-2f - WhileLoops while statement 1...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Iteration Control Structure While Loops
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
     Typical Code Structure while <condition> statement 1 statement 2 : : statement n end Note indentation for easier reading Means condition is  true Can any valid  MATLAB statements  including more  looping and/or  branching
Background image of page 2
The condition should eventually become false otherwise an infinite loop will result. Below are a few examples of an infinite loop. Note that the conditions given will never become false : A=1; while 0.1 disp(A) end      Un-Typical Code Structure <condition> is always true (0 is false; anything ~0 (not 0) is true
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
index = 0; while index <= 0 % end index = 0 while index >= 0 % Do some stuff % Do some more stuff end      Un-Typical Code Structure index goes  negative, never  becomes > 0 index remains = 0,  never becomes < 0 <condition> is  always true
Background image of page 4
Note that with a WHILE statement, the statements between the WHILE and END keywords must modify the condition so that it will become false some time in the future. fprintf( '\n***  Testing the WHILE loop structure.\n\n'); index=1; while index <= 4 % Could be index < 5 as well fprintf('Testing the WHILE loop. index = %g\n',index); index=index+1 % Modifies the condition end fprintf('\nEnd of WHILE loop. index=%g.\n\n', index);    A Typical Code Structure Note indentation for easier  reading Equivalent to a “for” loop from 1 to 4 (except index = 4 at end of loop) but clearly more awkward. Try Class Example 4_9
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
*** Testing the WHILE loop structure.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/13/2010 for the course EGR EGR115 taught by Professor Newman during the Spring '10 term at Embry-Riddle FL/AZ.

Page1 / 14

Chapter4-2f - WhileLoops while statement 1...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online