CSE 1010 Lecture 7: CSE1010 Lecture 7 - While Loops
20 views2 pages
Verified Note
26 Sep 2018
School
Department
Course
Professor
CSE 1010 verified notes
7/12View all
Document Summary
While loop: while loops are a way to repeat statements as long as a condition is true. While loop format: while condition: statements increment variable. It is very important to increment the variable that you are using. If the increment is missing, the loop will continue forever. If the condition is true, run the statement. The statement will continue to run as long as the condition is true. If the condition is false, exit the loop. While n<=5 print(n) n = n+1 #this statement increments n by 1. Note that you exit the loop when n = 5 and n remains being 5, not 6. Break statement: with the break statement the loop can be stopped even when the condition is true. Example: n = 1 while n<8 print(n) if n==3 break #exit the loop when n=3 n = n+1. Continue statement: with the continue statement the current iteration can be stopped, and continue with the next.