Matlab Tutorial 7: Common Programming Structures and Conditional Statements

While Loop in MATLAB

In a while loop the statements are repeated as long as the logical condition are true.
A while loop has the following the structure:

for variable=expression
control statements

If the logical expression is true, then the statements are executed. hen once again we evalute the logical expression and if still true then we repeat the statements. This continues until the logical expression becomes false. If the statement is false from the start, the loop is not entered.

Example 3:

Let us illustrate how to use a while-loop.

for i=1:10
fprintf('The sum of 10 elements gives: %f ', the_sum)

Test and check to see what the m-file count_up does! Are there any surprises?
In order to produce the output we need 8 times to evaluate the expression j<8. Often one compares two numbers in these evaluations. In Matlab we have 6 operators to be used in comparison. < Less than > Greater than == Equal <= Less tham or equal. >= Greater than or equal ~= Not equal If the logical expression is true, then it gets the value 1 and the statements that follows will be performed, otherwise 0.

If-else in MATLAB

It is sometimes needed to have alternatives in programming. In Matlab this is achieved by if and else conditions or switch statements. See the following example:

if t==1 % if t=1, then increment h with one.
else % else display the text ”Hello”.

The syntax looks like:

if logical expression
statement 2

If the logical expression is true then the statement 1 is carried out, but if the logical expression is false then statement 2 will be executed.There could of course be many statements to be done for each alternative.
Suppose one needs several alternatives instead of two. In that case syntax looks is:

if logical expression 1
statement 1
elseif logical expression 2
statement 2
elseif logical expresion n
statement n
statement n+1

Only one of the alternatives must be true otherwise the final statement n+1 will be performed.

Example 4:

We will now examplify this with a m-file. It requests temperatures from the keyboard and if we enter negative values (in degree Celsius) the answer will be ‘Freezing cold’. Temperatures above 25 gives the answer ‘Very hot’ and for all other inputs the reply will be ‘Temperature is OK!’ . The example is of course a very swedish one.

% M-file temperature.m
temp=input('Give a temperature !')
if temp<=0
disp('Freezing cold!')
elseif temp>25
disp('Very hot!')
disp('Temperature is OK')

The m-file above can deal with all temperatures given with numbers. One limitation is that we need to call it every time we want to decide the temperature. This can be improved by using a while loop outside the if and else structure.
We can have nested structures in Matlab.

Switch in MATLAB

Another way of performing evaluation of conditions is to use a switch and case structure. When we execute the structure, one case are supposed to be true and the corresponding statement is done. On the other hand if none of the cases are equal to the expression, then otherwise is valid.
The syntax is:

switch expression
case value 1
statement 1
case value 2
statement 2
case value n
statement n
statement n+1

Above the expression is compared to all the cases if none of these are equal with its corresponding value then otherwise. By using the case alternatives we can also compare with several values at the same time.

Pages: 1 2 3 4

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment