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
Debugging Techniques: Any error in the computer program is called a bug. This word is thought to date back to the 1940s, whenever a problem with an early computer was found to

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

Data structure layers: There are many layers in this variable. For illustration,  cyls is the whole data structure, that is a vector of structs  cyls(1) is a separate

Execution of persistent variables: The functions can also be called from the script or from the Command Window, as shown here. For illustration, the functions are called first

Roots function - Polynomials: The roots function in MATLAB is used to find the roots of an equation represented by a polynomial. For illustration, for the mathematical functio

function sortrows - sorting strings: The function sortrows sorts each and every row as a block, or group, and it also will work on numbers. Here in this illustration the rows

Nested for Loops: The action of a loop can be any of the valid statement(s). Whenever the action of a loop is the other loop, this is known as nested loop. As an illustrati

Core Objects: The Core Objects in MATLAB are the very fundamental graphics primitives. The description can be found under the MATLAB Help: Under the Contents tab, click the Ha

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

Advanced File Input and Output: In that section, we saw how to read the values entered by user using the input as well as the output functions disp and fprintf, that shows inf