GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — break statement in Python
break statement in PythonThis tutorial will go over the second most commonly used conditional statement in Python, "break." Here I've provided you with all the details about the "break" keyword or statement, along with some simple examples. This article deals with: What is a "break" statement?The "break" statement in Python is used to exit a loop. In other words, we use the "break" keyword to terminate the remaining execution of the whole or complete loop in its indentation. Don't worry about the definition; you'll get to know everything about it after understanding the examples given below. Syntax of the break statementThe syntax to use the "break" keyword is only the keyword itself. Therefore, its syntax is: break That's it. We do not need anything other than the keyword itself. Examples of a break statementNow let's have a look at some of the examples of the "break" keyword or statement. Let's start with the "break" statement in the while loop example. break statement in the while loopThis program contains a break inside the while loop. count = 0 while True: count = count+1 if count>10: break print(count) This program produces the output shown in the snapshot given below: As you can see from the above output, when the variable "count" becomes greater than 10, That is, when the value of "count" becomes 11, then the condition "count>10" evaluates to be "True," so the program flow goes inside the if's body and the statement "break" gets executed, which leaves the loop for further execution or simply terminates the remaining execution of the "while" loop. Let's take another example of the "break" keyword or statement that helps a lot in understanding the benefit of using "break" in Python: num = 2 while True: mul = 1 print("\n-----Table of", num, "-------") while mul<=10: print(num, "x", mul, "=", num*mul) mul += 1 print("\nWant to continue printing Table ? (y/n) ", end="") confirm = input() if confirm == 'n': break num += 1 This is the initial output produced by the above program: After printing table 2, the program prompts the user to choose whether to print the next table or skip or stop printing that table. To confirm, enter y to print the next table; otherwise, enter n to stop. For example, the output given below shows what is produced after providing y as input: Since the condition of the while loop is given as true, that always evaluates to true, and the execution of the loop never ends until and unless you use the break keyword. And to do this, we have to enter n as input, so that the condition confirm=="n" evaluates to be True and the program flow goes inside the if's body and executes the break keyword, which helps in exiting or leaving the loop, as shown in the sample run given below: break statement in the nested while loopThis program uses the break keyword in a while loop present inside another while loop: count = 0 while count<10: count = count+1 while count<5: if count==3: break count = count+1 print(count) This program produces the following output: The following is how the above program is run:
break statement in the for loopThe program given below uses a break statement in a for loop: for i in range(10000000): if i==5: break print(i) Here is its sample output: As you can see, using the break statement, I've stopped the remaining execution of the loop when the value of i becomes equal to 5. Let's take another example that shows you the most important use of the break statement: nums = list() print("Enter 5 numbers: ") for i in range(5): nums.append(int(input())) print("\nEnter a number to search: ") n = int(input()) for i in range(5): if n==nums[i]: print("\nItem found at Position:", i+1) break Here is its sample run with user inputs: 11, 22, 33, 44, 55 as five numbers, and 22 as the number to search for: Note: The break keyword helps a lot to prevent the unwanted execution of codes. As you can see from the above program, when our work was completed, i.e. when the element was discovered, then we don't need to execute or search for any further elements. Therefore, using the break keyword, we simply exit from the loop. That's it. « Previous Tutorial Next Tutorial » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |