You are on page 1of 11

DE LA SALLE UNIVERSITY – DASMARIÑAS

Dasmariñas, Cavite
COLLEGE OF ENGINEERING, ARCHITECTURE AND TECHNOLOGY

Name : _________________________ Date Started : __________


Course-Year &Section : __________ Date Completed: __________
No. of Hours Required : __________ Date Due : __________
Rating : __________

Experiment No. 4
Loops and Logicals

OBJECTIVES

 To do basic calculations and use some basic functions. Learn about using logical constructions to
make decisions and take different actions based upon the decision; and
 To learn the use of a while loop and switch/case construction to make decisions.

BASIC CONCEPTS / MATLAB FEATURES

30
31
EQUIPMENT AND MATERIALS REQUIRED:

Computer with MATLAB Software

PROCEDURE:

(1) Using if/elseif/else to make decisions.


MATLAB uses the if/elseif/else construct to execute different sets of actions depending upon the
truth of a condition. The basic form of the construction is
if (condition_A)
% execute these commands when condition_A is true
action A1
action A2
...
elseif (condition_B)
% execute these commands when condition_B is true
action B1
action B2
...
elseif (condition_C)
% execute these commands when condition_C is true
action C1
action C2
...
...
else % default actions

32
% execute these commands if none of the above is true
action default1
action default2
...
End

Note that if, elseif, else, and end are all blue (both in the Command Window and the Editor).
This indicates that these words are reserved keywords in MATLAB that cannot be used as a
variable or function name. Their meanings are fixed and cannot be changed. Also note that the
actions associated with any of the keywords are indented under the keyword. The MATLAB
Editor will automatically indent for you. The purpose of indentation is to easily identify which
actions belong with which keyword and condition. Finally, the actions can be any legitimate
MATLAB command, including, but not limited to, assignment statements, function calls (such as
disp and input), another if/elseif/else construct, or a looping construct.

Two abbreviated forms of the general construct are frequently used. The first is the simple if
construct
if (condition)
% execute these commands when condition is true
action 1
action 2
...
end
and the second is the if/else construct
if (condition)
% execute these commands when condition is true
action 1
action 2
...
else % default actions
% execute these commands when condition is false
action default1
action default2
...
End

33
The if construct is frequently referred to as a one-sided decision - actions are only taken if the
condition is true. The if/else construct is frequently referred to as a two-sided decision - actions
are taken regardless of whether the condition is true. When the condition is true, the “true” actions
are executed. When the condition is false, the default actions are executed.
If you are working from a reasonably well-developed outline, such as above, coding a decision in
MATLAB is relatively straight-forward. If you are not working from such an outline, good luck!
For example, the algorithm outline in the function design can be (partially) encoded as
% convert heading to bearing
if ((0 <= heading) & (heading < 90) ) % first quadrant
face = 'north';
degrees = heading;
turn = 'east';
elseif ( ?? ) % second quadrant
...
elseif ( ?? ) % third quadrant
...
elseif ( ?? ) % fourth quadrant
...
else % default - heading not valid
...
end
(2) Using switch/case to make decisions.
MATLAB has an alternative method to the if/elseif/else structure to evaluate and execute
decisions, as shown here:
switch (expression_value)
case valueA % execute when expression_value == valueA
action A1
action A2
...
case valueB % execute when expression_value == valueB
action B1
action B2
...
...
otherwise % optional default if expression_value ≠ any value
action default1
action default2

34
...
end
If the same actions are to be taken for more than one value of expression_value, which is
equivalent to a logical or, i.e., A or B, the switch/case construct is slightly altered to
switch (expression_value)
case {vA1, vA2} % expression_value == vA1 or vA2
action A1
action A2
...
case {vB1, vB2} % expression_value == vB1 or vB2
action B1
action B2
...
...
otherwise % optional default if expression_value ≠ any value
action default1
action default2
...
end
In contrast with the if/elseif/else construct, which tests for the truth of a logical expression, the
switch/case construct simply compares the results of evaluating any legitimate MATLAB
expression that produces a value with a list of possible outcomes (cases). As we have seen, a
legitimate MATLAB expression can be simply a “hard-wired” value, a variable name (which is a
command to get the variable value), an arithmetic expression, or an algebraic expression that
includes variable names and functions. If a match is made between the resulting
expression_value and any of the case values, the actions associated with the matching case
are executed. If no matches are made, then no actions are taken unless the optional otherwise
clause is included.

35
EXERCISES:

1. Create a MATLAB Program that will accept a value of N and a pattern P. The Pattern must be the
following (assuming n = 5)
a. PATTERN 1: *****
****
***
**
*

b. PATTERN 2: *
**
***
****
*****

c. PATTERN 3: *
**
***
****
*****

2. Create a looping question at the end to determine if the user wants to try another pattern or not.

