Nested if-else statements, MATLAB in Mathematics

Assignment Help:

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


Related Discussions:- Nested if-else statements

Morphological reconstruction, how can reconstruct just part of an image usi...

how can reconstruct just part of an image using imreconstruct?

Symbolic expression, Symbolic Expression The solve function solves an e...

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 spreadsheet files, Illustration of Spreadsheet Files: ...

Illustration of Spreadsheet Files: This reads the numbers in a double vector variable nums and the text in a cell array txt (the xlsread function forever returns the numbers f

Common form of the switch statement, Common form of the switch statement: ...

Common form of the switch statement: The common form of the switch statement is as shown below:   switch switch_expression   case caseexp1    action1   case cas

Logical vectors, Logical Vectors: The relational operators can also be...

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

Cholesky factorisation of packed storage matrix, If I have a vector represe...

If I have a vector representing the packed storage form of a symmetric matrix, how do I perform a cholesky factorisation on that?

Function fopen - file function, Function fopen - file function: The pe...

Function fopen - file function: The permission string in the call to the fopen function identifies that the file is opened for writing to it. Just as when reading from a file,

Animation, Animation: In this part we will observe a couple of ways to...

Animation: In this part we will observe a couple of ways to animate a plot. These are visuals, therefore the outcomes can't really be shown here; it is essential to type these

Illustration of advanced file input and output, Illustration of Advanced fi...

Illustration of Advanced file input and output: For illustration, to refer to the third number in the first element of the cell array: >> subjdata{1}(3) ans =

Plot functions, Plot Functions: Faraway, we have plotted to generate t...

Plot Functions: Faraway, we have plotted to generate two-dimensional plots and bar to generate bar charts. We have seen how to clear the Figure Window by using clf, and how to

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