Lecture No 12 Loop Logic Structure Overview Learning

  • Slides: 25
Download presentation
Lecture No. 12 Loop Logic Structure

Lecture No. 12 Loop Logic Structure

Overview • • • Learning Objectives Learning Outcomes Loop Logic Structures Example Summary

Overview • • • Learning Objectives Learning Outcomes Loop Logic Structures Example Summary

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

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

Learning Outcomes • The Students are able to: • Explain loop logic structures •

Learning Outcomes • The Students are able to: • Explain loop logic structures • Apply loop logic structures

Overview of Sequential Logic Structure

Overview of Sequential Logic Structure

Overview of Decision Logic Structure

Overview of Decision Logic Structure

Introduction to Loop Logic Structure

Introduction to Loop Logic Structure

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

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

Flowchart Diagram of While/While. End 0 -9

Flowchart Diagram of While/While. End 0 -9

Python Syntax: While Loop • Python Syntax: while condition : repeated statements 0 -10

Python Syntax: While Loop • Python Syntax: while condition : repeated statements 0 -10

Example 1 • Print n times “Hello” using while loop Algorithm: 1) Start 2)

Example 1 • Print n times “Hello” using while loop Algorithm: 1) Start 2) Input n 3) i=1 4) while i<=n print “Hello” i=i+1 while end 4) Stop 0 -11

Example 1 • Python Code: n=input(‘Enter N: ’) i=1 while i<=n: --->print “Hello” --->i=i+1

Example 1 • Python Code: n=input(‘Enter N: ’) i=1 while i<=n: --->print “Hello” --->i=i+1 Start input n i=1 While i<=n T print “Hello” i=i+1 Stop 0 -12 F

Example 2 • Print 1 to n numbers using while loop Algorithm: 1) Start

Example 2 • Print 1 to n numbers using while loop Algorithm: 1) Start 2) Input n 3) i=1 4) while i<=n print i i=i+1 while end 4) Stop 0 -13

Example 2 • Python Code: n = input (‘Enter N: ’) i=1 while i<=n:

Example 2 • Python Code: n = input (‘Enter N: ’) i=1 while i<=n: --->print i --->i=i+1 Start input n i=1 While i<=n T print i i=i+1 Stop 0 -14 F

Example 3 • Print 1 to n and find sum of all numbers using

Example 3 • Print 1 to n and find sum of all numbers using while loop Algorithm: 1) Start 2) Input n 3) i=1, sum=0 4) while i<=n print I sum = sum+i i=i+1 while end 5) print “Sum: ”, sum 6) Stop 0 -15

Example 3 • Python Code: n=input(‘Enter N: ’) i=1 sum=0 while i<=n: --->print i

Example 3 • Python Code: n=input(‘Enter N: ’) i=1 sum=0 while i<=n: --->print i --->sum=sum+i --->i=i+1 print “Sum: ”, sum 0 -16 Start input n i=1, sum=0 While i<=n T print i sum=sum+i i=i+1 print sum Stop F

Example 4 • Print all even numbers and find sum of all even numbers

Example 4 • Print all even numbers and find sum of all even numbers from 1 to n using while loop Algorithm: 1)Start 2)Input n 3) i=1, sum=0 4) while i<=n if n%i ==0 then print i sum = sum+i end if i=i+1 while end 5) print “Sum: ”, sum 0 -17 6) Stop

Example 4 • Python Code: n=input(‘Enter N: ’) i=1 sum=0 while i<=n: --->if n%i

Example 4 • Python Code: n=input(‘Enter N: ’) i=1 sum=0 while i<=n: --->if n%i ==0 then --->print i --->sum=sum+i --->i=i+1 print “Sum: ”, sum Start Input n i=1, sum=0 While i<=n i=i+1 T if n%i then print i sum=sum+i Print sum 0 -18 Stop F

Example 5 • Print reverse number of given number using while loop Algorithm: 1)Start

Example 5 • Print reverse number of given number using while loop Algorithm: 1)Start 2) Read n 3) rev=0 3) while n>0 d=n%10 rev=rev+d n=n/10 while end 4) print “Reverse Number: ”, rev 5) Stop 0 -19

Example 5 • Python Code: n=input(“Enter N: ”) sum=0 while n>0: --->d=n%10 --->rev=rev*10+d --->n=n/10

Example 5 • Python Code: n=input(“Enter N: ”) sum=0 while n>0: --->d=n%10 --->rev=rev*10+d --->n=n/10 print “Reverse: ”, rev 0 -20 Start Read n rev=0 While n>0 T d=n%10 rev=rev+d n=n/10 Print “Reverse: ”, rev Stop F

Example 6 Find average of the given ages Python Code: n=input(“Enter n: ”) i=1

Example 6 Find average of the given ages Python Code: n=input(“Enter n: ”) i=1 sum=0 while(i<=n): ---->age=input(“Enter Age: ”) ---->sum=sum+age ---->i=i+1 avg=sum/n print “Average: ”, avg 0 -21

0 -22

0 -22

Example • • Print 1 to n and its square using while loopc Check

Example • • Print 1 to n and its square using while loopc Check number is perfect square Check number is prime or not Check number is divisible by 3 Check number is divisible by 5 Find factorial of a given number Find Fibonacci series Find Multiplication Table 0 -23

Summary • Introduction of Loop Logic Structures • Use of Loop Logic Structures using

Summary • Introduction of Loop Logic Structures • Use of Loop Logic Structures using while loop

Important Questions Loop Logic Structure • Explain pretest condition loop logic structures with proper

Important Questions Loop Logic Structure • Explain pretest condition loop logic structures with proper example.