lecturenotes9_24

lecturenotes9_24 - terminating the program when the user...

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style 10/5/09 Error Checking with Loops Wednesday September 24, 2008
Background image of page 1

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

View Full DocumentRight Arrow Icon
10/5/09 Topic List 1. Customizing error messages 2. Loop constructs 3. Using loops to check user input
Background image of page 2
10/5/09 Error Messages Matlab has some specific error messages that you have all observed in labs and homework assignments We can also specify our own error messages displays an error message and returns control to the keyboard (exits the program)
Background image of page 3

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

View Full DocumentRight Arrow Icon
10/5/09 Implementation of Error Messages Example: Write a small program that asks the user for a favorite number between 1 and 10. Have the program return an error message if the user enters a number outside of the specified bounds.
Background image of page 4
10/5/09 break terminates the execution of a for or while loop. Statements in the loop that appear after the break statement are not executed. return causes a normal return to the invoking function or to the keyboard.
Background image of page 5

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

View Full DocumentRight Arrow Icon
10/5/09 Example 2 What if you wanted to implement the same example, but without
Background image of page 6
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
Background image of page 9

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

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

Unformatted text preview: terminating the program when the user inputs an invalid number? • For this, we need a “WHILE” loop 10/5/09 Loop constructs • There are two main classes of loop constructs: 1. Condition controlled 1. Count controlled 10/5/09 Condition Controlled (WHILE) Loop • This loop will continue to execute a block of statements until the condition is no longer “true” • Syntax: while (expression with logical result) statement 1; statement 2; etc. 10/5/09 Notes about the WHILE loop • Note that statements 1 and 2 will be continually executed until the condition evaluates to false. • One of the arguments in the condition MUST change within the loop, else we get a never-ending loop! 10/5/09 Example 2 (revisited) • Now, reconstruct the previous example so that the program keeps asking the user for the value until it IS between 1 and 10. • Try to solve the problem two different ways. (hint: one uses break and one does not)...
View Full Document

This note was uploaded on 02/25/2009 for the course BEE 1510 taught by Professor Staff during the Fall '05 term at Cornell.

Page1 / 10

lecturenotes9_24 - terminating the program when the user...

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