

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CS99 Lab 2 September 9, 2004
Often, we want our program to execute a certain set of statements only if some specific condition is met. For instance, me might want our program to output the word “cold” only if the temperature is below 32 degrees Fahrenheit. For that purpose, programming languages utilize the if statement. In Matlab, the if statement has the following syntax:
if (
... end
For example, the following Matlab program will output “cold” only if the user enters a temperature less than 32 degrees:
t = input(’Enter Temperature: ’); if (t < 32) disp(’Cold’); end
Exercise 2.1: Start up Matlab. Create a new M-file, and type in the program above. Save your M-file as cold.m. Execute the M-file by typing cold in the command window. Repeat the execution several times, entering different temperature values each time.
The if statement allows us to specify a set of actions to take if a certain condition holds true. In many cases, however, we would also like to specify an alternative set of actions our program should take if the said condition fails to hold. For instance, in our temperature example, me might still want our program to output “cold” if the temperature is below 32 degrees, but we would want it to output “not so cold” if the temperature is 32 degrees or higher. To accomplish this, we can add an else clause to our if statement. The if-else statement has the following general form:
if (
... else
The above can be interpreted in the following manner: if
t = input(’Enter Temperature: ’); if (t < 32) disp(’Cold’); else disp(’Not so cold’); end
Exercise 2.2: Save the above program into your cold.m M-file, and repeat exercise 2.1.
CS99 Lab 2 September 9, 2004
The if-else construct above only allows us to specify either/or scenarios. In practice, however, there are often situations where there are multiple possibilities for our program to choose from. For instance, in the case of our temperature program, we might wish to recognize more temperature ranges than just “cold” and “not so cold”. We might want our program to output “chilly” if the temperature is at least 32 and at most 50 degrees, “comfy” if the temperature is above 50 and at most 72 degrees, and “warm” if the temperature exceeds 72 degrees. The best way to accomplish this is to add an elseif clause to our if-else statement. The if-elseif-else statement has the following general form:
if (
... elseif (
The above can be interpreted as follows: if
Below, we give a modified version of our temperature program, which acts as follows:
If the temperature is below 32 degrees, output “cold” Otherwise, if it is at most 50 degrees, output “chilly” Otherwise, if it is at most 72 degrees, output “comfy” Otherwise, output “warm”
t = input(’Enter Temperature: ’); if (t < 32) disp(’Cold’); elseif (t <= 50) disp(’Chilly’); elseif (t <= 72) disp(’Comfy’); else disp(’Warm’); end
Exercise 2.2: Save the above program into your cold.m M-file, and repeat exercise 2.1. Try multiple different values of t. Note, for instance, that for t = 48, your program will always print “Chilly”, even though both conditions t <= 50 and t <= 72 are true. This is because the condition t <= 50 appears first in the if-elseif statement, and thus preempts the condition t <= 72. Thus, our program will only print “Comfy” if 50 < t ≤ 72, which is exactly what we want.