Here is the syntax and example of a one-line while clause − Similar to the if statement syntax, if your while clause consists only of a single statement, it may be placed on the same line as the while header.
#PYTHON WHILE LOOP CODE#
When the above code is executed, it produces the following result − The following example illustrates the combination of an else statement with a while statement that prints a number as long as it is less than 5, otherwise else statement gets executed. If the else statement is used with a while loop, the else statement is executed when the condition becomes false. Python supports to have an else statement associated with a loop statement. Example Live DemoĪbove example goes in an infinite loop and you need to use CTRL+C to exit the program. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
Here, key point of the while loop is that the loop might not ever run. Python uses indentation as its method of grouping statements. In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. When the condition becomes false, program control passes to the line immediately following the loop. The loop iterates while the condition is true. The condition may be any expression, and true is any non-zero value. Here, statement(s) may be a single statement or a block of statements. The syntax of a while loop in Python programming language is − Here we use break statement to terminate the while loop without completing it, therefore program control goes to outside the while - else structure and execute the next print statement.A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true. In the above example the loop is terminated when x becomes 5. Print('The sum of first 9 integers : ',s)Įxample: while loop with if-else and break statement x = 1 In the following example, while loop calculates the sum of the integers from 0 to 9 and after completing the loop, else statement executes. If a break statement executes in first program block and terminates the loop then the else clause does not execute. The else clause is only executed when the condition is false it may be the first time it is tested and will not execute if the loop breaks, or if an exception is raised. The while loop repeatedly tests the expression (condition) and, if it is true, executes the first block of program statements. You can attach an optional else clause with while statement, in this case, syntax will be. The difference is that block belongs to if statement executes once whereas block belongs to while statement executes repeatedly. Both have a block of statement(s) which is only executed when the condition is true. There is a structural similarity between while and else statement. The following while loop is an infinite loop, using True as the condition: x = 10
For example the following code never prints out anything since before executing the condition evaluates to false. If the initial test returns false, the body is not executed at all. One thing we should remember that a while loop tests its condition before the body of the loop (block of program statements) is executed. The following example prints the digits 0 to 4 as we set the condition x < 5. The condition is checked every time at the beginning of the loop and the first time when the expression evaluates to False, the loop stops without executing any remaining statement(s). The while loop runs as long as the expression (condition) evaluates to True and execute the program block.