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
If I have a vector representing the packed storage form of a symmetric matrix, how do I perform a cholesky factorisation on that?

How do I write a product series running from i to n of x''s using a for loop?

Customizing Plots : There are numerous ways to customize figures in the Figure Window. On clicking the Plot Tools icon will bring up the Property Editor & Plot Browser, with ma

Function isreal - complex numbers: The function isreal returns 1 for logical true when there is no imaginary part of the argument, or 0 for false when the argument does have a

Script a MATLAB program which meets the following speci cations: The program expects an input of a two-variable real-valued continuous function f : R^2--> R The program is to cal

Symbolic Expression The solve function solves an equation and returns the solution(s) as symbolic expressions. The answer can be converted to numbers by using any numeric funct

Illustration of nested function: The output arguments are distinct from variables. The scope of an output argument is merely the nested function; and it cannot be used in the

Print an imaginary number: To print an imaginary number, the function disp will show both parts automatically: >> disp(z1)    4.0000 + 2.0000i The function fprint

Logical Vectors: The relational operators can also be used with the vectors and matrices. For illustration, let's say that there is a vector, and we want to compare each eleme

Example of Minimum and Maximum Value For matrices, the functions min and max operate column wise by the default: >> mat = randint(2,4,[1 20]) mat =    9   10   1