Skip to Main Content
Learn to Code by Solving Problems
book

Learn to Code by Solving Problems

by Daniel Zingaro
June 2021
Beginner content levelBeginner
336 pages
7h 11m
English
No Starch Press
Content preview from Learn to Code by Solving Problems

4REPEATING CODE: INDEFINITE LOOPS

image

The for loops and range for loops that you learned in Chapter 3 are convenient for looping through a string or range of indices. But what do we do when we have no string or when the indices do not follow a fixed pattern? We use a while loop, the topic of this chapter. while loops are more general than for loops and can handle situations that a for loop cannot.

We’ll solve three problems where for loops fall short: determining the number of times slot machines can be played, organizing a song playlist until the user wants to stop, and decoding an encoded message.

Problem #8: Slot Machines

How many times can slot ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

40 Algorithms Every Programmer Should Know

40 Algorithms Every Programmer Should Know

Imran Ahmad
Coding with AI: The End of Software Development as We Know It

Coding with AI: The End of Software Development as We Know It

Tim O'Reilly, Addy Osmani, Gergely Orosz, Kent Beck, Camille Fournier, Avi Flombaum, Maxi Ferreira, Harper Reed, Jay Parikh, Birgitta Böckeler, Angie Jones, Craig McLuckie, Patty O’Callaghan, Chip Huyen, swyx, Andrew Stellman, Phillip Carter, Nikola Balic, Brett Smith, Chelsea Troy, Lili Jiang

Publisher Resources

ISBN: 9781098129033