If statement - matlab programming, MATLAB in Statistics

Assignment Help:

The IF Statement:

The if statement selects whether or not the other statement, or group of statements, is executed. The common form of the if statement is as shown below:

if condition

   action

end

The condition is a relational expression which is theoretically, or logically, either true or false. Action is a statement, or a group of statements, which will be executed when the condition is true. Whenever the if statement is executed, the condition is initially computed. If the value of the condition is theoretically true, the action will be executed, and if not, then the action will not be executed. The action can be several number of statements awaiting the reserved word end; the action is obviously bracketed by the reserved words if and end. (Note: This is dissimilar from the end which is used as an index into the vector or matrix.)

For illustration, the if statement below confirms to see whether the value of a variable is negative. When it is, the value changed to a positive number by using the absolute value function; or else nothing is changed.

if num < 0

num = abs(num)

end

When statements can be entered in the Command Window, though they usually make more sense in the scripts or functions. In the Command Window, if the line would be entered, then press the Enter key, then action, then Enter key, and finally end and Enter; the outcome will instantly follow. For illustration, the earlier if statement is shown twice here. Note that the output from the assignment is not suppressed; therefore the result of the action will be shown if the action is executed. At first time the value of the variable is negative therefore the action is executed and the variable is modified, but in the second situation the variable is positive therefore the action is skipped.

>> num = -4;

>> if num < 0

num = abs(num)

end

num =

4

>> num = 5;

>> if num < 0

num = abs(num)

end

>> 

This might be used, for illustration, to make sure that the square root function is not used on a negative number. The script below prompts the user for a number, and prints the square root. When the user enters a negative number, then the if statement changes it to positive before taking the square root.

1428_if statement.png


Related Discussions:- If statement - matlab programming

Example of core objects, Example of Core objects: The one core graphic...

Example of Core objects: The one core graphics object is a line that is also what the plot function produces. Here is an illustration of generating a line object, altering som

Example of nested functions, Example of Nested functions: For illustra...

Example of Nested functions: For illustration, the function below computes and returns the volume of a cube. Three arguments are then passed to it, for the width and length of

Steps for input output functions - lower level file, Steps for input output...

Steps for input output functions - Lower level file: The steps involved are as shown below:  Open the file.  Read the file, write to the file, or append to the file.

Function stubs, Function Stubs: The other common debugging method, whi...

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

Operation on file, Operation on file: We concentrate first on the fget...

Operation on file: We concentrate first on the fgetl function that reads strings from the file one line at a time. The fgetl functions afford more control over how the data is

Illustration of input in a for loop, Illustration of Input in a for loop: ...

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

Logical errors, Logical errors: The Logical errors are more complicate...

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 binary search, Illustration of Binary search: An illus...

Illustration of Binary search: An illustration to search for the key of 91 in the vector is as shown below: The table below shows what will happen in each iteration of

Sorting strings, Sorting Strings: For the matrix of strings, the sort ...

Sorting Strings: For the matrix of strings, the sort function works exactly as formerly for numbers. For illustration, >> words = char('Hi', 'Hello', 'Howdy', 'Goodbye', '

Curve fitting, Curve Fitting: The data is fundamentally either discret...

Curve Fitting: The data is fundamentally either discrete or continuous. In most of the applications, data is sampled, for illustration, The temperature recorded every ho

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd