Automatic Counter Loop Structure Overview Learning Objectives Learning

Automatic Counter Loop Structure

Overview • Learning Objectives • Learning Outcomes • Loop Logic Structures (Automatic counter loop) • Example • Summary

Learning Objectives • To explain automatic counter loop logic structures • To apply loop logic structures

Learning Outcomes • The Students are able to: • To explain automatic counter loop logic structures • To apply loop logic structures

Overview of Loop Logic Structure

While/While. End Pretest Looping (While Loop) 0 -6

Automatic Counter Loop Format Pretest Looping (For / Counter Loop) 0 -7

Flowchart of Automatic-Counter Loop 0 -8

Python Syntax: for Loop for counter in range (startvalue, endvalue, stepvalue): ---->repeated statements 0 -9

Example 1 Calculate average of given ages. 0 -10

Example 1 0 -11

Python Code Example 1 n=input(“Enter N: ”) sum=0 for i in range(1, n+1, 1): age=input(“Enter Age: ”) sum=sum+age avg=sum/n print “Average: ”, avg 0 -12

Example 2 Print “Helloworld” n times on output screen. 0 -13

Example 2 Algorithm: 1)Start 2)Read n 3)Loop: i=1 to n step 1 print “Helloworld” Loop. End: I 4)Stop 0 -14

Start Example 2 Read n i 1 n 1 Python Code: n =input(“Enter N: ”) for i in range(1, n+1, 1): print “Helloworld” Print “Helloworld” i Start 0 -15

Example 3 Print 1 -n numbers on output screen. 0 -16

Example 3 Algorithm: 1)Start 2)Read n 3)Loop: i=1 to n step 1 print i Loop. End: I 4)Stop 0 -17

Start Example 3 Read n i Python Code: n =input(“Enter N: ”) for i in range(1, n+1, 1): print i 1 n 1 Print i i Start 0 -18

Example 4 Print sum of all even numbers from 1 -n numbers on output screen. 0 -19

Start Example 4 Python Code: n =input(“Enter N: ”) sum=0 for i in range(1, n+1, 1): if i%2==0: sum=sum+i print “Sum: ”, su Read n sum=0 i 1 N 1 If i%2 == 0 T sum=sum+i i Print sum 0 -20 Start

Example 4 Algorithm: 1)Start 2)Read n 3)sum=0 4)Loop: i=1 to n step 1 if i%2 == 0 then sum=sum+I end if Loop. End: I 5)print “Sum: ”, sum 6)Stop 0 -21

Example 5 Print count of positive and negative numbers. 0 -22

Start Example 5 Python Code: n =input(“Enter N: ”) pc=0 Nc=0 for i in range(1, n+1, 1): if i>0: pc=pc+1 elif i<0: nc=nc+1 print “Positive Count: ”, pc print “Negative Count: ”, nc Read n pc=0, nc=0 i 1 n 1 If i > 0 T pc=pc+1 F If i < 0 F i Print pc, nc 0 -23 Start T pc=pc+1

Example 5 Algorithm: 1)Start 2)Read n 3)pc=0, nc=0 4)Loop: i=1 to n step 1 if i>0== 0 then pc=pc+1 else if i < 0 then nc=nc+1 end if Loop. End: I 5)print “Positive Count: ”, pc, “Negative Count: ”, nc 6)Stop 0 -24

Summary • Automatic Counter Loop • Use of Automatic Counter Loop

Important Questions • Find the sum of 1 tos n numbers using while, do-while and for loop • Check the number is palindrome or not • Check the number is perfect square • Check the number is prime or not • Find factorial of the number • Find Fibonacci series • Draw astrike graph • Find grade of n students
- Slides: 26