3

Statements

In the previous chapter, we learnt about the data types, expressions and operators supported by numEclipse. In this chapter, we will focus on statements including assignment statement, loops and conditional statements.

3.1 Assignment Statement

The assignment statement is the most simple statement in a programming language. In numEclipse an assignment could take different forms depending on the type of variables involved. In its simple form, it looks like the following expression.

< var_name > = < expression >

“=” is the assignment operator. This statement evaluates the expression and assigns the resulting value to the variable named on the left hand side. The left hand side or lvalue must be a valid identifier name. Identifier naming must start with a letter and it could be followed by zero or more letters, numbers or underscores in any combination. There is no need to declare a variable before assigning a value. numEclipse automatically determines the data type from the right hand side expression (or rvalue). The type of a variable could change as we assign a new value of different type to an existing variable. In the following, we show a few examples of assignments.

Listing 3.1

> > X = 1.0

X =

1.0000

> > Y = [1 2 3; 4 5 6]

Y =

1.0000 2.0000 3.0000

4.0000 5.0000 6.0000

> > S = ‘ Hello World’

S =

Hello World

> > B = 1 < 2

B =

1.0000

If a simple assignment is ended with a semi-colon then the interpreter will not report the value of the right hand side expression on the screen. A matrix assignment involves assigning an element of an existing matrix to a value. In case of a column or row vector, the assignment will be as follows.

< var_name > (< expression >) = < expression >

The expression within parentheses on the left hand side is an index value so it should evaluate to an integer value. In the case of an out of bound index value, the variable will be resized to accommodate the index value. The index value starts from “1”. In the case of a two dimensional matrix, the assignment will look like the following.

< var_name > (< expression >, < expression >) = < expression >

The first index in the parentheses determines the row number and the second index determines the column number of the matrix element. Here is an example for matrix element assignment.

Listing 3.2

> > A = rand(3)

A =

0.8467 0.0952 0.2836

0.9565 0.3782 0.5011

0.7280 0.2052 0.6668

> > A(2,3) = 1.5

A =

0.8467 0.0952 0.2836

0.9565 0.3782 1.5000

0.7280 0.2052 0.6668

numEclipse functions could return one or more variables so the syntax for assigning multiple return values is shown in the following.

[< var_1 > < var_2 > … < var_n >] = < expression >

The expression on the right-hand side will always be a function call. In the following, we provide an example of multiple returned variables.

Listing 3.3

> > x = min([2 1 3]])

x =

1.0000

> > [x idx] = min ([ 2 1 3]])

x =

1.0000

idx =

2.0000

In the first assignment example above, function min returns only the minimum value of the matrix element. In the second example, it returns the minimum value as well as the index of the minimum value. Not only a numEclipse function can return multiple values, it can also determine from the function call how many variables need to be returned on a particular call.

3.2 Loop Statements

Loop statements allow a program to run a block of statements a fixed number of times. numEclipse supports two loop statements, for loop and while loop.

3.2.1 “for” Statement

“for loop” is a compound statement. It is a repetitive statement since it allows a group of statements to be executed a number of times. The structure of this statement type is as follows.

image

A “for” statement should always be closed with either “end” or “endfor” keyword. The “identifier” works like a loop counter. The range expression evaluates to vector which determines the values taken by the identifier. Here is an example of a “for” loop statement.

Listing 3.4

> > x = 0;

> > for i = 1: 10

x = x + 1;

end;

> > x

x =

10

In the above example variable x is assigned a value “0”. The “for” loop is repeated 10 times and at each iteration x is incremented by 1.

3.2.2 “while” Statement

“while loop” is also a compound statement and it is repetitive in nature. The difference between a “for” and “while” loop is that the for loop executes statements for a fixed number of time whereas the while loop executes all grouped statements as long as a condition is satisfied. The structure of a while statement is as follows.

image

A “while” must be closed with either an “end” or “endwhile” keyword. In this statement the conditional expression is evaluated on the beginning of each iteration, if the conditional expression evaluates to true then the enclosed statements are executed otherwise the loop ends. Here is an example of a while loop.

Listing 3.5

> > y = [0 0 0];

> > i = 1;

> > x = 3;

> > while x > 0

y (i) = x;

i = i + 1;

x = x – 1;

end;

> > x

x =

0.0000

> > y

y =

3.0000 2.0000 1.0000

In the above example, three statements are executed as long as the condition x > 0 is satisfied.

3.3 Conditional Statements

Conditional statements execute when a condition is satisfied. numEclipse supports two conditional statements, “if” and “switch” statements.

3.3.1 “if” Statement

An “if” Statement is used to implement decisions in a program. It consists of one or more conditional expressions and corresponding block of statements. If a conditional expression evaluates to true then the corresponding block of statements is executed otherwise the next conditional expression is evaluated, if present.

image

In the above structure “if”, “elseif”, “else”, “end” and “endif” are the keywords. There could be zero or more “elseif” sections in an “if” statement. There could be zero or one “else” section in an “if” statement. Blocks of statements associated with “else” are only executed when none of the other conditions are satisfied.

An “if” statement could be nested within another “if” statement. It could increase the complexity but sometimes it is desirable. Remember that an inner “if” statement is evaluated before an outer statement.

Listing 3.6

x = input (‘ Enter a number’);

if (x > 0)

sign = 1;

else if (x > 0)

sign = − 1;

else

sign = 0;

In the above program, the user is asked to enter a number and it is assigned to variable x. If x is greater than 0 the variable sign is assigned the value 1; in the case where x is less than 0 the variable sign is set to − 1, otherwise it is set to 0.

3.3.2 “switch” Statement

A “switch” statement is also used to implement decisions in a program. It is helpful to use “switch” when you have a lot of “elseif” statements within an “if” statement. It switches among several cases based on an expression, as shown in the following syntax.

image

The “switch” expression should be either a string or a scalar value. The “case” expression should evaluate to the same data type as “switch” expression. When a “case” expression matches the “switch” condition, the corresponding block of statements is executed. Unlike C or Java, you do not need to use break statement only first matching case is executed. Here is an example.

Listing 3.7

money = input (‘Enter an amount (in cents)’);

switch money

case 1

cents = cents + 1;

case 5

nickel = nickel + 1;

case 10

dime = dime + 1;

case 25

quarter = quarter + 1;

case 100

dollars = dollars + 1;

Otherwise

alert(‘invalid amount’);

end;

3.4 Continue and Break Statements

In the absence of “goto” statements, “continue” and “break” statements are sometimes very useful. The continue statement moves the control to the next step of the “for” or “while” loop. For example,

Listing 3.8

> > for i = 1: 100

sum = sum + 1

end;

> > sum

sum =

100.0000

> > sum = 0;

> > for i = 1: 100

if rem (i, 2) == 0

continue;

end;

sum = sum + 1;

end;

> > sum

sum =

50.0000

The above example shows how the continue statement skips an iteration for an even number of values of “i” using condition rem(i, 2) == 0.

The break statement ends the execution of a “for” or “while” loop as shown below.

Listing 3.9

> > sum = 0;

> > for i = 1:100

 sum = sum + 1;

if i == 50

 break;

 endif;

endfor;

> > sum

Sum =

50

The above example shows how the break statement ends the execution of the “for” loop when the value of “i” becomes 50.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset