Looping Statements and Control Statements in Python

2,731 views 33 slides Feb 10, 2020
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

LOOPING STATEMENTS: WHILE, FOR, NESTED LOOP CONTROL STATEMENTS: BREAK, CONTINUE, PASS;


Slide Content

LOOPING STATEMENTS AND CONTROL STATEMENTS IN PYTHON Ms.C.PRIYANKA AP/CSE KIT-KALAIGNARKARUNANIDHI INSTITUTE OF TECHNOLOGY , COIMBATORE

LOOPING STATEMENTS: WHILE , FOR , NESTED LOOP CONTROL STATEMENTS: BREAK, CONTINUE, PASS;  

LOOPING STATEMENTS: WHILE, FOR, NESTED LOOP While loop For Loop Nested Loop

While loop statement in Python is used to repeatedly executes set of statement as long as a given condition is true. In while loop, test expression is checked first. While loop

The body of the loop is entered only if the test_expression is True. After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False. In Python, the body of the while loop is determined through indentation. The statements inside the while starts with indentation and the first unindented line marks the end.

Syntax

Flowchart

Examples: Program to find sum of n numbers: Program to find factorial of a number Program to find sum of digits of a number: Program to Reverse the given number: Program to find number is Armstrong number or not Program to check the number is palindrome or not

For loop for in range: We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10 numbers). In range function have to define the start, stop and step size as range( start,stop,step size). step size defaults to 1 if not provided. Syntax :

Flowchart

For in sequence The for loop in Python is used to iterate over a sequence ( list, tuple, string ). Iterating over a sequence is called traversal. Loop continues until we reach the last element in the sequence. The body of for loop is separated from the rest of the code using indentation

Sequence can be a list, strings or tuples:

Examples: print nos divisible by 5 not by 10 Program to print fibonacci series Program to find factors of a given number check the given number is perfect number or not check the no is prime or not Print first n prime numbers Program to print prime numbers in range

NESTED LOOPS Python programming language allows using one loop inside another loop. We can use one or more loop inside any another while, for or do...while loop .

else statement in loops else in for loop : If else statement is used in for loop, the else statement is executed when the loop has reached the limit. The statements inside for loop and statements inside else will also execute

else in while loop : If else statement is used within while loop , the else part will be executed when the condition become false. The statements inside for loop and statements inside else will also execute

CONTROL STATEMENTS BREAK Break statements can alter the flow of a loop. It terminates the current loop and executes the remaining statement outside the loop. If the loop has else statement, that will also gets terminated and come out of the loop completely .

Syntax:

Flowchart

CONTINUE It terminates the current iteration and transfer the control to the next iteration in the loop . Syntax: Continue

Flowchart

PASS It is used when a statement is required syntactically but you don’t want any code to execute. It is a null statement, nothing happens when it is executed.   Syntax:   pass break

Difference between break and continue
Tags