CSC108H1 Lecture 12: While Loops

89 views2 pages
15 Oct 2015
School
Course
yifanyang and 39659 others unlocked
CSC108H1 Full Course Notes
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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents