Anda di halaman 1dari 8

2/22/2014

EEE 315: Numerical Analysis

United International University

Structured Programming
 The simplest of all M-files perform instructions

sequentially.
 All computer languages include statements allowing
programs to take non sequential paths.
 These can be classified as
Decisions(or Selection). The branching of flow based on a
decision.
 Loops(or Repetition). The looping of flow to allow
statements to be repeated.


2/22/2014

Decisions: if structure
 This structure allows you to execute a set of statements if a logical

condition is true. Its general syntax is


if condition
statements
end
where condition is a logical expression that is either true or false.
For example, here is a simple M-file to evaluate whether a grade is
passing:
function grader(grade)
if grade >= 60
disp('passing grade')
end
>> grader(95.6)

Single line if structure


 For cases where only one statement is executed, it is

often convenient to implement the if structure as a


single line,
if grade > 60, disp ('passing grade'), end
 This structure is called a single-line if. For cases

where more than one statement is implemented, the


multiline if structure is usually preferable because it
is easier to read.

2/22/2014

Logical Conditions


The simplest form of the condition is a single relational expression that


compares two values as in
value1 relation value2
where the values can be constants, variables, or expressions and the
relation is one of the relational operators listed in Table below
Example

Operator

Relationship

x == 0

==

Equal

unit ~=m

~=

Not Equal

a<0

<

Less than

s>t

>

Greater than

3.9 <= a/3

<=

Less than or equal to

r >= 0

>=

Greater than or equal to

NOT, AND & OR Operators


 MATLAB also allows testing of more than one logical condition by



employing logical operators. We will emphasize the following:


~(Not) - Used to perform logical negation on an expression.
~ expression
&(And) - Used to perform a logical conjunction on two
expressions.
expression1 & expression2
If both expressions evaluate to true, the result is true. If either or
both expressions evaluates to false, the result is false.
|(Or) - Used to perform a logical disjunction on two expressions.
expression1 | expression2
If either or both expressions evaluate to true, the result is true.
Priority : highest to lowest: ~, & and |

2/22/2014

The if else Structure


 This structure allows you to execute a set of statements if a

logical condition is true and to execute a second set if the


condition is false.
Its general syntax is
if condition
statements1
else
statements2
end

Example,
if grade>=60
disp (passing grade)
else
disp (fail)
end

The if.elseif Structure




It often happens that the false option of an if...else structure is another


decision. For such cases, a special form of decision structure, the if...elseif
has been developed. It has the general syntax
if condition1
statements1
elseif condition2
statements2
elseif condition3
statements3
.
.
.
else
statementselse
end

2/22/2014

Example of if.elseif Structure


function sgn = mysign(x)
% mysign(x) returns
% 1 if x is greater than zero.
% -1 if x is less than zero.
% 0 if x is equal to zero.
if x > 0
sgn = 1;
elseif x < 0
sgn = -1;
else
sgn = 0;
end

The switch Structure


 The switch structure is similar in spirit to the if...elseif structure. However,

rather than testing individual conditions, the branching is based on single test
expression.
 Depending on its value, different blocks of code are implemented.
 An optional block is implemented if expression takes none of prescribed
values.
 It has the general syntax
switch testexpression
case value1
statements1
case value2
statements2
.
.
otherwise
statementsotherwise
end

2/22/2014

An Example of switch Structure


grade = 'B';
switch grade
case 'A'
disp('Excellent')
case 'B'
disp('Good')
case 'C'
disp('Mediocre')
case 'D'
disp(Poor')
case 'F'
disp(Failed')
otherwise
disp(WRONG!')
end

Loops
 As the name implies, loops perform operations repetitively.
 There are two types of loops, depending on how the

repetitions are terminated.


 A for loop ends after a specified number of repetitions.
 A while loop ends on the basis of a logical condition.

2/22/2014

The for loop


 The for...end Structure. A for loop repeats statements a specific

number of times. Its general syntax is


for index= start : step : finish
statements
end

 The for loop operates as follows.






The index is a variable that is set at an initial value, start. The program
then compares the index with a desired final value, finish.
If the index is less than or equal to the finish, the statements are
executed.
When it reaches the end line that marks the end of the loop, the index
variable is increased by the step and the program loops back up to the
for statement.
The process continues until the index becomes greater than the finish
value.

A while Structure
 A while loop repeats as long as a logical condition is true. Its general

syntax is

while condition
statements
end
 The statements between the while and the end are repeated as long

as the condition is true. A simple example is


x=8
while x > 0
x = x - 3;
disp (x)
end

2/22/2014

The while..break structure


Although the while structure is extremely useful, the fact that it always
exits at the beginning of the structure on a false result. If you want to
break the loop as soon as the condition is not satisfied, you can use
while.break structure.
 The syntax of while...break structure, can be written as
while (1)
statements
if condition, break, end
statements
end
 An example is:
while (1)
x = x - 3;
If x < 0, break, end
disp(x)
end


Thanks

Anda mungkin juga menyukai