Loop exercises

Use these loop patterns for guidance.

  1. Read input line-by-line, and when the input ends, print the line that comes first lexicographically.
  2. Read input line-by-line, and when the input ends, print the line that comes first lexicographically; if there were no input lines, print "no input" instead.
  3. Read input line-by-line, and when the input ends, print the lines that come first and last, lexicographically; if there were no input lines, print "no input" instead, and if there is only one input line, print it only once.
  4. Read input line-by-line, and when the input ends, print the count of how many lines were entered.
  5. Read input line-by-line, and when the line is "done", print "done" and stop.
  6. Loop forever, without input or output, until the program is killed.
  7. Read input line-by-line, printing a prompt before each one, and when the line is "done", print "done" and stop.
  8. Read input line-by-line, and when the line is "done", print "done" and quit; if the input ends and no line was "done", print "no more lines" instead.
  9. Read input line-by-line, printing a prompt before each one, until the user enters a number; whenever the input is not a number, print an error message before prompting again.
  10. Create a guessing game, in which the computer secretly chooses a random natural number less than 10. Read input line-by-line, prompting before each one; if the input is not a number, print an error message; if the input is a number but not in the range of possible choices, print a different error message; if the input is the correct number, print a count of how many wrong guesses were made (counting only numbers in range) and stop.

(On the UNIX console, the user can signal the end of the input by typing control-d, and can kill the program by typing control-c.)