css 161 fundamentals of compu3ng flow control 4 october
play

CSS 161 Fundamentals of Compu3ng Flow control (4) - PowerPoint PPT Presentation

CSS 161 Fundamentals of Compu3ng Flow control (4) October 17, 2012 Instructor: Uma Murthy Announcements and reminders Announcements HW 1 grades delayed


  1. CSS ¡161 ¡ Fundamentals ¡of ¡Compu3ng ¡ Flow ¡control ¡(4) ¡ October ¡17, ¡2012 ¡ Instructor: ¡Uma ¡Murthy ¡

  2. Announcements ¡and ¡reminders ¡ Announcements ¡ – HW ¡1 ¡grades ¡delayed ¡– ¡will ¡be ¡out ¡today ¡ – Website ¡update ¡ – HW1 ¡solu3on ¡(check ¡homeworks ¡and ¡solu3ons ¡page) ¡ – Lecture ¡7 ¡slides ¡(check ¡updated ¡schedule ¡page) ¡ Reminders ¡ – All ¡homeworks ¡will ¡be ¡due ¡before ¡class ¡at ¡11am, ¡unless ¡otherwise ¡ specified ¡ – Prac3ce ¡using ¡Prac3ce ¡it, ¡self-­‑test ¡exercises, ¡and ¡programming ¡ exercises ¡ – Midterm ¡1 ¡on ¡Wed, ¡Oct ¡24 ¡ CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 2 ¡

  3. Outline ¡today ¡ • Review ¡loops ¡ • Complete ¡Chapter ¡3 ¡ • Do ¡problems ¡from ¡book ¡and ¡Prac3ce ¡it ¡ CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 3 ¡

  4. Acknowledgments ¡ • A ¡subset ¡of ¡the ¡following ¡slides ¡have ¡been ¡ either ¡directly ¡taken ¡from ¡or ¡derived ¡from ¡the ¡ supplements ¡of ¡the ¡book: ¡ Building ¡Java ¡Programs: ¡A ¡Back ¡to ¡Basics ¡Approach , ¡ 2nd ¡edi(on ¡ by ¡Stuart ¡Reges ¡and ¡Marty ¡Stepp ¡ hVp://www.buildingjavaprograms.com/supplements.shtml ¡ ¡ CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 4 ¡

  5. Review ¡ • Boolean ¡expressions ¡ – Short ¡circuit ¡evalua3on ¡ – Side-­‑effects ¡of ¡Boolean ¡expressions ¡ – Boolean ¡expressions ¡outside ¡of ¡condi3onals ¡and ¡ loops ¡ – Comparing ¡Strings ¡ CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 5 ¡

  6. Comparing ¡Strings ¡ • Comparison ¡operators ¡only ¡work ¡on ¡ primi%ve ¡ types ¡ ¡ – boolean, ¡char, ¡byte, ¡short, ¡int, ¡long, ¡float, ¡double ¡ • To ¡compare ¡other ¡ types , ¡need ¡other ¡methods ¡ – compareTo() – equals() – equalsIgnoreCase() [for ¡Strings ¡only] ¡ CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 6 ¡

  7. Comparing ¡Strings ¡example ¡ String s1 = "tintin"; String s2 = "snowy"; String s3 = "Tintin"; System.out.println(s1.equals(s2)); System.out.println(s1.equals("tintin")); System.out.println(s1.equals(s3)); System.out.println(s1.equalsIgnoreCase(s3)); System.out.println(s1.compareTo(s2)); System.out.println(s2.compareTo(s1)); System.out.println(s2.compareTo("snowy")); CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 7 ¡

  8. Comparing ¡Strings ¡example ¡ String s1 = "tintin"; String s2 = "snowy"; String s3 = "Tintin"; false ¡ System.out.println(s1.equals(s2)); System.out.println(s1.equals("tintin")); true ¡ System.out.println(s1.equals(s3)); false ¡ System.out.println(s1.equalsIgnoreCase(s3)); true ¡ System.out.println(s1.compareTo(s2)); 1 ¡ System.out.println(s2.compareTo(s1)); -­‑1 ¡ System.out.println(s2.compareTo("snowy")); 0 ¡ CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 8 ¡

  9. 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: ¡  while ( boolean expression ) statement  do statement while ( boolean_expression )  for ( initialization ; stop_condition ; update ) statement CSS ¡161: ¡Fundamentals ¡of ¡Compu3ng ¡ 9 ¡

  10. 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 ¡ CSS ¡161: ¡Fundamentals ¡of ¡ 3-­‑10 ¡ Compu3ng ¡

  11. while ¡Syntax ¡ while (Boolean_Expression) Statement ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Or ¡ while (Boolean_Expression) { Statement_1 Statement_2 . ¡. ¡. ¡ Statement_Last } CSS ¡161: ¡Fundamentals ¡of ¡ 3-­‑11 ¡ Compu3ng ¡

  12. do-while ¡Syntax ¡ do Statement while (Boolean_Expression); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Or ¡ do { Statement_1 Statement_2 . ¡. ¡. ¡ Statement_Last } while (Boolean_Expression); CSS ¡161: ¡Fundamentals ¡of ¡ 3-­‑12 ¡ Compu3ng ¡

  13. The ¡ for ¡loop ¡

  14. The ¡ for ¡Statement ¡Syntax ¡ for (Initializing; Boolean_Expression; Update) Body • The ¡ Body ¡may ¡consist ¡of ¡a ¡single ¡statement ¡or ¡a ¡list ¡of ¡ statements ¡enclosed ¡in ¡a ¡pair ¡of ¡braces ¡( { } ) ¡ • Note ¡that ¡the ¡three ¡control ¡expressions ¡are ¡separated ¡by ¡two, ¡ not ¡three, ¡semicolons ¡ • Note ¡that ¡there ¡is ¡no ¡semicolon ¡aeer ¡the ¡closing ¡parenthesis ¡ at ¡the ¡beginning ¡of ¡the ¡loop ¡ ¡ CSS ¡161: ¡Fundamentals ¡of ¡ 3-­‑14 ¡ Compu3ng ¡

  15. for ¡loop ¡syntax ¡ for ( ini(aliza(on ; test ; update ) { header statement ; statement ; ... ¡ body statement ; } – Perform ¡ ini(aliza(on ¡once. ¡ – Repeat ¡the ¡following: ¡ • Check ¡if ¡the ¡ test ¡is ¡true. ¡ ¡If ¡not, ¡stop. ¡ • Execute ¡the ¡ statement s. ¡ • Perform ¡the ¡ update . ¡

  16. Seman3cs ¡of ¡the ¡ for ¡Statement ¡ CSS ¡161: ¡Fundamentals ¡of ¡ 3-­‑16 ¡ Compu3ng ¡

  17. Repe33on ¡with ¡ for ¡loops ¡ • So ¡far, ¡repea3ng ¡a ¡statement ¡is ¡redundant: ¡ System.out.println("Homer says:"); System.out.println("I am so smart"); System.out.println("I am so smart"); System.out.println("I am so smart"); System.out.println("I am so smart"); System.out.println("S-M-R-T... I mean S-M-A-R-T"); • Java's ¡ for ¡loop ¡statement ¡performs ¡a ¡task ¡many ¡3mes. ¡ System.out.println("Homer says:"); for (int i = 1; i <= 4; i++) { // repeat 4 times System.out.println("I am so smart"); } System.out.println("S-M-R-T... I mean S-M-A-R-T");

  18. Ini3aliza3on ¡ ¡ for ( int i = 1 ; i <= 6; i++) { System.out.println("I am so smart"); } • Tells ¡Java ¡what ¡variable ¡to ¡use ¡in ¡the ¡loop ¡ – Performed ¡once ¡as ¡the ¡loop ¡begins ¡ – The ¡variable ¡is ¡called ¡a ¡ loop ¡counter ¡ • can ¡use ¡any ¡name, ¡not ¡just ¡ i • can ¡start ¡at ¡any ¡value, ¡not ¡just ¡ 1

  19. Test ¡ for (int i = 1; i <= 6 ; i++) { System.out.println("I am so smart"); } • Tests ¡the ¡loop ¡counter ¡variable ¡against ¡a ¡limit ¡ – Uses ¡comparison ¡operators: ¡ less ¡than ¡ < less ¡than ¡or ¡equal ¡to ¡ <= greater ¡than ¡ > greater ¡than ¡or ¡equal ¡to ¡ >=

  20. Repe33on ¡over ¡a ¡range ¡ System.out.println("1 squared = " + 1 * 1); System.out.println("2 squared = " + 2 * 2); System.out.println("3 squared = " + 3 * 3); System.out.println("4 squared = " + 4 * 4); System.out.println("5 squared = " + 5 * 5); System.out.println("6 squared = " + 6 * 6); – Intui3on: ¡"I ¡want ¡to ¡print ¡a ¡line ¡for ¡each ¡number ¡from ¡1 ¡to ¡6" ¡ • The ¡ for ¡loop ¡does ¡exactly ¡that! ¡ for (int i = 1; i <= 6; i++) { System.out.println( i + " squared = " + (i * i) ); } – "For ¡each ¡integer ¡ i ¡from ¡1 ¡through ¡6, ¡print ¡..." ¡

  21. 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

  22. Increment ¡and ¡decrement ¡ shortcuts ¡to ¡increase ¡or ¡decrease ¡a ¡variable's ¡value ¡by ¡1 ¡ Shorthand ¡ Equivalent ¡longer ¡version ¡ variable ++; variable = variable + 1; variable = variable - 1; ¡ variable --; int x = 2; x++; // x = x + 1; // x now stores 3 double gpa = 2.5; gpa--; // gpa = gpa - 1; // gpa now stores 1.5 ¡

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