Lab100 Week 13: Loops and branches Theory

advertisement
Lab100 Week 13: Loops and branches
Loops allow us to instruct the computer to execute thousands of instructions by writing only a few lines
of code. This economy is an essential ingredient of computer programming.
By the end of session you should be able to:
− use and have a basic understanding of Scilab for loops
− use nested for loops
− use and have a basic understanding of simple branching techniques using if.
New Scilab commands
for end if else disp
Theory
Loops: for loops have the structure
FOR index = set of values
commands
END
Scilab executes the commands for each value of the index, until it ends.
Branches: An if statement only executes a command under certain conditions. If statements have
the general structure
IF expression
commands
END
Q 13.1 A very simple FOR loop.
for i = 1:10
disp(i)
end // for i
Write a FOR loop to print out the squares of the first ten numbers.
Q 13.2 The Fibonacci Sequence.
The beginning of the Fibonacci sequence is
1
1
2
3
5
8
13
21 34
55
1
It is generated with i as the index, and the code starts by setting up a vector with the first two
values equal to 1.
clear
x(1) = 1, x(2) = 1,
for i = 3:10
x(i) = x(i-1) + x(i-2) ; // sum prev two
end // for i
x
What is the value of x(7) ?
The commands in this code may be described by pseudo-code:
Define initial values of x(1) and x(2)
Let i take values from 3 to 10
Define x(i), e.g. x(3) = x(2) + x(1)
End, to stop the loop working indefinitely
Display results.
This iteration is written in formal mathematics as
x1 = 1, x2 = 1, and
xi = xi−1 + xi−2 , for i = 3, 4, . . . , 10
If you do not understand this loop ask a demonstrator before carrying on.
Q 13.3 Overwriting.
When constructing for loops, intermediate values of x may be overwritten as in this next example.
x
x
x
x
x
x
y
=
=
=
=
=
=
=
0;
0;
0;
1;
1;
1;
1;
for
for
for
for
for
i
i
i
i
i
=
=
=
=
=
1:5,
1:5,
1:5,
1:5,
1:5,
x
x
y
x
x
=
=
=
=
=
i
; end,x //
x + 1
; end,x //
x + 1; x = y; end,x //
x + x
; end,x //
x*i
; end,x //
for i = 1:5,
z = x ; x = x + y ; y = z
Try filling in the values of xi before
Scilab output.
A: x1 = 1
, x2 = 2
, x3 = 3
B: x1 = 1
, x2 = 2
, x3 =
C: x1 = 1
, x2 =
, x3 =
D: x1 = 2
, x2 = 4
, x3 =
A
B
C
D
E
; end,x // F
running the loops through Scilab. The final value of x is the
, x4 =
, x4 =
, x4 =
, x4 =
, x5 =
, x5 =
, x5 =
, x5 =
2
=x
=x
=x
=x
E: x1 = 1
, x2 = 2
, x3 =
F: x1 = 2
, x2 =
, x3 =
How many did you get right?
, x4 =
, x4 =
, x5 =
, x5 =
=x
=x
Q 13.4 Series.
P
The formal mathematical expression s = 5i=1 i is calculated in Scilab as
s = 0 ;
for i = 1:5,
s = s + i
; end , s
Write the Scilab code for the following expressions. (Hint: They are similar to the above.)
s=
P5
s=
P5
s=
P5
i=1 3
i
2
i=1 i
1
i=1 i!
Hint: The factorial i! is calculated from prod(1:i) or from factorial(i).
Q 13.5 Simple branching using if.
Try this script to test if i2 = 9.
for i = 1:4
if (i^2 == 9)
disp(’winner’)
else
disp(’loser’)
end // if
end // for i
Note the use of == rather than =. The statements define the for index, and if the condition is
satisfied display ‘winner’; but otherwise, if the condition fails, display ‘loser’.
Q 13.6 Nested for loops.
These contain for loops inside other for loops. What is the value of s after
s = 0
for i = 1:2,
for j = 1:3,
s = s + i + j
end // for j
end // for i
s
Before pasting the Scilab input, write out the elements in the sum as a 2 × 3 array.
3
Q 13.7 Pascal’s triangle.
Write out Pascal’s triangle by hand up to row 6. Then identify the elements C(i, j) in
// use C to store binomial coefficient
m = 6 ;
C = zeros(m,m) ; C(1:m,1) = 1
C
for i = 2:m
for j = 2:i
C(i,j) = C(i-1,j-1) + C(i-1,j) ;
end
end
C
;
Q 13.8 Closeness Test:
A test for closeness to zero might be
for i = 1:500
y = 1/i ;
if (y<.01)
disp(’danger’)
break
// break jumps out of the loop
end
end
i
Use pencil to connect the for and if with their appropriate ends. Do these connections cross?
ASSESSED WORK by Sunday: select quizwk13 from your MyModules page.
There is a more general IF statement of the form
IF expression
commands
ELSEIF expression
commands
ELSE
commands
END
4
Download