Lecture8 - EGR 106 Lecture 8 Loops, Break, Continue...

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

View Full Document Right Arrow Icon
EGR 106 Lecture 8 Loops, Break, Continue Commands “for-end” loops “while-end” loops Nested loops “break” and “continue” commands Textbook: 6.4 – 6.6
Background image of page 1

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

View Full DocumentRight Arrow Icon
Loops Commands to repeatedly execute certain blocks of code Two common types in MATLAB: for while
Background image of page 2
The “for-end” Loop
Background image of page 3

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

View Full DocumentRight Arrow Icon
Example – Sum the series + + + + + = ! 4 1 ! 3 1 ! 2 1 ! 1 1 1 e sum=1; for n=1:10 sum=sum+(1/factorial(n)); fprintf('\nsum = %5.4f',sum) end sum = 2.0000 sum = 2.5000 sum = 2.6667 sum = 2.7083 sum = 2.7167 sum = 2.7181 sum = 2.7183 sum = 2.7183 sum = 2.7183 sum = 2.7183 Important – Must First Initialize Value
Background image of page 4
Example – calculating interest for 10 years: Note the use of num2str
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Example – implement a count down timer (in seconds):
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
The “while-end” Loop while conditional expression . . . . . . . . . end Group of MATLAB commands Conditional expression must include at least one variable which is assigned and changed as the Group of MATLAB commands are executed - code is started for the case with conditional statement true - group of MATLAB commands are executed and conditional statement is rechecked
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/03/2011 for the course EGR 106 taught by Professor Taggart during the Spring '09 term at Rhode Island.

Page1 / 25

Lecture8 - EGR 106 Lecture 8 Loops, Break, Continue...

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

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