Download presentation
Presentation is loading. Please wait.
Published byRosalyn Green Modified over 8 years ago
1
LESSON 5 LOOPING AND BRANCHING Azalya Rahmatika Fathul Fithrah JURUSAN MATEMATIKA FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM UNIVERSITAS SYIAH KUALA DARUSSALAM, BANDA ACEH Mei, 2013
2
The if statement allows to perform a statement if a condition is satisfied. The if uses a boolean variable to perform its choice: if the boolean is true, then the statement is executed. A condition is closed when the end keyword is met.
3
If the condition is not satisfied, the else statement, as in the following script. In order to get a boolean, any comparison operator can be used: “==“, “>”, etc.
4
When we have to combine several condition, the elseif statement is helpful. In the following script, we combine several elseif statement in order to manage values of the integer i. But, if there are many elseif statement required, most of the time that implies that a select statement should be used instead.
5
The select statement allows to combine several branches in a clear and simple way. Depending on the value of a variable, it allows to perform the statement corresponding to the case keyword. There can be as many branches as required.
6
The error function generates an error associated with the given message. When an error is generated, the execution is interrupted and the interpreter quits all the function.
7
The for statement allows to perform loops, i.e. allows to perform a given action several times. Most of the time, a loop is performed over integer values, which go from a starting to an ending index value.
8
The while statement allows to perform a loop while a boolean expression is true. At the beginning of the loop, if the expression is true, the statement in the body of the loop are executed. When the expression becomes false (an event which must occur at certain time), the loop is ended.
9
It should be clear that the previous example is just an example for the while statement. If we really wanted to compute the sum of the numbers from 1 to 10, we should rather use the sum function.
10
The break statement allows to interrupt a loop. Usually, we use this statement in loops where, once some condition is satisfied, the loops should not be continued.
11
In the following example, we compute the sum s = 1 + 3 + 5 + 7 + 9 = 25. The modulo (i,2) function returns 0 if the number i is even. In this situation, the script goes on to the next loop.
12
As an example of vectorized computation, the previous algorithm can be performed in one function call only. Indeed, the following script uses the sum function combined with the colon operator “:” and produces same the result. The previous script has two main advantages over the while -based algorithm. 1.The computation makes use of a higher-level language, which is easier to understand for human beings. 2.With large matrices, the sum -based computation will be much faster than the while -based algorithm.
13
Thank You for Your Attention!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.