COMP1126 Lecture 03 - Looping I PDF

Summary

This document provides an explanation of looping mechanisms in programming, specifically focusing on while and for statements in the Python language. It explains the concept of iteration and the differences between iterative and recursive approaches. Several examples are provided to illustrate these concepts.

Full Transcript

COMP1126: Looping Looping I – Iteration Take-away Message All non-trivial programming solutions to a problem require some sort of repetition of a process in order to complete a task. (i.e. looping is unavoidable) Looping can be achieved through recursion or...

COMP1126: Looping Looping I – Iteration Take-away Message All non-trivial programming solutions to a problem require some sort of repetition of a process in order to complete a task. (i.e. looping is unavoidable) Looping can be achieved through recursion or iteration – Iteration: Focus on changing state; extract result from state at end. Thisextracting functions focuses on changing and from the state at end. – Recursion: Focus on result in terms of smaller results This functions focuses on getting results in smaller form. 2 Repetition - Recursion & Iteration  Iteration Its a special function that creates The use of looping special forms to create repetition infinite loops and conditions that never evaluates to false. Loops infinitely if condition never evaluates to false  Recursion The use of function calls to create repetition Loops infinitely if condition never breaks down to base case Its a function that calls to create repetition that loops conditions that Repeatedly invokes the mechanism and function breaks down the base case.  Uses more memory  Copies of the function’s variables are made Often presents elegant solutions While and For Statements The while statement is the more general repetition construct. It repeats a set of statements while some condition is True. Boolean The for statement is useful for iteration, moving through all the elements of data structure, one at a time. A while statement is a set of statements while some conditions are true on the other hand a for statement moves through elements of data structure. Iterative programs Focus on the state of computation Initialize a state outside the loop Test a condition on the state to determine whether Steps to follow to execute the body Construct block of code – What is being repeated – Change variable that modifies state Decide what to do when loop terminates while Loop If the Boolean If the Boolean expression is expression is false it would true it will skip everthing repete the inside the suite statements in however (while block) the suite or and just (while block) continue to the until the following pyton expressoion statements. becomes false while Loop Top-tested loop (pretest) – test the boolean before running – test the boolean before each iteration of the loop As long as Boolean Expression is True continue doing suite1 Only when Boolean expression is Follow Stepts while boolean expression: False do suite2 suite1 Note- To do suite1 at least once suite2 ensure that the Boolean expression will start off as being True and then the code in suite1 will make it False Repeat While the Boolean is True while loop will repeat the statements in the suite while the boolean is True (or its Python equivalent) If the boolean expression never changes during the course of the loop, the loop will continue forever. while Loop: Example Find the square root of a given perfect square x= 16 ans = 0 while while ans*ans ans*ans divisor_10() x=10 divisor 1 for i in [1,2,3,4,5,6,7,8,9,10]: divisor 2 divisor 5 if x%i == 0: divisor 10 print ('divisor ', i) >>> divisor(10) divisor 1 def divisor(x): divisor 2 divisor 5 for i in range(1,11): divisor 10 if x%i == 0: >>> divisor(26) divisor 1 print ('divisor ', i) divisor 2 for Loop: Example def divisor(x): >>> divisor(26) for i in range(1,x+1): divisor 1 divisor 2 if x%i == 0: divisor 13 print ('divisor ', i) divisor 26 for Loop: Example Count number of even numbers in a given list >>> even_nbrs([2,5,34,67,22,45,56]) 4 def even_nbrs(lst): nbr_even=0 for i in lst: if i%2 == 0: nbr_even = nbr_even +1 return nbr_even for Loop: Example Count number of vowels in a given string >>> count_vowels("hello") 2 def count_vowels(x): v=0 for i in x: if i == "a" or i == "e" or i == "i" or i == "o" or i=="u": v = v+1 return v Nested for Loop: Example Print a tic tac board >>>tic_tac_board(["XOX","OOX","OXO"]) print (“Welcome”) print (“to Computing”) def tic_tac_board(slst): Welcome to Computing print("-----") print (“Welcome”, end = “ ”) for row in slst: print (“to Computing”) for c in row: Welcome to Computing print (c,end = " ") print("\n-----") print("Who won?") Summary Programs that perform repetition Looping mechanisms – while and for statements

Use Quizgecko on...
Browser
Browser