cs 162 intro to programming ii
play

CS 162 Intro to Programming II Debugging 1 - PowerPoint PPT Presentation

CS 162 Intro to Programming II Debugging 1 Programming Errors Syntax errors Misuse of C++ language How are they caught? Logic errors


  1. CS ¡162 ¡ Intro ¡to ¡Programming ¡II ¡ Debugging ¡ 1 ¡

  2. Programming ¡Errors ¡ • Syntax ¡errors ¡ – Misuse ¡of ¡C++ ¡language ¡ – How ¡are ¡they ¡caught? ¡ • Logic ¡errors ¡ – Doesn’t ¡perform ¡task ¡correctly ¡(aka. ¡bugs) ¡ – How ¡are ¡they ¡caught? ¡ • RunJme ¡errors ¡ – Stops ¡your ¡program ¡from ¡running ¡ – How ¡are ¡they ¡caught? ¡ 2 ¡

  3. Syntax ¡Error ¡Examples ¡ • Missing ¡main ¡funcJon ¡ • Use ¡of ¡idenJfier ¡not ¡declared ¡ • Misspelled ¡Words ¡ • Forget ¡a ¡Semicolon ¡ • Forget ¡Required ¡Keyword ¡ • Missing ¡quote, ¡curly ¡brace, ¡and ¡parenthesis ¡ • Use ¡of ¡single ¡quotes ¡instead ¡of ¡double ¡ 3 ¡

  4. Logic ¡Error ¡Examples ¡ • Poorly ¡wriTen ¡programs ¡ – Add ¡instead ¡of ¡subtract ¡(incorrect ¡operaJon) ¡ – Using ¡last ¡two ¡digits ¡for ¡date ¡ – Same ¡error ¡message ¡for ¡different ¡errors ¡ – Program ¡that ¡never ¡ends ¡ – Add ¡one ¡to ¡the ¡largest ¡integer ¡(could ¡be ¡syntax) ¡ 4 ¡

  5. RunJme ¡Error ¡Examples ¡ • Open ¡a ¡file ¡that ¡doesn’t ¡exist ¡ • SegmentaJon ¡fault ¡ – Infinite ¡loop ¡that ¡eats ¡memory ¡ – Divide ¡by ¡variable ¡that ¡is ¡zero ¡ 5 ¡

  6. Debugging ¡Process ¡ • The ¡key ¡is ¡to ¡localize ¡the ¡problem ¡ • You ¡need ¡to ¡find ¡where ¡the ¡problem ¡starts ¡ • Fix ¡one ¡error ¡at ¡a ¡Jme, ¡usually ¡starJng ¡with ¡ the ¡first ¡reported ¡compiler ¡error ¡ ¡ • Fix ¡one ¡error ¡at ¡a ¡+me ¡ 6 ¡

  7. Localizing ¡the ¡Error ¡ • Syntax: ¡ – READ ¡compiler ¡errors ¡ (pay ¡aTenJon ¡to ¡line ¡#) ¡ – Use ¡ google ¡to ¡search ¡for ¡error ¡ • Logic/RunJme ¡ – Use ¡ std::cout ¡ to ¡find ¡where ¡the ¡code ¡is ¡breaking ¡ • Print ¡variable ¡values ¡ • Print ¡indicator ¡messages ¡ – Trace ¡through ¡the ¡code ¡ – Comment ¡out ¡suspicious ¡code ¡ 7 ¡

  8. Error ¡Handling ¡ • What ¡can ¡we ¡do ¡to ¡prevent ¡these ¡errors? ¡ – Overflow ¡ – Divide ¡by ¡zero ¡ • Overflow-­‑ ¡ ¡ – Check ¡if ¡full ¡before ¡adding ¡anything ¡ ¡ – You ¡can’t ¡put ¡13 ¡eggs ¡in ¡a ¡carton ¡made ¡for ¡a ¡dozen ¡ • Divide ¡by ¡zero-­‑ ¡ ¡ – Check ¡if ¡zero ¡before ¡using ¡ ¡ – Check ¡input ¡and ¡don’t ¡accept ¡a ¡0 ¡ ¡ ¡ 8 ¡

  9. DecomposiJon ¡ • Helps ¡localize ¡by ¡working ¡on ¡small ¡porJons ¡ ¡ – For ¡each ¡new ¡porJon ¡of ¡code, ¡you ¡know ¡where ¡the ¡ error ¡is! ¡ ¡ ¡ • Techniques ¡you ¡can ¡use ¡to ¡segment ¡your ¡code: ¡ – Use ¡stubs ¡as ¡placeholders ¡ ¡ – Use ¡drivers ¡to ¡fill ¡in ¡unwriTen ¡secJons ¡for ¡tesJng ¡ ¡ 9 ¡

  10. Stubs ¡ • Aher ¡you ¡do ¡decomposiJon ¡you ¡start ¡with ¡ high ¡level ¡structure ¡ • For ¡each ¡future ¡funcJon ¡you ¡write ¡a ¡stub ¡ • Stub-­‑ ¡placeholder ¡to ¡demonstrate ¡flow ¡of ¡ control ¡ – May ¡be ¡just ¡an ¡output ¡statement ¡ – As ¡program ¡is ¡developed ¡it ¡may ¡return ¡‘sample’ ¡ data ¡to ¡test ¡other ¡funcJons ¡ ¡ • Eventually ¡replaced ¡by ¡full ¡funcJon ¡ 10 ¡

  11. Stub ¡Example ¡ do ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cout ¡<< ¡"Please ¡enter ¡1 ¡or ¡2 ¡or ¡9 ¡to ¡exit ¡\n"; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cin ¡>> ¡x; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡switch( ¡x ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡1: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cout ¡<<"OpJon ¡one" ¡<< ¡endl; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡2: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cout ¡<<"OpJon ¡two" ¡<< ¡endl; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡9: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cout<<"Bye ¡Bye!" ¡<< ¡endl; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡default: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cout ¡<< ¡"Please ¡enter ¡a ¡valid ¡number" ¡<< ¡endl; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡} ¡while(x ¡!= ¡9); ¡ 11 ¡

  12. Drivers ¡ • Similar ¡in ¡purpose ¡to ¡the ¡second ¡type ¡of ¡stub ¡ • From ¡the ¡other ¡direcJon ¡ ¡ – Stub ¡‘simulates’ ¡internal ¡operaJon ¡of ¡program ¡ – Driver ¡provides ¡input ¡and ¡receives ¡output ¡to ¡test ¡ the ¡program ¡ ¡ – May ¡do ¡minimal ¡input ¡to ¡get ¡test ¡data ¡from ¡user ¡ ¡ For ¡examples ¡see ¡pages ¡385-­‑387 ¡of ¡the ¡text. ¡ ¡ 12 ¡

  13. Fundamental ¡Rule ¡for ¡TesJng ¡ • DecomposiJon ¡is ¡important ¡ • SJll ¡need ¡for ¡thorough ¡tesJng! ¡ ¡ • Every ¡funcJon ¡should ¡be ¡tested ¡in ¡a ¡program ¡ in ¡which ¡every ¡other ¡funcJon ¡in ¡that ¡program ¡ has ¡already ¡been ¡fully ¡tested ¡and ¡debugged. ¡ ¡ ¡ 13 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend