A While Loop Will Continue Until the Loop Condition is

In this Python tutorial, we will discuss the Python While loop condition. Here we will also cover the below examples:

  • Python while loop condition at the end
  • Python while loop without condition
  • Python while loop string condition
  • Python while loop if condition
  • Python while loop exit condition
  • Python while loop double condition
  • Python while loop check condition
  • Python assignment in while loop condition
  • Python while loop break condition
  • Python while loop boolean condition

Python while loop condition

  • Let us see how to use the while loop condition in Python.
  • This statement is used to construct loops.
  • The condition is prepared to check if it's True or false.
  • If the while condition is true, the statements that represent the loop are executed.
  • When the condition represents false, the loop condition will stop and the programm continues beyond the loop.

Syntax:

Here is the Syntax of the while loop condition

          while expression:    statement(s)        

Example:

Let's take an example and check how to use them while loop condition in Python

          x = 4  while x < 8:     print(x)     x += 1        

In the above code, we write this while the loop with condition x is less than 8 (x<8). The loop completes three ways and it stops when x is equal to 8.

Here is the Screenshot of the following given code

Python while loop condition
Python while loop condition

Another example to check how to use while loop condition in Python

Example:

          y = 0 while (y < 4):          y = y + 1     print("George")     print()  b = [9, 5, 2, 1] while b:     print(b.pop())        

Here is the output of the following given code

Python while loop condition method
Python while loop condition method

Read: Python dictionary length

Python while loop condition at the end

  • Here we can see how to use them while loop condition at the end.
  • In this example we can easily use the continue statement method that will always return the control to the beginning of the loop.

Example:

          x = 0 b = 'JohnMIcheal'   while x < len(b):      if b[x] == 'e' or b[x] == 'c':          x += 1         continue     print('Present :', b[x])     x += 1        

Here is the execution of the following given code

Python while loop condition at the end
Python while loop condition at the end

Python while loop string condition

  • Let us see how to use them while loop string conditions in Python.
  • First, we initialize an empty string which is used to take in input from the user inside the while loop.
  • In this example, while is followed by the variable 'name'. Here we can see if the variable 'name' is set to the string "name".
  • Here we can check, if the user inputs the string "name", then the loop will stop and the code will execute outside of the loop.
  • Inside of the while loop condition the code runs a print statement that produces the "name". Then the variable "name" is set to the user's input.

Example:

Let's take an example and check how to use them while loop string conditions in Python

          name = ''  while name != 'name':     print('What is your name?')     name = input()  print('Yes, the name is ' + name + '. You may enter.')        

Here is the implementation of the following given code

Python while loop string condition
Python while loop string condition

Another example to check how to use while loop string conditions in Python

In this example, we have to calculate the capital variable 't' after that one year including the interest. This problem will be solved by using the while loop condition.

          a = int(input("new capital ")) b = int(input("Interest rate ")) c = int(input(" years "))  x = 0 while x < c:     a +=  a * b / 100.0          x += 1     print(x, a) print("Capital after " + str(c) + " ys: " + str(a))        

Here is the Screenshot of the following given code

PYthon while loop string condition
Python while loop string condition

Read: Create Python Variables

Python while loop if condition

  • Here we can see how to use the if condition in the while loop.
  • In this example we can easily use the continue statement that terminates the current loop and it returns the control to the top of the loop.

Example:

          x = 0 b = 'Java is a complex programming language'    while x < len(b):      if b[x] == 'o' or b[x] == 'a':          x += 1         continue     print('str_ :', b[x])     x += 1        

In the above code, we have to print all letters except 'o' and 'a'.

The Screenshot of the given code is as follows.

Python while loop if condition
Python while loop if condition

Another example to check how to use the if condition in the while loop

In this example, we can use the if statement to check the condition and the break keyword is used to go out of the while loop.

Example:

          m = 2 n = 2 while (m<8):   print ('Iterate_value:',m)   m = m + 1   m = m + 1   if (m == 6):     break print ('loop will terminated')        

The output of the following code is given below.

Python while loop if condition
Python while loop if condition

Read: Python Tkinter Image

Python while loop exit condition

  • Let us see how to exit while loop condition in Python.
  • In this example we will match the condition only when the control statement returns back to it.
  • By using break statement we can easily exit the while loop condition.
  • This statement terminates a loop entirely.

Example:

Let's take an example and check how to exit while loop condition in Python

          a = 4 while a > 0:     a -= 1     if a == 2:         break     print(a) print('while Loop end.',a)        

