![]() Since the condition is always False, the instructions in the loop's body don't execute. ![]() It's possible that the while loop never runs if it doesn't meet the condition, like in this example: number = 50 This time Number is 9! is printed and number is incremented, but now number is equal to 10 so the condition is no longer met and therefore the loop is terminated. The syntax of the while loop is very similar to the if statement, as you can see above. However, as opposed to the if statement, the while loop continues to execute the code repeatedly as long as the condition is True. The condition is re-evaluated and it is again True, so the whole procedure repeats until number is equal to 9. The while loop is somewhat similar to an if statement, it executes the code inside, if the condition is True. It evaluates to True so the print statement gets executed and Number is 0! is printed to the console. Here, the variable number is set to 0 initially.īefore any code is run, Python checks the condition ( number < 10). If the condition evaluates to True then the loop will run the code within the loop's body.įor example, this loop runs as long as number is less than 10: number = 0 It will keep executing the desired set of code statements until that condition is no longer True.Ī while loop will always first check the condition before running. The general syntax of a while loop in Python looks like this: while condition:Ī while loop will run a piece of code while a condition is True. So, the above code will give us the following output.ADVERTISEMENT What is a while loop in Python? Otherwise, we will print the value of x and repeat. ![]() If it is 7 then we will skip the printing part. Inside the body of the while loop we are first increasing the value of x by 1. In the above code we are starting by initialising x = 0. If the number is 7 then we are skipping it using the continue statement. In the following Python program we are printing integer number from 1 to 10. We use the continue keyword to skip the current iteration and move to the next. When the value of x = 7 then the break statement will be executed and we will jump out of the while loop body.įollowing is the output of the above code. In the following Python program we are going to print integer numbers from 1 to 10 but we will jump out of the while loop when we encounter number 7. ![]() We use the break keyword to come out of the while loop body even if the condition of the loop is satisfied. In the following Python code we are printing only the odd numbers between 1 to 20 both inclusive. When the value of x becomes 11 the condition fails so we come out of the while loop. Then we are incrementing the value of x by 1.Īfter incrementing the value of x we are again checking the loop condition and repeating the body. Inside the body of the while loop we are printing the value of x. So, as long as value of x is less than or equal to 10 we are going to execute the body of the loop. We start by initialising the variable x = 1.įor the condition we are checking is x <= 10. The above code will print the following output. In the following Python program we are printing integer number 1 to 10 using while loop. Inside the body of the while loop we keep updating the condition of the loop so that we can come out of it. Where, condition is some condition and if it is satisfied then the body of the while loop is executed otherwise, it is ignored. We use the while keyword to create a while loop in Python. So, to solve this task we take help of a loop that executes the print statement for us for 1000 or N number times. You can write the print statement 1000 times which we both know is very tiring and not the best way to solve the problem. Imagine you have to print "Hello World" text 1000 times. We use loop to complete repeating task with ease. In this tutorial we will learn about while loop in Python.Ī loop is a block of code that gets executed over and over again as long as the given condition is satisfied.
0 Comments
Leave a Reply. |