Download presentation
Presentation is loading. Please wait.
Published byShannon Joseph Modified over 8 years ago
1
UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri. 9/22/00
2
Homework #3 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 Mon, 9/18 Part 2 2Fri, 9/15 Fri, 9/22 Part 1 & Part 2 3Fri, 9/22 Fri, 9/29Part 1 & Part 2 HW# Assigned Due Content Homework is due at the start of lecture on the due date.
3
Java Fundamentals Control Structures
4
Program Control ä Sequential execution ä Instructions executed one after another ä Transfer of control ä Something other than the next instruction is executed
5
A Caution ä Flow of control has to be carefully thought out ä “spaghetti” code is hard to maintain ä No “goto” in Java ä goto is a keyword, but is not used
6
Single-entry/single-exit Programming ä Every logical “module” has a single defined entry point and a single defined exit point ä Control passes from the exit point of one “module” to the entry point of another ä Control structure stacking ä Control structure nesting
7
Selection (branching) ä if ä if/else ä switch
8
The “if” Structure ä Single selection ä If condition is true, do some special action ä If condition is false, continue
9
“if” Syntax ä boolean expression ä An expression that evaluates to true or false ä statement ä An arbitrary Java statement ä E.g., could be another if statement if (boolean expression) statement;
10
“if” Examples int j = 2; int a = 0; if (j < 3) { a = 4; } int j = 2; int a = 0; if ((j 10)) { a = 4; j = 55; if (a+j > 100) { // do something }
11
The “if/else” Structure ä Double selection ä If condition is true, do some special action ä If condition is false, do some other special action ä Then continue
12
“if/else” Syntax ä statement2 can be its own “if-statement” resulting in “if/else-if/else” nested logic ä An ‘else’ is always paired with a preceding ‘if’ ä The most recent previous one unless bracketed otherwise ä Similar to conditional operator (?:) ä boolean expression ? if-true-statement : if-false-statement ä It is good form to ALWAYS bracket statement1 and statement 2 (even when they are single-line statements) (even when they are single-line statements) if (boolean expression) statement1; else statement2;
13
“if/else” Examples int j = 2; int a = 0; if (j < 3) { a = 4; } else { a = 5; } int j = 2; int a = 0; if (j < 3) { a = 4; } else if (j = 3 { a = 5; } else // j >= 10 { a = 6; } cascading if
14
The “switch” Structure ä The switch structure embodies multiple selection logic (like a cascading if) ä Do one of an arbitrary number of actions based on the value of an expression ä NOTE: With object-oriented programming, we can use derived classes and polymorphism to get multiple selection behavior more elegantly ä Select on object type (class) ä Invoke the same named method of whatever class is involved at runtime ä More later in the course
15
“switch” Syntax switch (integer-expression) { case constant-expression : statement; … default : statement; } Requires condition to be expressible as an int (or convert to an int - e.g., byte, char, short) Uses labeled cases (also integers) Use break to ensure only one case is executed at a time Includes support for the C/C++ default label Can be nested
16
“switch” Examples int n = 10; final int ONE = 1, THREE = 3, FOUR = 4; switch (n) { case ONE : Sytem.out.println(“Value is 1”); break; case THREE : case FOUR : Sytem.out.println(“Value is 3 or 4); break; default : Sytem.out.println(“Value is not 1, 3, or 4”); break; // not strictly needed, but good form }
17
Repetition (iteration, or loops) ä for ä while ä do/while
18
The “for” structure ä Basic test-compute-increment control flow ä Includes “built-in” initialization and increment elements
19
“for” Syntax ä initialization is executed once upon entry ä test is evaluated each time through (including first time) ä If test passes, statement block is executed ä Else, execution continues after for statement ä increment is executed after each time through loop, then test is evaluated as above (“increment” can actually be a “decrement”) ä action-statement can be a single statement (or even an empty statement/no-op) ä Both initialization and increment can be comma-separated multiple statements ä E.g., for (i = 0, j = 0; i+j < 100; i++, j +=2) { // do something } ä loop variables can be declared in the initialization ä Unless you use a comma separated multiple-statement initialization ä For good form, ALWAYS bracket the action-statement, even if it is a single- line statement for (initialization; boolean test; increment) action-statement
20
“for” Examples for (int i = 10000; i < 0; --i); for (int i = 1; i < 10; i++) { System.out.println(“i = “ + i); } for (int i = 1; i < 10; i+=2) { System.out.println(“i = “ + i); } for (int i = 10000; i > 0; --i);
21
The “while” Structure ä Basic test-compute control flow ä Does not have built-in initialization or increment ä while loop “compute” statement(s) not guaranteed to execute
22
“while” Syntax ä boolean expression is evaluated ä if true, ä do statement (could be a compound statement) ä control flow then returns to boolean expression (for reevaluation) ä else, control flows to next statement after while statement ä No built-in increment!! ä For good form, ALWAYS bracket the statement, even if it is a single-line statement while (boolean expression) statement
23
“while” Examples int n = 10; while (n > 0) { System.out.println(“n = “ + n); n--; } int n = 10; while (true) { System.out.println(“n = “ + n); n--; if (n < 0) { break; } }
24
The “do/while” Structure ä Basic compute-test control flow ä Loop “compute” statement(s) guaranteed to execute at least once
25
“do/while” Syntax ä statement is executed ä boolean expression is evaluated ä if true, do statement block again ä else, control flows to next statement after do/while statement ä No built-in increment!! ä For good form, ALWAYS bracket the statement, even if it is a single- line statement ä Especially critical here for readability do statement while (boolean expression)
26
“do/while” Examples int n = 10; do { System.out.println(“n = “ + n); n--; } while (n > 0);
27
Related Statement Types ä labels ä break ä continue ä return
28
Labels ä Consists of an identifier followed by a colon ä Can be used to label: ä a repetition structure or switch ä a block that is delimited by { } ä Can work in conjunction with break and continue (to modify repetition structure control logic)
29
The ‘break’ Statement ä Transfer control out of a switch, for, do or while statement or out of a labeled structure ä Only break out of the innermost block (no label specified) or out of the labeled statement or compound statement identified by the break ä Use basically same as C, C++ except for use with labels ä Some programmers avoid the use of break
30
‘break’ Example foo: for (int j = 1; j < 10; j++) { for (int k = 1; k < 10; k++) { // do something; if (j+k > 10) { break foo; } // do something else
31
The ‘continue’ Statement ä Similar to break (alters control flow in for, do, while loops) ä Can be used with a label ä With no label - return to the boolean expression and reevaluate it (the for loop’s iteration statement is executed first) ä With label - return to the labeled for, do, or while and reevaluate as above ä Some programmers avoid the use of continue
32
‘continue’ Example foo: for (int j = 1; j < 10; j++) { for (int k = 1; k < 10; k++) { // do something; if (j+k > 10) { continue foo; } // do something else
33
The ‘return’ Statement ä Exit from a method invocation ä Can return a value (or void) ä Control flows back to the next statement after the method invocation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.