Python while Loop
The formats used are used as a special code to restart the program. In this article, you will learn how to create a second time in Python.
Table of Contents
- What is while loop in Python?
- Syntax of while Loop in Python
- Flowchart of while loop
- Example: Python while Loop
- while loop with else
What is While loop in Python?
Although known as Python is used to attach the code, so long as the trial application is not true.
Usually, we use the line if we do not already know, the number of times it's time to go.
Syntax of while Loop in Python:
while test_expression:
Body of while
Through the circle, test the word to check first. The weight loss is only taken if the test scores are assessed correctly. After repeated, the exam reviews. This system continues until the test scores are evaluated in Final.
In Python, while the body of loop is determined by slapping.
The staff starts bleeding, and the line indicates the first end without having to go through.
Python interprets any other value. None of the 0 was interpreted as false.
Flowchart of while loop:
Example: Python while Loop
# Program to add natural
# numbers upto
# sum = 1+2+3+...+n
# To take input from the user,
# n = int(input("Enter n: "))
n = 10
# initialize sum and counter
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1 # update counter
# print the sum
print("The sum is", sum)
Once the program is implemented, production will be:
Enter n: 10
The sum is 55
The above test feature is always true that our table is less than or equal to n (10 our programs).
We must increase the cost of change in the design process. This is very important (often forgotten). Failure to do so will result in the rounded boundary (without the circle).
The final result is shown.
while loop with else:
In the same way, we can have the option of another type.
The other is carried out when the timely delivery criteria are assessed.
Although the prosecution may be terminated. For example, for example, in the other, was ignored. Therefore, the other way to go if they do not break and the conditions are false.
Below is a simple example of this.
# Example to illustrate
# the use of else statement
# with the while loop
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")
Once the program is implemented, production will be:
Inside loop
Inside loop
Inside loop
Inside else
Here, we use the variable to mark the triple stroke three times.
On the back, when that is the case. Therefore, the other part is carried out.
Comments
Post a Comment