Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Stotts Computer Science Department UNC Chapel Hill.

Similar presentations


Presentation on theme: "David Stotts Computer Science Department UNC Chapel Hill."— Presentation transcript:

1 David Stotts Computer Science Department UNC Chapel Hill

2 My Mom said, “Dear, please go to the store and get a bottle of milk. If they have eggs, bring a dozen.” I came home and put her 12 bottles of milk on the table. “What in the world …” She was truly confused. “Why did you bring 12 bottles of milk??” “Because… THEY HAD EGGS !” Programmers loose in the Real World

3 Use parentheses to be clear and unambiguous Also, watch rules in making boolean conditions if (x == 100 || 200) { … } wrong What happens here? Try it … if (x == 100 || x == 200) { … } correct Watch out for mistakes like this if (x = 100) { … } wrong assignment, but you meant “==“ equality if (x == 100) { … } correct

4  Programs should be robust … meaning they should be written to survive mistakes as best they can  Robust means not delicate … we don’t want a program to just quit or crash due to problems we could have anticipated and perhaps repaired or remedied.  One area we will check is user input data Users can, and will, make mistakes  When asking for input, data from a user must be examined to make sure it matches the expected type, format and range

5 When asking for input, data from a user must be examined to make sure it matches the expected type, format and range var num = prompt (“give an int between 1 and 100”) ; What can go wrong? ◦ Type: user might give “five” or “what?” or just a blank (return key) ◦ Format: user might type “34.2” ◦ Range: user might type “105”, or “-12”

6 What can we do about it? Write conditionals after the prompt to check for the various possible problems If problems are detected, then we either  Alert the user to the error and quit gracefully  Alert the user to the problem and ask for new input (a loop)  Alert the user to the error, then make correct data out of the bad data and continue the program (and be sure to inform the user what you did)

7 Code examples This is a good use for functions, our next item in the big six


Download ppt "David Stotts Computer Science Department UNC Chapel Hill."

Similar presentations


Ads by Google