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
Sequential Search: A sequential search is completed by looping through the vector element-by-element starting from the beginning, looking for the key. Usually the index of the

WHILE Loops: The while statement is used as a conditional loop in MATLAB; it is used to repeat an action whenever ahead of the time it is not known how many times the action w

Function Stubs: The other common debugging method, which is used when there is a script main program which calls many functions, is to use the function stubs. A function stub

Nested Structures: The nested structure is a structure in which at least one of the members is itself a structure. For illustration, a structure for the line segment may co

Example of Vectorizing: Likewise, for an operation on a matrix, a nested loop would be needed; for illustration, supposing a matrix variable mat: [r c] = size(mat); for

Areacirc function: The areacirc function can be called from the Command Window as shown here, or from a script. Here is a script which will prompt the user for the radius of o

Example of Median For the vector [1 4 5 9 12 33], the median is the average of the 5 & 9 in the middle: >> median([1 4 5 9 12 33]) ans =     7

Function polyval - interpolation: The function polyval can then be used to compute the polynomial at particular values. For illustration, we could compute at every value in th

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

Illustration of Input in a for loop: In this illustration, the loop variable iv iterates through the values 1 through 3, therefore the action is repeated three times. The acti