Functions and Loops: Programming. It Is The Most Popular Way of Programming Since It Is Quite
Functions and Loops: Programming. It Is The Most Popular Way of Programming Since It Is Quite
Functions and Loops
6.1 Introduction
When a particular numerical task needs to be “repeated” over different
data points, digital computers become a useful tool since they can perform
this action with greater speeds than humans. Loops perform exactly this
task. Using a condition to check the start and termination rules, users can
perform repetitive parts of a process as desired. Different programming
languages and environments have different rules for defining loops.
Scilab provides a simple way to define and run loops. In addition to
loops, functions also define an important part of modern programming
architecture. A big program may require a set of instructions to be
called at different times. Hence, this set of instructions can be defined
as a subprogram, which can be requested to perform the computation
at a desired time. In this way, a complicated task can be divided into
many small parts. This architecture of programming is called modular
programming. It is the most popular way of programming since it is quite
logical, good at visualizing the problem, and easy to debug. The most
popular way of defining these small sets of instructions is to define them as
functions. Together, functions and loops break a numerical computation
problem into a series of simpler problems that can be accessed as
required. In this chapter, we will discuss both of these concepts in detail.
Chapter 6 Functions and Loops
6.2 Loops
Loops form an essential part of an algorithm since they perform the
tasks that computers perform best: doing repetitive actions in a very fast
manner. Loops can come in many flavors such as for loop, which repeats
certain tasks over a list of variable values; while loop, which checks a
logical condition before executing a certain task; and if-then-else loop,
which checks a condition and directs the flow of an algorithm. The loop
you choose depends on the problem at hand.
A variety of functions and their usage are described in the following
sections. Judging their usage critically becomes supremely important because
the looping part of an algorithm consumes most of the execution time.
6.2.1 while
while loop defines a logical condition and, until it is satisfied, it runs a
block of code. The syntax for while loop is the following:
1 while condition
2 BODY
3 endwhile
1 i = 1
2 while i<20
3 disp(i);
4 i = i∗2;
5 end
146
Chapter 6 Functions and Loops
1 −−>exec('/Users/sandeepnagar/.../while.sce', −1)
2 1.
3 2.
4 4.
5 8.
6 16
147
Chapter 6 Functions and Loops
6.2.3 for
for loop is used to perform computation on a list of known values. The
syntax of for loop is the following:
The keyword for declares the starting of the loop where a variable
takes the values stored in a vector. Then a body of code (represented by
BODY) is executed. The keyword end declares the end of for loop. This is
explained in Listing 6-2.
1 for i = 1:10
2 square_root = sqrt(i);
3 disp(square_root)
4 end
5
6 disp("Program f inished")
1 −−−>exec('/Users/sandeepnag.../for.sce', −1)
2 1.
3 1.4142136
4 1.7320508
5 2.
148
Chapter 6 Functions and Loops
6 2.236068
7 2.4494897
8 2.6457513
9 2.8284271
10 3.
11 3.1622777
12 Program f inished
6.2.4 if-elseif-else
In situations where a number of conditions needs to be checked at
different points of time, if-elseif-else loop works well. The syntax for
this loop is given by the following:
1 if condition1
2 BODY1
3 elseif condition2
4 BODY2
5 else
6 BODY3
7 endif
149
Chapter 6 Functions and Loops
1 i = rand(1,1);
2 if i>0.5 then
3 disp(i);
4 disp("True");
5 else
6 disp(i);
7 disp("False");
8 end
150
Chapter 6 Functions and Loops
1 i = rand(1,1);
2 if i>0.5 then
3 disp(i);
4 disp("Value is larger then 0.5");
5 elseif i>0.3 then
6 disp(i);
7 disp("value is larger than 0.5 and 0.3");
8 else
9 disp(i)
10 disp("value is smaller than 0.5")
11 end
1 −3−>exec('/Users/sandeepnagar/.../ifelseif.sce', −1)
2 0.0485566
3 value is smaller than 0.5
4 −3−>exec('/Users/sandeepnagar/.../ifelseif.sce', −1)
5 0.6723950
6 Value is larger then 0.5
7 −3−>exec('/Users/sandeepnagar/.../ifelseif.sce', −1)
8 0.2017173
9 value is smaller than 0.5
10 −3−>exec('/Users/sandeepnagar/.../ifelseif.sce', −1)
11 0.3911574
12 value is larger than 0.5 and
13 0.3
151