Flow Control Presented by: Md Fashiar Rahman
Arithmetic/Logic Unit If…else statement if if True: print('Hello world') Statement 1 Statement 2 Statement 3 if False: print('Hello world') Control Unit Arithmetic/Logic Unit if False: print('Hello world') print('Bye') Statement 7 Statement 8 Statement 9 Statement 4 Statement 5 Statement 6 Memory Unit if False: print('Hello world') print('Bye') How we get True or False?
Evaluate from an expression If…else statement How we get True or False? Evaluate from an expression x = 3 r = x % 2 if r == 0: print('Even') print('Bye') x = 3 r = x % 2 if r == 0: print('Even') if r == 1: print('Odd') print('Bye') x = 8 r = x % 2 if r == 0: print('Even') else: print('Odd') print('Bye') If test expression: Statement (s) If in if ?
If…else statement Nested if if…elif…else x = 8 r = x % 2 if r == 0: print('Even') if x > 5: print('Great') else: print('Not so great') print('Odd') print(‘Bye') x = 4 if x == 1: print('One') if x == 2: print('Two') if x == 3: print('Three') if x == 4: print('Four') x = 1 if x == 1: print('One') elif x == 2: print('Two') elif x == 3: print('Three') elif x == 4: print('Four') x = 5 if x == 1: print('One') elif x == 2: print('Two') elif x == 3: print('Three') elif x == 4: print('Four') else: print(‘Wrong Input')
While loop While Or For 100 times 1000 times print('Hello world') Initialization Initialization Condition i = 0 while i < 5: print('Hello world') i = i + 1 Condition i = 5 while i > 0: print('Hello world') i = i - 1 Increment Decreament
While loop Nested while loop i = 1 j = 1 while i <= 5: print('Hello world:‘ , end=“ “) while j <=3: print('Seminar‘ , end = “ ”) j = j + 1 i = i + 1 print() i = 1 j = 1 while i <= 5: print('Hello world:', end = " ") while j <=3: print('Seminar', end = " ") j = j + 1 i = i + 1 print() Hello world: Seminar Seminar Seminar
For loop for val in sequence: Body of for for i in range(1,11): if i % 5 != 0: print(i) for i in range(11,21,2): print(i) for i in range(21,10,-2): print(i) x = ['pop', 'rock', 'jazz'] I like pop I like rock I like jazz x = ['pop', 'rock', 'jazz'] for i in x: print("I like", i) x = ['pop', 'rock', 'jazz'] for i in range(len(x)): print("I like", x[i])
For loop # # # # for j in range(4): print("# ", end = "") for i in range(4): for j in range(4): print("# ", end = "") print() # # # # # # # #
For loop for i in range(4): for j in range(i): print("# ", end = "") # # # # # # # # # # # # # # # # # # # # # # # # #
Break, Continue & Pass 1 3 5 7 9 11 13 15 17 19 Bye for i in range(5): if i == 3: break print("Hello", i) print(‘Bye’) for i in range(5): if i == 3: continue print("Hello", i) print(‘Bye’) Hello 0 Hello 1 Hello 2 Hello 4 Bye for i in range(1,21): if i % 2 == 0: pass else: print(i) print('Bye') Hello 0 Hello 1 Hello 2 Bye for i in range(1,11): if i % 3 == 0 or i % 5 == 0: continue print(i) print('Bye') def fun(): pass Class Student: pass
Quiz for i in [1, 0]: print(i + 1) 2 1 [2, 1] 2 [2, 0]
Quiz i = sum = 0 while i <= 4: sum += i i = i + 1 10 print(sum) 4 10 4 None of the above
Quiz 4 is printed once while 4 == 4: print('4') 4 is printed four times 4 is printed infinitely until program closes Syntax error
Quiz for char in 'PAYTHON STRING': if char == ' ': break print(char, end = '') if char == 'O': continue PYTHON for char in 'PAYTHON STRING': if char == ' ': break if char == 'O': continue print(char, end = '') PYTHONSTRING PYTHN STRING
Quiz for i in range(1,11): if i % 2 == 0 and i % 3 == 0: continue print(i) print('Bye') 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 7 8 9 10 1 5 7 9 1 4 5 7 8 9 10
Quiz for i in range(4): for j in range(i+1,5): print(j , end = " ") 1 2 3 4 1 2 3 4 2 3 4 3 4 4 1 2 3 4 1 2 3 1 2 1 0 1 2 3 4 5
Any Question ??