Nested if-else statements, MATLAB in Mathematics

Nested IF-ELSE statements:

The if-else statement is used to select between the two statements. In order to select from more than two statements, the if-else statements can also be nested, one inside of the other. For illustration, consider executing the following continuous mathematical function y = f(x):

y = 1 for x < -1

y = x2 for -1 ≤ x ≤ 2

y = 4 for x > 2

The value of y depends on the value of x that could be in one of three possible ranges. Selecting which range could be proficient with three separate if statements, which is as shown:

if x < -1

   y = 1;

end

if x > = -1 && x < = 2

   y = x^2;

end

if x > 2

   y = 4;

end

 

As the three possibilities are mutually exclusive, then the value of y can be determined by using three individual if statements. Though, this is not very efficient code: all the three Boolean expressions should be computed, regardless of the range in which the x falls. For illustration, if x is less than -1, the initial expression is true and 1 would be assigned to y. Though, the two expressions in the next two if statements are still computed. Rather than of writing it in such a way, the expressions can be nested so that the statement ends whenever an expression is found to be true:

if x < -1

   y = 1;

else

   % If we are here, x must be > = -1

   % Use an if-else statement to choose

   % between the two remaining ranges

   if x > = -1 && x < = 2

  y = x^2;

   else

  % No need to check

  % If we are here, x must be > 2

  y = 4;

   end

end

Posted Date: 10/19/2012 3:20:47 AM | Location : United States







Related Discussions:- Nested if-else statements, Assignment Help, Ask Question on Nested if-else statements, Get Answer, Expert's Help, Nested if-else statements Discussions

Write discussion on Nested if-else statements
Your posts are moderated
Related Questions
Refer the subset of a matrix: It is also possible to refer to the subset of a matrix. For illustration, this refers to the first & second rows, second & third columns: >> m

Writing variables to a File: The save command is used to write variables to a file, or to append the variables to a MAT-file. By the default, save function writes to a MAT-fil

Program of built-in factorial function: Calling this function yields similar result as the built-in factorial function: >> fact(5) ans =   120 >> factorial(

5 p2+8p+15, 3 p2-3p-18, 12 p2-p-30

Fliplr function: The fliplr function "flips" the matrix from left to right (in another words the left-most column, the first column, become the last column and so on), and the

Strcat function - Concatenation : The strcat function, though, will eliminate the trailing blanks from strings before concatenating. Note that in these illustrations, the trail

Differentiation: The derivative of a function y = f(x) can be written as follows or f  '(x) and is defined as the rate of change of the dependent variable y with respe

Illustration of Writing to files: Here is the other illustration in which a matrix is written to a file. At First, an arbitrary 2 × 4 matrix is generated, and then it is writt

Illustration of Standard Deviation The less spread out the numbers are, therefore smaller the standard deviation will be, as it is a way of determining the spread of the data.

fprintf function - File function: The fprintf function really returns the number of bytes which was written to the file, therefore if you do not want to see that number, suppr