Input in a while loop, MATLAB in Statistics

Input in a While Loop:

The script below repeats the procedure of prompting a user, the reading in a positive number, and echo-printing it, as long as the user properly enters positive numbers whenever prompted. As soon as the user types in a negative number, the program will print the OK and end.

1318_Input in a While Loop.png

Whenever the program is executed, the input/output mayt look like this:

>> whileposnum

Enter a positive number: 6

You entered a 6.

Enter a positive number: -2

OK!

 

Whenever the user enters a negative number at the first time, no values will be echo-printed:

>> whileposnum

Enter a positive number: -33

OK!

 

This describes a very important feature of the while loops: it is possible that the action will not be executed at all, if the value of the condition is false at the first time it is computed.

As we have seen before, the MATLAB will give an error message when a character is entered instead of a number.

 

>> whileposnum

Enter a positive number: a

??? Error using ==> input

Undefined function or variable 'a'.

Enter a positive number: -4

OK!

 

Though, if the character is really the name of variable, it use the value of that variable as the input. For illustration:

>> a = 5;

>> whileposnum

Enter a positive number: a

You entered a 5.

Enter a positive number: -4

OK!


Posted Date: 10/19/2012 6:08:36 AM | Location : United States







Related Discussions:- Input in a while loop, Assignment Help, Ask Question on Input in a while loop, Get Answer, Expert's Help, Input in a while loop Discussions

Write discussion on Input in a while loop
Your posts are moderated
Related Questions
bjjh

Illustration of Indexing into vectors of structures: For illustration, the function to print the information from packages has been modified therefore in addition to the vecto

Functions which complete a task without returning Values: Most of the functions do not compute values, but instead of accomplish a task like printing formatted output. As thes

Opening and Closing a File: The Files are opened with the fopen function. By the default, fopen function opens a file for reading. If the other mode is preferred, a permission

Reading from Files: There are many lower level functions which read from files. The function fscanf reads the formatted data into a matrix, by using conversion formats like %s

Combining Nested FOR Loops and the IF Statements: The statements inside a nested loop can be any of the valid statement, involving any selection statement. For e.g., there can

Logical errors: The Logical errors are more complicated to locate, as they do not result in any error message. The logical error is a mistake in reasoning by the programmer, b

Illustration of Customizing plots: Illustration, the bar and barh functions by the default place a width of 0.8 between bars. Whenever called as bar(x,y), the width of 0.8 is

Example of sorting strings: To sort a cell array of strings, also the sort function is used. The illustration is as follows, >> engcellnames = {'Chemical','Mechanical',.

Illustration of Nested loops: Running the script shows the output: >> printstars ***** ***** ***** The variable rows identifies the number of rows to print, and