Last active
January 1, 2016 13:39
-
-
Save NathanielWroblewski/8152346 to your computer and use it in GitHub Desktop.
Ruby while loop example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #original | |
| # DEF ask question | |
| # puts question | |
| # reply = gets.chomp.downcase | |
| # WHILE (reply != 'yes' OR reply != 'no') | |
| # puts 'Please answer with a yes or a no.' | |
| # reply = gets.chomp.downcase | |
| # END | |
| # IF (reply == 'yes') | |
| # wetsBed = TRUE | |
| # ELSE | |
| # wetsBed = FALSE | |
| # END | |
| # wetsBed | |
| # END | |
| # working examples: | |
| # using and with while | |
| def ask(question) | |
| puts question | |
| reply = gets.chomp.downcase | |
| while reply != 'yes' && reply != 'no' | |
| puts 'Please answer with a yes or a no.' | |
| reply = gets.chomp.downcase | |
| end | |
| reply == 'yes' | |
| end | |
| # using or with until | |
| def ask(question) | |
| puts question | |
| reply = gets.chomp.downcase | |
| until reply == 'yes' || reply == 'no' | |
| puts 'Please answer with a yes or a no.' | |
| reply = gets.chomp.downcase | |
| end | |
| reply == 'yes' | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment