In the code snippet above, we have implemented a while loop inside a while loop. Let’s have an example of the nested loop to understand it better. We can create multiple loops inside a while loop, in this case, the while loop will be called a nested while loop. The above code will print “GoLinuxCloud” five times.ĪLSO READ: SOLVED: Measure Execution Time in Python Example 2: Nested while loop now, when the loop runs five times, the value of i will become 5 and when it tries to run six times, the condition will now become false and the loop will end. In the while loop, we have specified a condition that if the value of i becomes less than or equal to 5, print “GoLinuxCloud”, then we are incrementing the i variable by adding 1 to it every time the loop runs. i = 1Īs you can see in the code snippet above, we have declared a variable i and assigned it a value of 1. Loops help us to implement the same thing in a code multiple times. What if I say to print it a thousand times? This is where the looping concept in any programming language will help you out. Well, you have the options, you can print this by just using the print function as shown below: print('GoLinuxCloud') Suppose we are interested to print “GoLinuxCloud” five times. We will start implementing a while loop by giving just one condition, later on, we will see what a while loop looks like with multiple conditions. ALSO READ: Python any() function Examples Ī while loop can have one or more conditions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |