While Loops In Python and It’s Example

0
9846
While Loops In Python and It’s Examples

Introduction To While Loops In Python

It is very frustrating when working with repetitive code and repetitive commands also it is very hard for programmers to counter this problem ‘While Loop’ In python is most useful it will run a set of execution codes as long as possible until the condition is true. Python programming allows users to use loops and conditional statements to overcome this hurdle. This blog will help you to a better understanding of loops in python programming, types of loop, and its syntax.

What Are Loops In Python?

In simple words loops in python can be used to run a group of statements multiple times. And with the help of using conditional statements, Suppose we calculate the mathematical terms and perform it iteratively then loops are used. So loops are saved your time to code.

while Loops In python

What is a while loop In Python?

The ‘while’ loop executes the set of conditional statements as long as the condition is true.
It consists of conditions and the code means a body with the set of statements, It will keep on iterate the statement until the condition becomes false. There is no guarantee to examine how long the loop will keep iterating.

Example:

i = 1
 while i < 5:
     print(i)
     i = i+1

Output:

1
 2
 3
 4

In the above example, the while loops will iterate till the condition gets false ( in this case I< 5 ). Once the value of I gets less than 5, then the condition fails, therefore control comes out of a while loop.

While and for loop have operations of Incrementer and decrementer operators


What are loop control statements?

In the While Loop, there are some statements that alter the execution of while loop-based on some conditions.
There are three control statements

  1. Break
  2. Continue
  3. Pass

Widget not in any sidebars

Break:

A break statement is used to break the execution of the loop containing it. As soon as the loop comes across a break statement, the loop end, and the execution transfers to the next statement in the loop.
As in the flowchart, the execution moves to the part statement below the loop, and when the break returns True.

Flowchart of including break statement:

Example:

i = 1
 while i < 6:
    print(i)
    if i == 3:
        break
    i += 1

Output:

1
 2
 3

The above code only executes till i == 3 then the loop is a break.


Continue:

The Continue Statement is used to Skip the operation in the loop for the current iteration. It does not end the loop like a break statement and continues with another operation. we used For loop to continue the iteration.

Example: Pass String into for loop and add continue statement in if loop

#while loop using continue statement

 i = 10                    # Second Example
  while i > 0:              
    i = i -1
    if i == 5:
       continue 
    print ('Current value :', i)
  print ("Loop End")

Output:

Current value : 9
 Current value : 8
 Current value : 7
 Current value : 6
 Current value : 4
 Current value : 3
 Current value : 2
 Current value : 1
 Loop End

In the above example, when i=” A”, it will continue the loop and skip printing “A”.


Pass:

Python pass statement is a null statement. There is a difference between a comment and a python pass statement in Python is that while the interpreter ignores a comment entirely, pass is not ignored.
However, nothing happens when the Python pass is executed in code. It results in no operation (NOP).

Example:

# while loop using pass statement
 i = 0

 while i < 10:
           i += 1
           if i == 5:
                 pass
                 print(' This is pass blog')
          Print(' current value : ',i)

Output:

Current value : 1
 Current value : 2
 Current value : 3
 Current value : 4
 This is pass blog
 Current value : 5
 Current value : 6
 Current value : 7
 Current value : 8
 Current value : 9
 Current value : 10

 
Widget not in any sidebars

Conclusion

In this blog, Cover While loop and Statements in python To Get A better understanding of loops and its syntax.

LEAVE A REPLY

Please enter your comment!
Please enter your name here