Lesson 14. Making more-complicated decisions

 

After reading lesson 14, you’ll be able to

  • Combine many decisions in one conditional statement
  • Make a choice when presented with various options
  • Write code that gets the computer to decide between a few choices

It’s limiting and time-consuming if every decision you make is the result of asking only one question at a time. Say you want to buy a new phone. There are only three phones that you’re considering, but you’re not sure how much money you have in your bank account. Additionally, one other criteria is that the phone is available in green. Using yes or no questions, you could ask the following:

  • Do I have between $400 and $600?
  • Do I have between $200 and $400?
  • Do I have between $0 and $100?
  • Does Phone 1 come in green?
  • Does Phone 2 come in green?
  • Does Phone 3 come in green?

Because you have more than one condition you want to check, you can combine two (or more) together. For example, you could ask, “Do I have between $400 and $600, and does Phone 1 come in green?”

14.1. Combining multiple conditions

 

14.2. Choosing which lines to execute

 
 
 
 

Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage