Docsity

Understanding the if-elseif-else Statement in Matlab for Temperature Conditions, Summaries of Programming Languages

Toggle sidebar
bg1
CS99 Lab 2 September 9, 2004
The if statement
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 (<condition>)
<statement>
. . .
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-else statement
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 (<condition>)
<statement>
. . .
else
<alternative statement>
. . .
end
The above can be interpreted in the following manner: if <condition >holds true, execute the set of state-
ments starting with <statement>; otherwise, execute the set of statements starting with <alternative
statement>. As an example, consider a modified version of our temperature program, as shown below:
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.
1
pf2

Partial preview of the text

Download Understanding the if-elseif-else Statement in Matlab for Temperature Conditions and more Summaries Programming Languages in PDF only on Docsity!

CS99 Lab 2 September 9, 2004

The if statement

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-else statement

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 ... end

The above can be interpreted in the following manner: if holds true, execute the set of state- ments starting with ; otherwise, execute the set of statements starting with . As an example, consider a modified version of our temperature program, as shown below:

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-elseif-else statement

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 () ... elseif... ... else ... end

The above can be interpreted as follows: if is true, execute the set of statements starting with ; otherwise, if is true, execute the set of statements starting with ; otherwise, etc.; otherwise, if all the above conditions fail, execute the set of statements starting with . Note that during execution, for a given if-elseif-else statement, your program will keep checking conditions until it finds the first one that holds true, at which time it will execute the corresponding statement, and skip over any remaining elseif clauses. If, however, none of the conditions match, the program will execute the statements specified in the else clause. Note also that you don’t have to specify an else clause – that is, you may just have an if-elseif statement. In that case, if none of the conditions hold true, your program will simply take no action.

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.