css 161 fundamentals of compu3ng flow control debugging
play

CSS 161 Fundamentals of Compu3ng Flow control, Debugging, - PowerPoint PPT Presentation

CSS 161 Fundamentals of Compu3ng Flow control, Debugging, Classes October 29, 2012 Instructor: Uma Murthy Announcements and reminders Announcements Quizzes


  1. CSS ¡161 ¡ Fundamentals ¡of ¡Compu3ng ¡ Flow ¡control, ¡Debugging, ¡Classes ¡ October ¡29, ¡2012 ¡ Instructor: ¡Uma ¡Murthy ¡

  2. Announcements ¡and ¡reminders ¡ Announcements ¡ – Quizzes ¡(see ¡forthcoming ¡slide) ¡ ¡ – Late ¡days ¡status ¡maintained ¡on ¡gradebook ¡ Reminders ¡ – Homework ¡4 ¡due ¡midnight ¡Wednesday, ¡October ¡31 ¡ 2 ¡

  3. Late ¡days ¡on ¡gradebook ¡ Indicates the number of late days taken, ranging from 0 to 3 3 ¡

  4. Quizzes ¡ • Begin ¡next ¡week ¡ • Tenta3ve ¡schedule ¡and ¡topics: ¡ – 11/5: ¡condi3onal ¡statements ¡and ¡loops ¡ ¡ – 11/14: ¡Classes, ¡I/O ¡ – 11/28: ¡Arrays ¡ • Mechanical ¡ques3ons ¡ – Short ¡answer, ¡output, ¡tracing, ¡etc. ¡ 4 ¡

  5. Outline ¡today ¡ • Definite ¡( for ) ¡and ¡indefinite ¡( while, do-while ) ¡loops ¡ • Pseudocode ¡and ¡nested ¡loops ¡example ¡ • Fencepost ¡analogy ¡ • Wrap ¡up ¡debugging ¡ • Review ¡methods ¡ • Parameterized ¡methods ¡ 5 ¡

  6. Definite ¡and ¡indefinite ¡loops ¡ 6 ¡

  7. Loops ¡ • Mechanism ¡for ¡repea3ng ¡block ¡of ¡statements ¡ – Do ¡we ¡know ¡how ¡many ¡3mes ¡to ¡repeat? ¡ – Do ¡we ¡want ¡to ¡execute ¡at ¡least ¡once? ¡ • 3 ¡types ¡of ¡loops: ¡  for ( initialization ; stop_condition ; update ) statement  while ( boolean expression ) statement  do statement while ( boolean_expression ) 7 ¡

  8. Loops ¡ • Loops ¡in ¡Java ¡are ¡similar ¡to ¡those ¡in ¡other ¡high-­‑level ¡ languages ¡ • Java ¡has ¡three ¡types ¡of ¡loop ¡statements: ¡ ¡the ¡ while , ¡ the ¡ do-while , ¡and ¡the ¡ for ¡statements ¡ – The ¡code ¡that ¡is ¡repeated ¡in ¡a ¡loop ¡is ¡called ¡the ¡ body ¡of ¡the ¡ loop ¡ – Each ¡repe33on ¡of ¡the ¡loop ¡body ¡is ¡called ¡an ¡ itera-on ¡of ¡ the ¡loop ¡ 3-­‑8 ¡

  9. Categories ¡of ¡loops ¡ • definite ¡loop : ¡Executes ¡a ¡known ¡number ¡of ¡3mes. ¡ – for ¡loops ¡are ¡definite ¡loops. ¡ • Print ¡"hello" ¡10 ¡3mes. ¡ • Find ¡all ¡the ¡prime ¡numbers ¡up ¡to ¡an ¡integer ¡ n . ¡ • Print ¡each ¡odd ¡number ¡between ¡5 ¡and ¡127. ¡ • indefinite ¡loop : ¡One ¡where ¡the ¡number ¡of ¡3mes ¡its ¡ body ¡repeats ¡is ¡not ¡known ¡in ¡advance: ¡ ¡ – while and ¡ do-while loops ¡are ¡indefinite ¡loops ¡ • Prompt ¡the ¡user ¡un3l ¡they ¡type ¡a ¡non-­‑nega3ve ¡number. ¡ • Print ¡random ¡numbers ¡un3l ¡a ¡prime ¡number ¡is ¡printed. ¡ • Repeat ¡un3l ¡the ¡user ¡has ¡types ¡"q" ¡to ¡quit . ¡ 9 ¡

  10. for ¡Syntax ¡ for(initialization; Boolean_Expression; update) Statement for(initialization; Boolean_Expression; update) { Statement_1 Statement_2 . ¡. ¡. ¡ Statement_Last } 3-­‑10 ¡

  11. for ¡loop ¡walkthrough ¡ 1 2 3 for (int i = 1; i <= 4; i++) { 4 System.out.println(i + " squared = " + (i * i)); } System.out.println("Whoo!"); ¡ 5 1 ¡Output: ¡ 1 squared = 1 2 2 squared = 4 3 squared = 9 4 squared = 16 4 Whoo! ¡ 3 5 11 ¡

  12. while ¡Syntax ¡ while (Boolean_Expression) Statement ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Or ¡ while (Boolean_Expression) { Statement_1 Statement_2 . ¡. ¡. ¡ Statement_Last } 3-­‑12 ¡

  13. The ¡ while ¡loop ¡ • while ¡loop : ¡Repeatedly ¡executes ¡its ¡ body ¡as ¡long ¡as ¡a ¡logical ¡test ¡is ¡true. ¡ • Example: ¡ int num = 1; // initialization while (num <= 200) { // test System.out.print(num + " "); num = num * 2; // update } // output: 1 2 4 8 16 32 64 128 ¡ 13 ¡

  14. do-while ¡Syntax ¡ do Statement while (Boolean_Expression); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Or ¡ do { Statement_1 Statement_2 . ¡. ¡. ¡ Statement_Last } while (Boolean_Expression); 3-­‑14 ¡

  15. The ¡ do/while ¡loop ¡ • do/while ¡loop : ¡ Performs ¡its ¡test ¡at ¡the ¡ end ¡of ¡each ¡ repe33on. ¡ – Guarantees ¡that ¡the ¡loop's ¡ {} ¡body ¡will ¡run ¡at ¡least ¡once. ¡ //Example: prompt until correct password is typed String phrase; do { System.out.print("Type your password: "); phrase = console.next(); } while (!phrase.equals("abracadabra")); 15 ¡

  16. Algorithms ¡and ¡Pseudocode ¡ • The ¡hard ¡part ¡of ¡solving ¡a ¡problem ¡with ¡a ¡computer ¡ program ¡is ¡coming ¡up ¡with ¡the ¡underlying ¡solu3on ¡ method ¡ • An ¡ algorithm ¡is ¡a ¡set ¡of ¡precise ¡instruc3ons ¡that ¡lead ¡ to ¡a ¡solu3on ¡ – An ¡algorithm ¡is ¡normally ¡wrieen ¡in ¡ pseudocode , ¡which ¡is ¡a ¡ mixture ¡of ¡programming ¡language ¡and ¡a ¡human ¡language, ¡ like ¡English ¡ – Pseudocode ¡must ¡be ¡precise ¡and ¡clear ¡ – However, ¡pseudocode ¡is ¡much ¡less ¡rigid ¡than ¡code ¡ 3-­‑16 ¡

  17. Nested ¡Loops ¡ • Loops ¡can ¡be ¡ nested , ¡just ¡like ¡other ¡Java ¡structures ¡ – When ¡nested, ¡the ¡inner ¡loop ¡iterates ¡from ¡beginning ¡to ¡end ¡for ¡each ¡ single ¡itera3on ¡of ¡the ¡outer ¡loop ¡ int rowNum, columnNum; for (rowNum = 1; rowNum <=3; rowNum++) { for (columnNum = 1; columnNum <=2; columnNum++) System.out.print(" row " + rowNum + " column " + columnNum); System.out.println(); } 3-­‑17 ¡

  18. Example: ¡Drawing ¡complex ¡figures ¡ • Use ¡nested ¡ for ¡loops ¡to ¡produce ¡the ¡ following ¡output. ¡ +------+ |\..../| | \../ | | \/ | | /\ | | /..\ | |/....\| +------+ 18 ¡

  19. Development ¡strategy ¡ • Recommenda3ons ¡for ¡managing ¡ complexity: ¡ 1. ¡Design ¡the ¡program ¡ ¡(think ¡about ¡steps ¡or ¡ methods ¡needed ¡-­‑ ¡ pseudocode ). ¡ +------+ • write ¡an ¡English ¡descrip3on ¡of ¡steps ¡required ¡ |\..../| • use ¡this ¡descrip3on ¡to ¡decide ¡the ¡methods ¡ | \../ | | \/ | 2. ¡Create ¡a ¡table ¡of ¡paeerns ¡of ¡characters ¡ | /\ | • use ¡table ¡to ¡write ¡your ¡ for ¡loops ¡ | /..\ | |/....\| +------+ 19 ¡

  20. 1. ¡Pseudo-­‑code ¡ • pseudo-­‑code : ¡An ¡English ¡descrip3on ¡of ¡an ¡algorithm. ¡ • Example: ¡Drawing ¡a ¡12 ¡wide ¡by ¡7 ¡tall ¡box ¡of ¡stars ¡ ¡ print ¡12 ¡stars. ¡ ¡for ¡(each ¡of ¡5 ¡lines) ¡{ ¡ ¡ ¡ ¡ ¡ ¡print ¡a ¡star. ¡ ************ * * ¡ ¡ ¡ ¡ ¡print ¡10 ¡spaces. ¡ * * ¡ ¡ ¡ ¡ ¡print ¡a ¡star. ¡ * * * * ¡} ¡ * * ¡print ¡12 ¡stars. ¡ ************ 20 ¡

  21. Pseudo-­‑code ¡algorithm ¡ 1. ¡Line ¡ • + ¡, ¡6 ¡ - , ¡ + 2. ¡Top ¡half ¡ • | • spaces ¡(increasing) ¡ • \ +------+ • dots ¡(decreasing) ¡ |\..../| • / | \../ | • spaces ¡(same ¡as ¡above) ¡ • | | \/ | 3. ¡Boeom ¡half ¡(top ¡half ¡upside-­‑down) ¡ | /\ | 4. ¡Line ¡ | /..\ | • + ¡, ¡6 ¡ - , ¡ + |/....\| +------+ 21 ¡

  22. Methods ¡from ¡pseudocode ¡ public class Mirror { public static void main(String[] args) { line(); topHalf(); bottomHalf(); line(); } public static void topHalf() { for (int line = 1; line <= 3; line++) { // contents of each line +------+ } } |\..../| public static void bottomHalf() { | \../ | for (int line = 1; line <= 3; line++) { | \/ | // contents of each line } | /\ | } | /..\ | public static void line() { // ... |/....\| } } +------+ 22 ¡

  23. 2. ¡Tables ¡ • A ¡table ¡for ¡the ¡top ¡half: ¡ – Compute ¡spaces ¡and ¡dots ¡expressions ¡from ¡line ¡number ¡ line spaces dots 1 0 4 +------+ 2 1 2 |\..../| | \../ | 3 2 0 | \/ | | /\ | | /..\ | |/....\| +------+ 23 ¡

  24. 2. ¡Tables ¡ • A ¡table ¡for ¡the ¡top ¡half: ¡ – Compute ¡spaces ¡and ¡dots ¡expressions ¡from ¡line ¡number ¡ line spaces line - 1 dots -2 * line + 6 +------+ 1 0 0 4 4 |\..../| 2 1 1 2 2 | \../ | 3 2 2 0 0 | \/ | | /\ | | /..\ | |/....\| +------+ 24 ¡

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