Skip to Main Content
Learn to Program, 2nd Edition
book

Learn to Program, 2nd Edition

by Chris Pine
March 2009
Intermediate to advanced content levelIntermediate to advanced
194 pages
4h
English
Pragmatic Bookshelf
Content preview from Learn to Program, 2nd Edition

Looping

Often, you’ll want your computer to do the same thing over and over again. After all, that’s what they’re supposed to be good at doing.

When you tell your computer to keep repeating something, you also need to tell it when to stop. Computers never get bored, so if you don’t tell it when to stop, it won’t.

We make sure this doesn’t happen by telling the computer to repeat certain parts of a program while a certain condition is true. It works the way if works:

input = ​''
while​ input != ​'bye'
puts input
input = gets.chomp
end
puts ​'Come again soon!'
<= 
=> Hello?
<= Hello?
=> Hi!
<= Hi!
=> Very nice to meet you.
<= Very nice to meet you.
=> Oh...how sweet!
<= Oh...how sweet!
=> bye
<= Come again soon!

It’s not a fabulous program, ...

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

Learn to Program, 3rd Edition

Learn to Program, 3rd Edition

Chris Pine
The Book of Ruby

The Book of Ruby

Huw Collingbourne

Publisher Resources

ISBN: 9781680500172Errata