Вы находитесь на странице: 1из 17

MECN2012: Computing

Skills and Software


Development
Lecture 7:
The WHILE Algorithm, Nesting
& the BREAK Function

The WHILE Algorithm


Comparable to the FOR algorithm but used
for value-based flow control
Used in cases where the discriminating
variable does not change in a regular /
consistent manner
Most often used in numerical calculations
where the accuracy of the solution
(measured by the change from one
iteration to the next) is the key
discriminator

The WHILE Algorithm


Basic syntax:
while (condition)
(executable code)
end
Loops back to declaration until the
discriminating condition is violated
HIGHLY prone to infinite loops:
Often used with an IF-THEN-ELSE
statement to limit the number of iterations

The WHILE Algorithm


e.g. Print all fractions of 2 larger than 0.001
Value = 1
while Value > 0.001
print Value
temp1 = Value/2
Value = temp1
clear temp1
end
(All answers rounded to 5 decimal places
for presentation)

The WHILE Algorithm


Value

temp
1

Value
>
0.001

Scree
n

Value

temp1

Value
>
0.001

Screen

1
4

0.25

0.125

1
5

0.125

0.125

1
6

0.125

0.5

1
7

0.125

0.5

0.5

1
8

0.125

0.125

0.5

1
9

0.125

0.0625

0.5

2
0

0.062
5

0.0625

0.5

0.5

2
1

0.062
5

The WHILE Algorithm


Value

temp
1

Value
>
0.001

Scree
n

Value

temp1

Value
>
0.001

Screen

27 0.031
25

4
0

0.003
91

0.00391

28 0.031
25

0.031
25

4
1

0.003
91

29 0.031
25

0.015
63

4
2

0.003
91

30 0.015
63

0.015
63

4
3

0.003
91

0.00391

31 0.015
63

4
4

0.003
91

0.00195

32 0.015
63

4
5

0.001
95

0.00195

33 0.015
63

0.015
63

4
6

0.001
95

34 0.015
63

0.007
81

4
7

0.001
95

Nesting
Nesting is the use of one flow control
algorithm inside of another
A typical example is the use of IF-THEN-ELSE
statements within a FOR or WHILE loop
Nesting allows further branching within main
braches of a code
Many codes may use 2 or 3 levels of nesting
Nesting increases the chances of infinite
loops and black holes

Nesting
e.g. Calculating the output of a
piece-wise polynomial function for an
input vector X
for q = 1:1:length(X)
if X(q) < 0 then
Y(q) = 1 X(q)
else
Y(q) = X(q)*X(q) + 1
end
end

Nesting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
5
4
5
1
5
5 1
1
5
6
5
6
6
2
6
6 2
6
7
6
7
7 3
7
7
7

1
1

[4]
[4]

1
1
1
1
1
1
0
0
0
0

[4
[4
[4
[4
[4
[4
[4
[4
[4
[4

0
0
0

[4
32
1 2]
[4
[4 3
32
21
1 2]
2]

0
0

[4
[4 3
32
21
12
2 5]
5]

[4 3 2 1 2 5 10]

3]
3]
3]
3
2]
3
3 2]
2]
3 2 1]
3
3
2
1]
32
2 1]
1]

q=7
X(q) = X(7) = 3
X(q) < 0 0
Y(q) = X(q)*X(q) + 1
Y(7) = (3)*(3) + 1
Y(7) = 10

The BREAK Function


In some cases of nesting the function
may need to terminate once a
particular discriminating criterion is
met
This is done using the BREAK
function
BREAK terminates only the innermost
looping function (i.e. the first active
FOR or WHILE loop encountered
when searching up from the BREAK

The BREAK Function


e.g. Terminating a FOR loop for calculating
the value of a polynomial if the result
exceeds 10 000
for q = 1:1:100
Value = 3*q*q 2*q + 5
if Value > 10000 then
break
else
print Value
end
end

Оценить