36
% Created by Sabayton, Khaye S.
% Finalized 04/02/23 % Pattern 5:
elseif(n==4 && p==2)
%execute these commands when condition is
disp ('MATLAB Program with star patterns') true
disp ('ECE21 ') disp (' *')
disp ('Khaye S. Sabayton ') disp (' * *')
disp (' ') disp (' * * *')
disp (' ') disp ('* * * *')
disp ('See the results below:') % Pattern 6:
n = input (' Please enter numbers 2 to 5 for elseif(n==4 && p==3)
height = '); % execute this command when condition is
p = input ('Please enter 1, 2, or 3 for your true
desired pattern = '); disp (' * ')
disp (' * * ')
disp (' * * * ')
% For Pattern 1: disp (' * * * * ')
if(n==5 && p==1)
%execute these commands when condition is % Pattern 7:
true elseif(n==3 && p==1)
disp ('* * * * *') % execute this command when condition is
disp ('* * * * ') true
disp ('* * * ') disp ('* * * ')
disp ('* * ') disp ('* * ')
disp ('* ') disp ('* ')
% Pattern 2: % Pattern 8:
elseif (n==5 && p==2) elseif(n==3 && p==2)
%execute these commands when condition is % execute this command when condition is
true true
disp (' *') disp (' *')
disp (' * *') disp (' * *')
disp (' * * *') disp (' * * *')
disp (' * * * *') % Pattern 9:
disp ('* * * * *') elseif(n==3 && p==3)
% Pattern 3: % execute this command when condition is
elseif(n==5 && p==3) true
%execute these commands when n==3 disp (' * ')
disp (' * ') disp (' * * ')
disp (' * * ') disp (' * * * ')
disp (' * * * ') % Pattern 10:
disp (' * * * * ') elseif(n==2 && p==1)
disp ('* * * * *') % execute this command when condition is
% Pattern 4: true
elseif(n==4 && p ==1) disp ('* * ')
%execute these commands when condition is disp ('* ')
true % Pattern 11:
disp ('* * * *') elseif(n==2 && p==2)
disp ('* * * ') % execute this command when condition is
disp ('* * ') true
disp ('* ') disp (' *')
disp (' * *')
37
% Pattern 12: disp ('* * ')
elseif(n==2 && p==3) disp ('* ')
% execute this command when condition % Pattern 5:
is true elseif(n==4 && p==2)
disp (' * ') %execute these commands when condition is
disp (' * * ') true
elseif(n>5 && p>3) disp (' *')
% execute this command when condition disp (' * *')
is true disp (' * * *')
disp ('Uh-oh! Please Try Again.') disp ('* * * *')
elseif(n<=5 && p>3) % Pattern 6:
% execute this command when condition elseif(n==4 && p==3)
is true % execute this command when condition is
disp ('Uh-oh! Please Try Again.') true
end disp (' * ')
Q = input('If you want to try again, plese disp (' * * ')
press 1. If you want to exit, please press disp (' * * * ')
2.: '); disp (' * * * * ')
while Q == 1
n = input (' Please enter numbers 2 to 5 % Pattern 7:
for height = '); elseif(n==3 && p==1)
p = input ('Please enter 1, 2, or 3 for % execute this command when condition is
your desired pattern = '); true
disp ('* * * ')
% For Pattern 1: disp ('* * ')
if(n==5 && p==1) disp ('* ')
%execute these commands when condition is % Pattern 8:
true elseif(n==3 && p==2)
disp ('* * * * *') % execute this command when condition is
disp ('* * * * ') true
disp ('* * * ') disp (' *')
disp ('* * ') disp (' * *')
disp ('* ') disp (' * * *')
% Pattern 2: % Pattern 9:
elseif (n==5 && p==2) elseif(n==3 && p==3)
%execute these commands when condition is % execute this command when condition is
true true
disp (' *') disp (' * ')
disp (' * *') disp (' * * ')
disp (' * * *') disp (' * * * ')
disp (' * * * *') % Pattern 10:
disp ('* * * * *') elseif(n==2 && p==1)
% Pattern 3: % execute this command when condition is
elseif(n==5 && p==3) true
%execute these commands when n==3 disp ('* * ')
disp (' * ') disp ('* ')
disp (' * * ')
disp (' * * * ')
OBSERVATIONS/RESULTS:
disp (' * * * * ')
disp ('* * * * *')
% Pattern 4:
elseif(n==4 && p ==1)
%execute these commands when condition is 38
true
disp ('* * * *')
disp ('* * * ')
disp ('* * ')
% Pattern 11:
elseif(n==2 && p==2)
% execute this command when condition is true
disp (' *')
disp (' * *')
% Pattern 12:
elseif(n==2 && p==3)
% execute this command when condition is true
disp (' * ')
disp (' * * ')
elseif(n>5 && p>3)
% execute this command when condition is true
disp ('Uh-oh! Please Try Again.')
elseif(n<=5 && p>3)
% execute this command when condition is true
disp ('Uh-oh! Please Try Again.')
end

Q = input('If you want to try again, plese press 1. If you want to exit,
please press 2.: ');
if Q == 2
break
end
end

disp ('Thank You and Goodbye')

39
OBSERVATIONS/RESULTS:

The laboratory activity has else/if statements to achieve the desired outcomes, which in
clude decisions about the nvalue, which determines height, and the type or number of patterns 
that must be input. It's common to refer to the if/else construct as a two-sided decision
because actions are executed whether the condition is true or not. The "true" actions are
carried out when the condition is met. The default actions are carried out when the condition is
false. Only when the condition is true will the statement's code or statement block be executed.
It is a keyword for conditional programming that is used to specify conditions to the Matlab
program. In Matlab activity it was designed with number of height from two to five.

CONCLUSION:

A set of operations known as "logicals" work on unprocessed and logical.  Typically, they
check whether there is a TRUE or FALSE link between two vectors. They can be used for less
complicated jobs like setting up conditions for loops, searching and screening data before
performing tests, etc. as well as more complex ones like simple calculations and comparisons.
Also, loops repeatedly run across a section of code based on specific criteria. 

RECOMMENDATION:

Without prior knowledge, it will be difficult for anyone. Moreover, make sure to finish
your research and setup the variables before beginning the else and if statements. In order to
avoid coding mistakes and confusion. The laboratory activity was multiple time testes using
codes and encountered error.

40

You might also like