Skip to main content

Python break statement

break statement

The break statement is used to exit a for or a while loop. The purpose of this statement is to end the execution of the loop (for or while) immediately and the program control goes to the statement after the last statement of the loop. If there is an optional else statement in while or for loop it skips the optional clause also. Here is the syntax.
Syntax:
 
     while (expression1) :
     statement_1 
     statement_2
     ......
     if expression2 :
     break
for variable_name in sequence :
   statement_1 
   statement_2
   if expression3 :
     break
Example: break in for loop
In the following example for loop breaks when the count value is 5. The print statement after the for loop displays the sum of first 5 elements of the tuple numbers.
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple
num_sum = 0
count = 0
for x in numbers:
    num_sum = num_sum + x
    count = count + 1 
    if count == 5:
        break
print("Sum of first ",count,"integers is: ", num_sum)

Output:
Sum of first  5 integers is:  15

Example: break in while loop

In the following example while loop breaks when the count value is 5. The print statement after the while loop displays the value of num_sum (i.e. 0+1+2+3+4).
num_sum = 0
count = 0
while(count<10):
    num_sum = num_sum + count
    count = count + 1 
    if count== 5:
        break
print("Sum of first ",count,"integers is: ", num_sum)

Output:
Sum of first  5 integers is :  10

Comments

Popular posts from this blog

Introduction of SQL

Introduction of SQL SQL (Structure Query Language)  was initially developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s. This version, initially called SEQUEL (Structured English Query Language), was designed to manipulate and retrieve data stored in IBM's original quasi-relational database management system. Features of SQL SQL is not a case sensitive language. Every commands in SQL should ends with semicolon(;). SQL can also called as sequel (SEQUEL). SQL Sub Language SQL is mainly divided into four sub language Data Definition Language(DDL) Data Manipulation Language(DML) Transaction Control Language(TCL) Data Control Language(DCL) Only for remember You can remember all these command like below; DDL Commands "dr. cat" d-Drop, r-Rename, c-Create, a-Alter, t-Truncate. DML commands "sudi". s-Select, u-Update, d-Delete, i-Insert.

Text Formatting

• Text formatting tags modify the text between the opening tag and the closing tag – Ex. <b>Hello</b> makes “Hello” bold

css introduction

What is CSS? •Stands for Cascading Style Sheet •CSS allows you to style HTML elements, or define how they are displayed. •Styles are normally stored in Style Sheets •External Style Sheetscan save you a lot of work •External Style Sheets are stored in CSS files •Multiple style definitions will cascadeinto one •Examples: –Changing the color of text –Specifying the size and position of a div What is CSS? (cont.) •CSS is an alternative to the “style” attribute. – <div style=“height:500px; width:200px”></div> •Advantages: –Can control the style of many different elements with a single command. –Separation of function: HTML can focus on content while CSS handles styling. CSS (Cascading Style Sheet) •Simple mechanism for adding style to web page •Code be embedded into the HTML file •HTML tag: <style type=“text/css”>CODE</style> •Also be in a separate file FILENAME.css •HTML tag: <link rel=“stylesheet” href=“scs.css” type=“text/css”> •St...