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
Applications of Customized Strings: Labels, Prompts, Arguments to Functions: The one of very useful application of this is to involve numbers in strings that are used to plot

Writing and Reading Spreadsheet Files: The MATLAB functions xlswrite & xlsread will write to and read from the spreadsheet files which have the extension .xls. For illustratio

Writing to Files: There are many lower level functions which can write to files. We will focus on the fprintf function that can be used to write a file and also to append to a

Illustration of Variance For illustration, for the vector [4, 6, 1, 5], there are n = 4 values therefore n - 1 is 3. The mean of this data set is also 4. The variance will be

Built-in function for Differentiation: The MATLAB has a built-in function, diff that returns the differences between consecutive elements in a vector. For illustration, >>

Recursive Functions: The Recursion occurs whenever something is defined in terms of itself. In the programming, a recursive function is a function which calls itself. The Recu

strvcat function - concatenation: The function strvcat will concatenate it vertically, that means that it will generate a column vector of the strings.   >> strvcat(fir

Polar Form: Any complex number z = a + bi can be thought of as a point (a,b) or vector in the complex plane in which the horizontal axis is the real part of z, and the vertica

sprintf function: The sprintf function works precisely like the fprintf function, but rather than printing it generates a string. Here are some illustrations in which the outp

Error-checking for integers: illustrations of running this script as shown below: >> readoneint Enter an integer: 9.5 Invalid! Enter an integer: 3.6 Invalid