March 2009
Intermediate to advanced
194 pages
4h
English
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, ...