1 / 23

Flow of Control

Flow of Control. IST 311 / MIS 307. Control Structures. Sequence : one after the other Selection : selects a path based upon action Iteration : repetition. Selection. Simple if Statement Multiway if / else Statement Nested if / else switch Statement

ksingh
Download Presentation

Flow of Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Flow of Control IST 311 / MIS 307

  2. Control Structures • Sequence: one after the other • Selection: selects a path based upon action • Iteration: repetition

  3. Selection • Simple if Statement • Multiwayif / elseStatement • Nested if / else • switch Statement • Alternative way to write a multiway selection structure • Involves a test of equality • Logical expression being tested must be a primitive integral type byte, short, int, char

  4. Selection • switch executes as follows: • Integral expression is evaluated • Control passes to the statements following the case label whose value equals the integral expression • If no cases match, control passes to the default clause • All statements are executed up to the break statement

  5. Selection int m = 2; switch (m) { case 1: System.out.println (“m = 1”); break; case 2: System.out.println (“m = 2”); break; case 3: System.out.println (“m = 3”); break; default: System.out.println(“default case”); }

  6. Control Structures • Sequence: one after the other • Selection: selects a path based upon action • Iteration: repetition

  7. Iteration / Repetition • Repeats a statement or sequence of statements • Counting Loop • for • Conditional Loop • while • do-while

  8. Iteration / Repetition • Counter-Controlled Loop • Know exactly how many times to repeat the loop beforehand for ( initializer; loop entry condition; updater) { for loop body; }

  9. Iteration / Repetition int k; for (k = 0; k < 5; k++) { System.out.println (“Howdy”); } System.out.println(“k = “ + k);

  10. Iteration / Repetition • Infinite Loops • Loops execute while the loop entry condition remains true • Infinite loops occurs when the loop entry condition never becomes false for (k = 0; k < 5; k--) • When program generates output, loop is obvious • When no output, program will appear to freeze

  11. Iteration / Repetition • Nested Loop for (row = 1; row <= 4; row++) { for (col = 1; col <= 9; col++) { System.out.print (col * row + “\t” ); } System.out.println ( ); }

  12. Iteration / Repetition • Conditional Loops initializer statement; while ( loop entry condition) { loop body; updater statement; }

  13. Iteration / Repetition while (n != 1) { System.out.print (n + “ “); if (n % 2 == 0) n = n / 2; else n = 3 * n + 1; } System.out.println (n);

  14. Iteration / Repetition • Conditional Loops initializer statement; do { loop body; updater statement; } while ( loop entry condition);

  15. Control Structures • Sequence: one after the other • Selection: selects a path based upon action • Iteration: repetition

  16. Static Methods • static – means that only one copy of the variable or method is created, whether or not the class in which it occurs is instantiated • static elements are underlined in UML

  17. Math Class • java.lang.Math class provides common mathematical functions like: • double pow (double x, double y) • double random( ) • long round (double x) • double sqrt (double x) • All Math methods are static class methods and are invoked through the class name answer = Math.sqrt(9.0); • Included implicitly in all Java programs

  18. Generate Random Numbers • Math.random( ) – used to generate random value from 0 to 1 (0 to 0.99999999) • Given the same seed value, Math.random( ) will generate the same sequence of numbers every time. • Number generated is type double • Use a scaling factor to establish the range of numbers to be generated

  19. Generate Random Numbers • int num1 = (int)(Math.random( ) * 10); • Generate numbers from 0 to 9

  20. javadoc

  21. Comments • Should be used to improve readability of code

  22. Comments • C-style comments /* multiline comment */ // single line comment • Java documentation format: /** ….. */ • Uses the “javadoc.exe” file

  23. Java Documentation Tool • javadoc tool • Generates HTML pages that describe: • Public and protected classes • Inner classes • Interfaces • Constructors • Methods • Fields • Online documentation for Java library classes

More Related