16/11/20171Introduction toprogrammingLoopsFlow chart – for loopfor (item in sequence)Statement blockYesNoFor each item in sequenceLast item checked?Statement blockTerminatefor x in range(1,20):print(x)16/11/20172Flow chart – while loopWhileLoop partLoopconditionTFWhile loop – repeat a block of statements as long as thecondition is truewhile test expression:statement blockyi =0while True:print(i)i = i+14Draw the flowchart for the following problem:– a positive number N … Continue reading “Introduction to programming Loops | My Assignment Tutor”
16/11/20171Introduction toprogrammingLoopsFlow chart – for loopfor (item in sequence)Statement blockYesNoFor each item in sequenceLast item checked?Statement blockTerminatefor x in range(1,20):print(x)16/11/20172Flow chart – while loopWhileLoop partLoopconditionTFWhile loop – repeat a block of statements as long as thecondition is truewhile test expression:statement blockyi =0while True:print(i)i = i+14Draw the flowchart for the following problem:– a positive number N is input– the sum from 1 to N is calculated– the sum is output to the screenFlow chart – example16/11/201735StartStopInput NOutput sumi = i + 1FTsum=0i = 0i 0Fact=Fact * iFactorial number – solutionExample code – for loop• digits = [0, 1, 5]• for i in digits:• print(i)• else:• print(“No items left.”)16/11/20175Example code – for loop• # Program to iterate through a listusing indexing• genre = [‘pop’, ‘rock’, ‘jazz’]• # iterate over the list using index• for i in range(len(genre)):• print(“I like”, genre[i])Factorial of a number• # Factorial of a number n! = n*(n‐1)!• num = int(input(“Enter a number: “))• factorial = 1• # check if the number is negative, positive or zero• if num