× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

While Loop

While:
'while' is a keyword in python language which is used to execute one block of statements several number of times based on the condition.
Syntax
while(condition):
    ...
    ...
    ...
 
In the above syntax if the condition is true the statements of while block is execute infinite times, if the condition is false while block terminated :

Key Points

  • What is While Loop


Image
Display 1 to 10 Values Using While Loop



def myfun():
    x=1
    while(x<=10):
        print(x)
        x=x+1
   
if __name__=="__main__":
    myfun()

                                

Output:
1
2
3
4
5
6
7
8
9
10

Display 10 to 1 Values Using While Loop



def myfun():
    x=10
    while(x>=1):
        print(x)
        x=x-1

if __name__=="__main__":
    myfun()

                                

Output:
10
9
8
7
6
5
4
3
2
1

Nested For While In java


Nested While Loop :
One While loop within another While loop is knows as Nested While Loop Syntax

while(condition):
    while(condition):
        ...
        ...
        ...
    ...
    ...
    ...

In the above syntax :
Outer loop is Row
Inner loop is Column

Key Points

  • What is Nested While Loop


Image
***
***
***
***
***
***


def myfun():
    x=1
    while(x<=6):
        y=1
        while(y<=3):
            print("*",end="")
            y=y+1

        print(end="\n")
        x=x+1

if __name__=="__main__":
    myfun()

                                

Output:
***
***
***
***
***
***

*
**
***
****
*****
******


def myfun():
    x=1
    while(x<=6):
        y=1
        while(y<=x):
            print("*",end="")
            y=y+1

        print(end="\n")
        x=x+1

if __name__=="__main__":
    myfun()

                                

Output:
*
**
***
****
*****
******

******
*****
****
***
**
*


def myfun():
    x=1
    while(x<=6):
        y=6
        while(y>=x):
            print("*",end="")
            y=y-1

        print(end="\n")
        x=x+1

if __name__=="__main__":
    myfun()

                                

Output:
******
*****
****
***
**
*