230 likes | 251 Views
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
E N D
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 • 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
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
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”); }
Control Structures • Sequence: one after the other • Selection: selects a path based upon action • Iteration: repetition
Iteration / Repetition • Repeats a statement or sequence of statements • Counting Loop • for • Conditional Loop • while • do-while
Iteration / Repetition • Counter-Controlled Loop • Know exactly how many times to repeat the loop beforehand for ( initializer; loop entry condition; updater) { for loop body; }
Iteration / Repetition int k; for (k = 0; k < 5; k++) { System.out.println (“Howdy”); } System.out.println(“k = “ + k);
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
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 ( ); }
Iteration / Repetition • Conditional Loops initializer statement; while ( loop entry condition) { loop body; updater statement; }
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);
Iteration / Repetition • Conditional Loops initializer statement; do { loop body; updater statement; } while ( loop entry condition);
Control Structures • Sequence: one after the other • Selection: selects a path based upon action • Iteration: repetition
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
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
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
Generate Random Numbers • int num1 = (int)(Math.random( ) * 10); • Generate numbers from 0 to 9
Comments • Should be used to improve readability of code
Comments • C-style comments /* multiline comment */ // single line comment • Java documentation format: /** ….. */ • Uses the “javadoc.exe” file
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