Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Laboratory Exercise 4

Statements

I. Introduction

Statements as defined by [6] can be a simple constant expression or it can be as


complicated as a list of conditional statements and nested loops.

The if, while and other control statements in Octave control the flow of execution
in the program. Its basic structure starts with a special keyword such as while or if.
This is to set them aside from the expressions that are considered simple. It is then
ended with a corresponding end statement, which marks its end. For example, the
endif is the keyword for the end of an if statement. The body of the control statement
is defined by the list of statements contained between the special keyword and the
corresponding end statement.

In this activity, you will learn the different statements which will be useful in automating
your code or script. This will result to a shorter script which is always desirable to have
the shortest code to save computing time and memory. But, be careful in using these
statements since improper script may result to an infinite loop which will cause your
system to solve forever.

Octave’s decision-making statement is the if statement. The if statement has


three basic forms. The simplest form is:

If (condition)
then-body
endif

In this form, control is coming from the condition expression. The execution of the
then-body will happen if the condition is true. This condition may be a Boolean
algebra. It can be true (non-zero value) and false if its value is zero. It is also possible
that this condition is in a form of vector or matrix. In this case the value of
the expression can only be true if it is non-empty and all of the elements are non-
zero. Otherwise, it will be considered as false.

The second form is:

If (condition)
then-body
else
else-body
endif

In this form, the then-body will only be executed if the condition is true. If the
condition is false, the else-body will be executed. The third form is the most
general form and it is in the form as follows:

If (condition)
then-body
elseif (condition)
elseif-body

else
else-body
endif
P a g e 28 | 70
Just like the forms earlier, the condition will still dictate whether the body will be
executed or not. The number of elseif clauses may be used. Each of these
conditions will be tested in turns. When it is true, it will execute the corresponding body.
If none of the conditions is true, then the else-body will be executed. It should be
noted that only one else clause should be used and it must be written last.

It is desirable to write our codes using indention, as shown above, for you to identify
which part of the statement is missing. It is easy to miss some parts of statements
especially if you have a complicated nested loop.

The switch statement is an improvement of the if statement. This is the original if


form which can be converted to a switch statement.

if (A == 1)
do_one_task ();
elseif (A == 2)
do_another_task();
else
do_a_completely_different_task ();
endif

Using the switch statement, it can be written as:

switch expression
case label
command_list
case label
command_list

otherwise
command_list
endswitch

A label is characterized as any expression. Having duplicate label values will create
problem as only the first-match corresponding command_list will be executed.

The simplest looping statement in GNU Octave is the while statement. But what is
looping? Looping means that a certain part of the program is executed twice or more
in succession. In while statement, the body will be executed repeatedly as long as the
condition is true. It can be written as:

while(condition)
body
endwhile

Firstly, the while loop tests the condition, if it is true, then the body will be
executed. After the body has been executed, the condition is again tested. In case
the condition is still true, the body will be executed again. On the other hand, if the
condition is false in the first test, the body will not be executed at all.

The do-until statement is somewhat similar to the while statement. However, here,
the body is executed until the statement is true and the condition is at the end. It

P a g e 29 | 70
means that the body is executed at least once before the condition is tested. The
format is as follows:

do
body
until (condition)

Sometimes counting the iterations in the loops may be tedious. Use the for statement
to make this easy. Its general form is

for variable=expression
body
endfor

Tip: Its easy to get lost in making statements, especially


loops. To avoid the GNU Octave to run without end
(continuous looping), press Ctrl+C to stop the script from
running.

II. Objectives
At the end of the activity, the students are expected to:
1. Acquire a vivid idea on the algorithms for the conditional and looping statements;
2. Recognize the importance of iterative solutions in power systems;
3. Demonstrate basic operations with conditional and looping statements in GNU
Octave.

III. Materials/Equipment
Computer with a full version of GNU Octave installed

IV. Procedure

TIME TO ACT IF Statement

1. Start the GNU Octave as instructed at the start of Laboratory Exercise 1 Part IV.
2. In the editor window, type the following:

clc;clear;

a=5

if (rem(a,2)==0)
fprintf("a is an even number\n");
else
fprintf("a is an odd number\n");
endif

3. Save your file and execute it.


4. Write the results in Part V.1.

TIME TO ACT SWITCH Statement


1. In the editor window, type the following:
clc;clear;

P a g e 30 | 70
a=7
switch a
case (rem(a,2)==0)
fprintf("a is an even number\n");
otherwise
fprintf("a is an odd number\n");
endswitch

2. Save your file and execute it.


3. Write the results in Part V.2.

In the previous examples, you used the function fprintf. This function returns the script
in between the “ “ in its argument. It is one way that the GNU Octave returns an output
through the Command window. The fprintf (format, data) function requires the following
formatting to display certain values:

%d integer
%f floating point format
%e exponential format
%g either floating point or exponential format, whichever is shorter
\n new line character
\t tab character

For example, you can write fprintf(‘the number is %d’,3) which will result to the
number is 3. You can also write fprintf(‘The circumference of the circle
with radius %d is %f’, 2, 2*pi*2), it will result to The circumference of
the circle with radius 2 is 12.566371.

However, do not confuse the use of % in the script outside the ‘ ‘ marks since this will
create a comment which the program will ignore in execution. This can be seen by being
written with color green in the editor window.

You can also use disp(array) function such as writing disp(‘Hello’) to output
Hello or disp(5) to output 5. The ‘ ‘ marks dictates that it will be read as a string and not
a numeric value. You can also concatenate series of strings like disp([‘Happy’
‘Birthday’]) to output Happy Birthday. You can also convert numeric value to string
using num2str() function or int2str() function. For example, if you write
d=[num2str(1) ’-May-‘ num2str(2022)]; disp(d) it will return 1-May-2022.

TIME TO ACT WHILE Statement

1. Write the following in the editor window:


%this is called a nested loop
clc; clear;
A=ones(6,6);
x=1;
while x<=6
y=1;
while y<7
if y~=x
A(x,y)=A(x,y)+x;
endif
if y==x
A(x,y)=y*2;
endif
P a g e 31 | 70
y=y+1;
endwhile
x=x+1;
endwhile
disp (A)

4. Save your file and execute it.


5. Write the results in Part V.3.

TIME TO ACT DO-UNTIL Statement

1. Write the following in the editor window:

%this is called a nested loop


clc; clear;
A=ones(6,6);
x=1;
do
y=1;
do
if y~=x
A(x,y)=A(x,y)+x;
endif
if y==x
A(x,y)=y*2;
endif
y=y+1;
until y>6
x=x+1;
until x>=7
disp (A)

2. Save your file and execute it.


3. Write the results in Part V.4.

P a g e 32 | 70
V. Observation Matrix

Instructions: Put your observations and calculated values in the space provided
1.
IF Statement

2.
SWITCH Statement

3.
WHILE Statement

4.
DO-UNTIL Statement

P a g e 33 | 70
VI. Guide Questions
In this part, please provide your answers to the guide questions in the space provided.

1. In your own, using the third form of if statement, create a code that differentiates
an even number, odd number that is divisible by three and an odd number. Write the
code space provided.

2. Differentiate IF statement from SWITCH statement

3. In the WHILE statement example, how do you explain the results based on the
given code?

3. What is the difference of the While and Do-Until examples? Why did we do
these changes to have the same output?

P a g e 34 | 70
4. Using For statement, write the code for the same output of while and do-until
examples.

P a g e 35 | 70

You might also like