CSC108H1 Lecture 12: While Loops
yifanyang and 39659 others unlocked
21
CSC108H1 Full Course Notes
Verified Note
21 documents
Document Summary
While loops iterate over boolean expressions, similar to if statements. While loops are more flexible than for loops since they can loop many times. Unlike for loops, they check the body and executes until the loop condition becomes false. E. g a program that prints out characters demonstrates this: #only h is printed because since e is a vowel, the loop condition is false and the loop exits. E. g a program that checks number guesses would look like: >>>guess = int(input( guess a number between 1 & 20:")) >>> guess = int(input( guess a number between 1 & 20:")) >>> #while loop is checking to make sure that the input is valid. Since the int function converts guesses to integers, answers such as 11. 45 would still result in you won!" being printed. A function that skips characters in a string would look like: >>> return a string that contains every nth character from string, starting at.