While Loop

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Continue Statement

It returns the control to the beginning of the while loop.. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops. i = 0 while i < 6: i += 1 if i == 3: continue print(i) When the above code is executed, it produces the following result − 1 2 4 5 6

Break Statement

It terminates the current loop and resumes execution at the next statement, just like the traditional break statement in the programming language C. The most common use for break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops. If you are using nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block.

Else Statements with Loops

Python supports to have an else statement associated with a loop statement. If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list. If the else statement is used with a while loop, the else statement is executed when the condition becomes false. 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.

condition

The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop.

example

count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!" When the above code is executed, it produces the following result − The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye! Print i as long as i is less than 6: i = 1 while i < 6: print(i) i += 1 When the previous code is executed, it produces the following result − 12345

Example of Else Statements with Loops

count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5" 0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5

Syntax Break

i = 1 while i < 6: print(i) if i == 3: break i += 1 When the above code is executed, it produces the following result 1 2 3

statement(s)

statement(s) may be a single statement or a block of statements 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. Python uses indentation as its method of grouping statements

syntax

while expression: statement(s)


Set pelajaran terkait

Chapter 1: The Equity Method of Accounting for Investments, Chapter 2: Consolidation of Financial Information, Ch. 3 Consolidations - Subsequent to Acquisition, LS Ch. 1, LS Ch.2

View Set

Entrepreneurship and Small Business

View Set

Word Choice And Evidence In Wheels Of Change part 3

View Set

Simplifying Fractions of Place Value 1000

View Set

Introductory Astronomy and Cosmology (PHYS 202) - Chapters 13 & 14 Review

View Set

Cybersecurity Interview Questions

View Set

Chapter 21 All including Elsevier

View Set

Tyra Moore 66 years old, Felame, 5'2'', 150 lbs, Weight lost goal: 1 lb/week, Activity Level: Sedentary

View Set

Chapter @: Constitutional Democracy

View Set