CHAPTER 6: Control Flow Tools (for and while loops) COMPUTER PROGRAMMING SKILLS 1439-1440
Outline Overview Loops – Example The range function The in operator For loops while loops Using break Using continue Exercises
Overview Loops are used to execute a block of code several times. A loop statement allows you to specify that an action is to be repeated while some condition remains true. There are different types of loops: for, while. Each of them has its specific uses.
Five-element sequence For loops - Examples The following program will print “Hello” five times: Iteration variable Five-element sequence for i in [1,2,3,4,5] : print('Hello') Hello Indentation
For loops - Examples The program below asks the user for a number and prints its square, then asks for another number and prints its square, etc. It does this three times and then prints that the loop is done for i in [1,2,3] : num = eval(input('Enter a number: ')) print ('The square of your number is', num*num) print('The loop is now done.') Enter a number: 3 The square of your number is 9 Enter a number: 5 The square of your number is 25 Enter a number: 23 The square of your number is 529 The loop is now done.
The range function Example: The value we put in the range function determines how many times we will loop. The way range works is it produces a list of numbers from zero to the value minus one. For instance, range(3) produces five values: 0, 1, and 2. Examples: range( Number) range( begin,end) range( begin,end,step) for i in range(3): print('Python') Python
The in operator The in operator is used with lists. The in operator is also used with strings. For example, it can be used to tell if a string contains something or not. for i in [3,4,5]: print(i) 3 4 5 Name= 'hello' if 'o' in Name: print('Your string contains the letter o.') Name= 'hello' if ';' not in Name: print('Your string doesn’t contain the letter a.') Your string contains the letter o. Your string doesn’t contain the letter a.
For loops For loops are also called "definite loops" because they execute an exact number of times. for i in [0,1,2,3,4]: print(i) print ('----End---') for x in range (5,7): print(x) print ('----End---') Friends =['Ahmad', 'Nawaf', Mohammad'] for x in Friends: print('Hi ', x) print ('----End---') 1 2 3 4 ----End--- 5 6 ----End--- Hi Ahmad Hi Nawaf Hi Mohammad ----End---
While loops Example: No Yes n = 5 print (n) n = n -1 n = 5 print ('End') Yes n = 5 n = 5 while n > 0 : print (n) n = n-1 print ('End') print (n) 5 4 3 2 1 End n = n -1
While loops While loops are called "indefinite loops" because they keep going until a logical condition becomes False. Loops (repeated steps) have iteration variables that change each time through a loop. Often these iteration variables go through a sequence of numbers.
While loops Infinite loop!!! No Yes n = 5 print (n) No Yes n = 5 n=n+1 print ('End') Yes n = 5 print (n) while n > 0 : Infinite loop (No end) 5 … n > 0 ? No print ('End') Yes n = 5 n=n+1 while n > 0 : print (n) Infinite loop (No end) 5 6 … 7 8 print (n)
Using break The break statement ends the current loop and jumps to the statement immediately following the loop. It is like a loop test that can happen anywhere in the body of the loop. X=5 while X>1: X=X-1 if X == 3 : break print (X) print ('End') 4 End
Using continue The continue statement ends the current iteration and jumps to the top of the loop and starts the next iteration. X=5 while X>1: X=X-1 if X == 3 : continue print (X) print ('End') 4 2 1 End
Tips with loops Counting in a Loop To count how many times we execute a loop we introduce a counter variable that starts at 0 and we add one to it each time through the loop. Lp=0 print ('How many loops before? ', Lp) for i in [21,9,7]: Lp = Lp +1 print ('How many loops after? ', Lp) How many loops before? 0 How many loops after? 3
Tips with loops Summing in a Loop To add up a value we encounter in a loop, we introduce a sum variable that starts at 0 and we add the value to the sum each time through the loop. Sum=0 print (' Sum before loop = ', Sum) for i in [21,9,7]: Sum = Sum + i print (' Sum of all elements = ', Sum) Sum before loop = 0 Sum of all elements = 37
Tips with loops Finding the Average in a Loop An average just combines the counting and sum patterns and divides when the loop is done. Count =0 Sum=0 print (' Count before loop = ', Count) print (' Sum before loop = ', Sum) for i in [21,9,7,3,10,50,12]: Count = Count +1 Sum = Sum + i print (' Sum of all elements = ', Sum) print (' Average of all elements = ', Sum/Count) Count before loop = 0 Sum before loop = 0 Sum of all elements = 112 Average of all elements = 16.0
Tips with loops Filtering in a Loop We use an if statement in the loop to catch / filter the values we are looking for. print (' Before ') for value in [21,9,7,3,10,50,12]: if value >20: print (value) print (' After loop ') Before 21 50 After loop
Exercises The code below prints the numbers from 1 to 50. Rewrite the code using a while loop to accomplish the same thing. Write a program that asks the user for their name and how many times to print it. The program should print out the user’s name the specified number of times. A good program will make sure that the data its users enter is valid. Write a program that asks the user for a weight and converts it from kilograms to pounds. Whenever the user enters a weight below 0, the program should tell them that their entry is invalid and then ask them again to enter a weight. [Hint: Use a while loop, not an if statement]. for i in range(1,51): print (i)
Exercises Write a program that asks the user to enter his name and a character. The program should print out the user’s name and specify how many times the character occur in his name.