Algo Pekan 05

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Algorithm and Programming 1

Muhamad Sabar
Informatics Engineering
Sekolah Tinggi Teknologi Bandung
2019
Prev
Python
1. Intro to Python
2. Comments
3. Data Type
4. Variable
5. Operator
Decision & Loops
Decision
The if–else is used to make choices in Python code. This is a compound
statement. The simplest form is
if c o n d i t i o n :
a c t i o n −1
else:
a c t i o n −2
The indentation is required. Note that the else and its action are
optional. The actions action-1 and action-2 may consist of many
statements; they must all be indented the same amount. The condition
is an expression which evaluates to True or False. Of course, if the
condition evaluates to True then action-1 is executed, otherwise action-
2 is exe-cuted. In either case execution continues with the statement
after the if-else.
x=1
if x > 0:
print " Friday is wonderful"
else:
print " Monday sucks"
print " Have a good weekend"

results in the output


Friday is wonderful
Have a good weekend
Note that the last print statement is not part of the if-else statement
(because it isn’t indented), so if we change the first line to say x = 0
then the output would be
Monday sucks
Have a good weekend
More complex decisions may have several alternatives depending on
several conditions. For these the elif is used. It means “else if” and one
can have any number of elif clauses between the if and the else
if x >= 0 and x < 10:
digits = 1
elif x >= 10 and x < 100:
digits = 2
elif x >= 100 and x < 1000:
digits = 3
elif x >= 1000 and x < 10000:
digits = 4
else:
digits = 0 # more than 4

In the above, the number of digits in x is computed, so long as the number is


4 or less. If x is negative or greater than 10000, then digits will be set to zero.
Loops
The syntax of a for loop is
for item in list :
action
As usual, the action consists of one or more statements, all at the same
indentation level. These statements are also known as the body of the loop.
The item is a variable name, and list is a list.
Execution of the for loop works by setting the variable successively to each
item in the list, and then executing the body each time. Here is a simple
example (the comma at the end of the print makes all printing occur on the
same line):
for i in [2, 4, 6, 0]:
print i,
This produces the output
2460
The syntax of the while loop is
while c o n d i t i o n :
action
Of course, the action may consist of one or more statements all at the same
indentation level. The statements in the action are known as the body of the
loop. Execution of the loop works as follows.
First the condition is evaluated. If True, the body is executed and the
condition evaluated again, and this repeats until the condition evaluates to
False. Here is a simple example:
n=0
while n < 10:
print n,
n=n+3
This produces the following output
0369
Note that the body of a while loop is never executed if the condition
evaluates to False the first time. Also, if the body does not change the
subsequent evaluations of the condition, an infinite loop may occur. For
example
while True:
print " Hello ",
will print Hellos endlessly. To interrupt the execution of an infinite loop,
use CTRL-C.
A loop may have an optional else which is executed when the loop finishes. For example,
the loop
for n in [10 ,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1]:
print n,
else:
print " blastoff"
results in the output
10 9 8 7 6 5 4 3 2 1 blastoff

and the loop


n=10
while n > 0:
print n,
n=n-1
else:
print " blastoff"
The break statement, like in C, breaks out of the smallest enclosing for or
while loop. The continue statement, also borrowed from C, continues with
the next iteration of the loop. The pass statement does nothing. It can be
sed when a statement is required syntactically but the program requires no
action. Here is an example of the use of a break statement and an else clause
in a loop.
for n in range (2, 10):
for x in range (2, n):
if n % x == 0:
print n, ’equals ’, x, ’*’, n/x
break
else:
# loop fell through without finding a factor
print n, ’is a prime number ’
Quiz
Preparing your devices
Write down a syntax for this cases
1. How to find out the bigest orang among tree orang that have been
stored?
2. How to refil a glass 4 times with orange juice?
3. How can we know if the glass have been filled for 4 times?
Thanks

You might also like