Tutorials > Programming > Task 2–2: Writing If-Else If Statements
Task 2–2: Writing If-Else If Statements
Use elseif or else statements to evaluate an expression based on whether a particular if statement evaluates as true or false.
1. To modify the second if statement in the function created in Task 2–1 to else if, select the second if programming operator and on the Math tab, in the Operators and Symbols group, click Programming, and then click else if.
 The else if operator is only available directly after an if or an else if statement.
2. To add an else statement, covering all other options, position the cursor as shown below.
3. To add an empty else statement, on the Math tab, in the Operators and Symbols group, click Programming, and then click else.
4. Type "RAINBOW".
5. Call reverse to get the value of 5.
Practice
1. Before you move to the next exercise, write a function f that receives a parameter x and performs the following tasks, using also if:
If x is a string, it assigns 1 to local variable y. Use the IsString function.
If the length of x is greater than 5, it assigns 2 to y. Use the strlen function.
Otherwise, it assigns 3 to y.
 You can insert an also if statement only after an if statement or an also if statement. When the if statement is true, all also if statements are evaluated.To add an also if statement, position the cursor at the end of the last line of the if block (as shown below) and on the Math tab, in the Operators and Symbols group, click Programming. The programming operators list opens. Click also if.
2. Define the following function in your worksheet:
Write a function h that receives a parameter x and behaves as described below.
Function h is a program containing a piecewise function.
If the value of x is from -1 to 1, h(x) value is f(x), defined earlier in your worksheet. Otherwise, the value of h(x) is f(x) multiplied by -1.
Add an XY plot to visualize the function.