Here is the execution of the following given code.

Python while loop exit condition
Python while loop exit condition

Python while loop double condition

  • Here we can see how to use double conditions in the while loop by if-else method.
  • In this example the loop repeated until the condition was consumed.
  • If-else condition is used in while loop.

Example:

Let's take an example and check how to use double conditions in the whole loop

          b = 6 while b > 0:     b -= 1     print(b)     if b == 2:         break else:     print('Loop double condition.')        

The execution of the following given code is as follows.

Python while loop double condition
Python while loop double condition

Read: Python NumPy to list

Another example to check how to use double condition in while loop

In this example, we can combine two conditional expressions into one loop. To perform this task we can use logical operators.

Example:

          import random m = 2 n = 8 max_val = random.randint(0,10)  cou_new = 0 while cou_new < m or cou_new < n and not cou_new >= max_val:     print(f"count: {cou_new}, a: {m}, b: {n}")     cou_new += 1        

Here is the Screenshot of the following given code

Python while loop double condition method
Python while loop double condition method

Python while loop check condition

  • Let us see how to check while loop condition in Python.
  • In while loop, the control will move inside the loop only when this while condition is true and when it is true it will execute the body of the loop and then again it will go back and see whether the condition is still true or not if it is still true then again it will execute the body of the loop.

Example:

Let's take an example and see how to check while loop condition in Python

          m = 2  while(m <= 8): 	print(m) 	m = m+1        

In this example, we will print the numbers from 2 to 8. You can see in the above code the loop will only run if m is less than or equal to 8.

The implementation of the given code is as follows.

Python while loop check condition
Python while loop check condition

Python assignment in while loop condition

  • Here we can see how to use assignment expression in while loop condition.
  • Assignment expression is written with (:=). This assignment expression allows us to collapse more context into the loop condition and returns a value in the same expression.
  • In this example, we take a user input function within the while loop.
  • The assignment text:= input("Enter the name: ") attaches the value of text to the value rectify from the user inside the input function.

Example:

Let's take an example and check how to use assignment expression in the while loop condition

          while (text := input("Enter the name: ")) != "end":     print("Receive textname:", text)        

Here is the Screenshot of the following given code

Python assignment in while loop condition
Python assignment in while loop condition

Read: Python NumPy where

Python while loop break condition

  • Let us see how to use break statement in while loop condition.
  • In Python the break statement terminates a loop iteration. Control of the programm execution further proceeds to the statement after the loop body.
  • If the break statement is within a nested loop then the break statement will execute the inner part of the loop.

Example:

Let's take an example and check how to use break statement in while loop

          p = 6 while p > 0:     p -= 1     if p == 3:         break     print(p)        

Here is the output of the following given code.

Python while loop break condition
Python while loop break condition

Another example to check how to use break statement in while loop

In this example we can break the loop as soon it sees 'u' and 'a'

Example:

          z = 0 y = 'Australia Germany'    while z < len(y):      if y[z] == 'u' or y[z] == 'a':          z += 1         break     print('present Letter :', y[z])     z += 1        

The implementation of the following code is given below.

Python while loop break condition method
Python while loop break condition method

Python while loop boolean condition

  • To check the boolean expression in while loop condition we can simply use while if condition.
  • It checks if the 'a' variable is true and the loop will exit after 3 iteration values (n=n+1) must be terminated 3 times until n==4.

Example:

Let's take an example and check how to use while loop condition in case of a boolean expression

          a = True n = 1  while a:     n=n+1     print(a)     if n == 4:         a = False        

Here is the implementation of the following given code.

Python while loop boolean condition
Python while loop boolean condition

You may like reading the following articles.

  • Python NumPy concatenate
  • Python sort NumPy array
  • Python NumPy matrix
  • Python dictionary remove
  • Python while loop continue
  • For loop vs while loop in Python

In this Python tutorial, we have discussed the Python While loop condition. Here we have also covered the following examples:

  • Python while loop condition at the end
  • Python while loop without condition
  • Python while loop string condition
  • Python while loop if condition
  • Python while loop exit condition
  • Python while loop double condition
  • Python while loop check condition
  • Python assignment in while loop condition
  • Python while loop break condition
  • Python while loop boolean condition

brunettitrummunded.blogspot.com

Source: https://pythonguides.com/python-while-loop-condition/

Belum ada Komentar untuk "A While Loop Will Continue Until the Loop Condition is"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel