SlideShare a Scribd company logo
1 of 15
Download to read offline
Looping & It's Control
CONTENTS
L PING
FOR Loop
NESTED Loops
WHILE Loop
BREAK
CONTINUE Loop Control Statements
PASS
.
.
WHILE L p
A while loop statement in Python programming language repeatedly
executes a target statement as long as a given condition is true. A
uniform indent is mandatory for rest of the statements to be in a
loop.
Example: Printing Factorial of a number
num = int(input("Enter a number to calc it's factorial:"))
temp=1
fact=1
while temp <= num:
fact*=temp
temp=temp+1
print("Factorial of", num, "is :",fact)
WAP: To Print first 10 Odd numbers by correctig foll. code
n=1
while n<=10:
if(n%2!=0):
print(">", n)
n=n+1
else:
print("Bye")
FOR L p
The for statement in Python has the ability to iterate over the items of
any sequence, such as a list or a string. Unlike the traditional FOR
loop of C / C++ / Java, Python uses different structure than others.
Example: Printing first 10 numbers of Fibonacci series
a=0; b=1
fib=1
for i in range(10):
print(fib)
fib = a+b
a=b
b=fib
WAP: To Print a the Star Pattern using single for loop as
shown below:
*
**
***
****
*****
s=""
for i in range(5):
s+= "*"
print(s)
else:
print("Goodbye")
NESTED L p
Python programming language allows the use of one loop inside
another loop. The following section shows a few examples to
illustrate the concept.
FOR LOOP
for iterating_val in sequence:
for iterating_val in sequence:
statement(s)
statement(s)
WHILE LOOP
while expression:
while expression:
statement(s)
statement(s)
Example: Printing Multiplication tables from 1 to 10
for i in range(1,11):
for j in range(1,11):
k=i*j
print(k, end="t")
print('n')
n = 4
while n>0:
for i in range(4):
x="*t" *n
print(x)
n-=1
else:
exit(0)
Example: Printing Embroidery Star Pattern
BREAK
The break statement is used for premature termination of the current
loop. After abandoning the loop, execution at the next statement is
resumed, just like the traditional break statement in C.
The most common use of break is when some external condition is
triggered requiring a hasty exit from a loop. The break statement can
be used in both while and for loops.
If you are using nested loops, the break statement stops the execution
of the innermost loop and starts executing the next line of the code
after the block.
SYNTAX:
break
EXAMPLE: Using break statement
for i in range(10):
print(i)
if(i==5):
break
else:
print(i)
CONTINUE
The continue statement in Python returns the control to the beginning
of the current loop.
When encountered, the loop starts next iteration without executing the
remaining statements in the current iteration.
SYNTAX:
continue
EXAMPLE: Using Continue statement
n= 0
while n != 10:
n += 1
if n%2 == 0:
continue
print(n)
Affects the statements written after
continue statement
for i in range(1,11):
for j in range(1,11):
k=i*j
print(k, end="t")
if i%3==0:
break
print()
EXAMPLE: BREAK in FOR - Mul. Tables
It is used when a statement is required syntactically but you do not
want any command or code to execute.
The pass statement is a null operation; nothing happens when it
executes.
The pass statement is also useful in places where your code will
eventually go, but has not been written yet i.e. in stubs).
PASS
SYNTAX:
pass
EXAMPLE: Using Pass in order to replace a Statement which
is undecided
num=1
if num<0:
print("Number is Negative")
elif num>0:
pass
else:
print("Number is Zero")
python program

More Related Content

Similar to python program

6 Iterative Statements.pptx
6 Iterative Statements.pptx6 Iterative Statements.pptx
6 Iterative Statements.pptx
ssuser8e50d8
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
bsdeol28
 

Similar to python program (20)

6 Iterative Statements.pptx
6 Iterative Statements.pptx6 Iterative Statements.pptx
6 Iterative Statements.pptx
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptxControl_Statements_in_Python.pptx
Control_Statements_in_Python.pptx
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
Python session3
Python session3Python session3
Python session3
 
PYTHON 3.pptx
PYTHON 3.pptxPYTHON 3.pptx
PYTHON 3.pptx
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Loops in c
Loops in cLoops in c
Loops in c
 
Control structures
Control structuresControl structures
Control structures
 
GE3151 UNIT II Study material .pdf
GE3151 UNIT II Study material .pdfGE3151 UNIT II Study material .pdf
GE3151 UNIT II Study material .pdf
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
C++ programming
C++ programmingC++ programming
C++ programming
 

Recently uploaded

Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
BalamuruganV28
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 

Recently uploaded (20)

engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
Working Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdfWorking Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdf
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdf
 
Intro to Design (for Engineers) at Sydney Uni
Intro to Design (for Engineers) at Sydney UniIntro to Design (for Engineers) at Sydney Uni
Intro to Design (for Engineers) at Sydney Uni
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdf
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & Modernization
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 

python program

  • 1. Looping & It's Control
  • 2. CONTENTS L PING FOR Loop NESTED Loops WHILE Loop BREAK CONTINUE Loop Control Statements PASS . .
  • 3. WHILE L p A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true. A uniform indent is mandatory for rest of the statements to be in a loop. Example: Printing Factorial of a number num = int(input("Enter a number to calc it's factorial:")) temp=1 fact=1 while temp <= num: fact*=temp temp=temp+1 print("Factorial of", num, "is :",fact)
  • 4. WAP: To Print first 10 Odd numbers by correctig foll. code n=1 while n<=10: if(n%2!=0): print(">", n) n=n+1 else: print("Bye")
  • 5. FOR L p The for statement in Python has the ability to iterate over the items of any sequence, such as a list or a string. Unlike the traditional FOR loop of C / C++ / Java, Python uses different structure than others. Example: Printing first 10 numbers of Fibonacci series a=0; b=1 fib=1 for i in range(10): print(fib) fib = a+b a=b b=fib
  • 6. WAP: To Print a the Star Pattern using single for loop as shown below: * ** *** **** ***** s="" for i in range(5): s+= "*" print(s) else: print("Goodbye")
  • 7. NESTED L p Python programming language allows the use of one loop inside another loop. The following section shows a few examples to illustrate the concept. FOR LOOP for iterating_val in sequence: for iterating_val in sequence: statement(s) statement(s) WHILE LOOP while expression: while expression: statement(s) statement(s)
  • 8. Example: Printing Multiplication tables from 1 to 10 for i in range(1,11): for j in range(1,11): k=i*j print(k, end="t") print('n') n = 4 while n>0: for i in range(4): x="*t" *n print(x) n-=1 else: exit(0) Example: Printing Embroidery Star Pattern
  • 9. BREAK The break statement is used for premature termination of the current loop. After abandoning the loop, execution at the next statement is resumed, just like the traditional break statement in C. The most common use of break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops. If you are using nested loops, the break statement stops the execution of the innermost loop and starts executing the next line of the code after the block. SYNTAX: break
  • 10. EXAMPLE: Using break statement for i in range(10): print(i) if(i==5): break else: print(i)
  • 11. CONTINUE The continue statement in Python returns the control to the beginning of the current loop. When encountered, the loop starts next iteration without executing the remaining statements in the current iteration. SYNTAX: continue
  • 12. EXAMPLE: Using Continue statement n= 0 while n != 10: n += 1 if n%2 == 0: continue print(n) Affects the statements written after continue statement for i in range(1,11): for j in range(1,11): k=i*j print(k, end="t") if i%3==0: break print() EXAMPLE: BREAK in FOR - Mul. Tables
  • 13. It is used when a statement is required syntactically but you do not want any command or code to execute. The pass statement is a null operation; nothing happens when it executes. The pass statement is also useful in places where your code will eventually go, but has not been written yet i.e. in stubs). PASS SYNTAX: pass
  • 14. EXAMPLE: Using Pass in order to replace a Statement which is undecided num=1 if num<0: print("Number is Negative") elif num>0: pass else: print("Number is Zero")