Chapter 5
Repeating Commands by Looping
IN THIS CHAPTER
Looping a fixed number of times
Looping zero or more times
Looping at least once
Working with nested loops
Exiting early from a loop
Examining your loops
To write any program, you must specify what the computer needs to do at any given time. Sometimes, you may need to write the same command multiple times. For example, suppose you want to print your name five times. You could just write the same command five times like this:
PRINT "John Smith"PRINT "John Smith"PRINT "John Smith"PRINT "John Smith"PRINT "John Smith"
Writing the same five commands is cumbersome. Even worse, what if you suddenly decide you want to print your name not just 5 times, but 5,000 times? Do you really want to write the same command 5,000 times?
Probably not, which is why computer scientists invented loops. A loop is just a shortcut for making the computer ...
Get Beginning Programming All-in-One For Dummies, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.