2 - Control of The Instruction Flow: 1. Simple Instructions and Instruction Sequences
2 - Control of The Instruction Flow: 1. Simple Instructions and Instruction Sequences
1.
2.
Conditional instructions
3.
Repetition instructions
4.
Embedded instructions
An assignment has the form : target = expression . The target often is a variable but it can be a
more complex object able to receive and to store a value (tuple, array entry ). The = assignment operator can be preceded by an operator used to express the new value of a store from the old
one.
The most simple way of composing instructions is to organize them in sequences. In a sequence, two instructions are separated by a semicolon or a newline.
conditions.
The most simple conditional instruction is the if instruction, which has the following syntax : if <condition> : <block of instructions>
<condition> is a boolean expression and <block of instructions> is a sequence of instructions which are indented in the same manner with respect to the line where the condition is.
The meaning of the instruction is the following: if the value of <condition> is True , <block of instructions> is executed, otherwise no action is performed.
>>>
<block2 of instructions>
The meaning of the instruction is the following: if the value of <condition> is True ,
x est impair
>>> x = 8 >>> if x % 2 == 0 : print "x est pair" else: print "x est impair"
The meaning of the instruction is the following: if the value of <condition 1> is True , <block 1 of instructions> is executed, otherwise if the value of <condition 2> is True , <block 2 of instructions> and so on. If no condition is true, <block n+1 of instructions> is executed.
it is afternoon
>>>
The for instruction has the following syntax: for <controller> in <seq>: <block of instructions>
In this instruction, <controller> is a variable used to control the number of iterations and <seq> is an object of type sequence (string, list, tuple).
The meaning of the instruction is the following: variable <controller> successively takes the different values of <seq> in the order of their apparition. At each instantiation of the controller, <bloc of instructions> is executed.
>>> range(5) [0, 1, 2, 3, 4] >>> for n in range(5) : print the square number of ", n, is: ", n**2
the square number of 0 is: 0 the square number of 1 is: 1 the square number of 2 is: 4 the square number of 3 is: 9 the square number of 4 is: 16 >>>
The meaning of the instruction is the following: expression <condition> is evaluated and if its value is True, <bloc dinstructions> is executed; then, expression <condition> is evaluated again and so on. As soon as <condition> takes the value False, the execution of the instruction ends.
For instructions are used when iterations are known in advance and while instructions when iterations are not known in advance but they depend on a condition.
>>> n = 0 >>> while n <= 5 : print the square number of ", n, is: ", n**2 n += 1
the square number of 0 is: 0 the square number of 1 is: 1 the square number of 2 is: 4 the square number of 3 is: 9 the square number of 4 is: 16 the square number of 5 is: 25
>>>
2 3 5
2.5 Exercises
1. What is the result of the execution of the following program when values 4, 2, 5 are respectively entered on the keyboard for a, b, c ? The same question when 2, 4, 5 are entered. What is the function of this program ? a = input(n1 : ) b = input(n2 : ) c = input(n3 : ) if a >b : if a > c : print a else : print c elif b >c : print b else : print c
2.5 Exercises
2. Give the result of the execution of the following program when value arma is entered for w1. The same question when value erre is entered. What is the function of this program ?
w1
3.
Give the result of the execution of the following program when value 5 is entered for x. The same question when value 6 is entered. What is the function of this program ?
x = input(Enter an integer ! ") y =2 goon = True while y < x and goon : z=y while z < x and goon : z += y if z == x : goon = False y += 1 print goon
2.5 Exercises
4. Write a Python program for each of the following specifications (without using Python libraries of functions).
a)
b)
Compute the length of a string (go through the string and increment a variable at each step).
c)
d)
Display all words of a sentence, one per line (two words are assumed to be separated by exactly one space character.
1. >>> n1 : 4 n2 : 2 n3 : 5 5
>>>
>>> n1 : 2 n2 : 4 n3 : 5 5 >>>
The program displays the maximum of three integers entered on the keyboard.
The program detects palindroms (words that remain unchanged when reversed)
3.
The function of the program is to detect if an integer entered on the keyboard is a prime number.
#c) s = input("Enter a string !\n") na=0 ne=0 ni=0 for c in s: if c=="a": na +=1 elif c=="e": ne +=1 elif c=="i": ni+=1 print "In the string \"", s, "\" the respective numbers of \"a\", \"e\", \"i\" are:", na, ",", ne, ",",ni