Control Flow and Arrays

Download Report

Transcript Control Flow and Arrays

TODAY’S LECTURE
• Review Chapter 2
• Go over exercises
CONTROL FLOW
•
•
•
•
•
•
Boolean expressions
if / if-else
switch
while, do-while
for (original, Iterator versions)
(break, continue)
BOOLEAN EXPRESSIONS
Control flow uses boolean expressions to navigate
blocks of code.
How do we get booleans?
• directly, with true and false.
• using relational operators: <, <=, >, >=, ==, !=
• using boolean operators: &&, ||, !
• calling a method that returns a boolean
e.g. myScanner.hasNext()
• any expression, as long as it results in true or false.
BLOCK STATEMENT
• As we introduce blocks of code for the branches of ifelse's and switch statements, and for the bodies of loops,
we want to group many statements together.
• In Java, we place curly braces { } around multiple
statements to group them.
• It is so common to use them with control structures that it
seems like {}'s are part of their syntax, but it is a separate
statement structure all on its own.
Example: {
stmt1;
stmt2;
}
• If there is only one statement inside the curly braces, we
often drop the curly braces for legibility. Can you think of
a danger with this approach?
'ELIF' IN JAVA?
There is no 'elif' in Java: just chain "if else" statements together:
if
(be1) s1
else if (be2) s2
else if (be3) s3
else s4
SWITCH STATEMENT
Syntax:
switch (expr) {
case val1: stmt1
case val2: stmt2
...
default: stmtD // this 'default' case is optional
}
Semantics:
• expr must be integral (whole number) or char (no Strings or
booleans or floats or objects!). All case values must be constants.
• evaluate expr, and compare against each case value in order
until exact match is found.
• execute all stmt's after matching case! (thus break is common at
the end of each case)
• default: no value; stmtD always runs if no other case values
equaled the switch expression.
DO-WHILE LOOP
Syntax:
do stmt
while (boolexpr);
Semantics:
• evaluate stmt (no matter what).
• evaluate boolexpr; if true, repeat (evaluate stmt again). If
false, do-while is done.
• Note: semicolon after (boolexpr) is required! ;
• Note: stmt runs at least once (unlike while loop, whose stmt
might not run at all).
Example:
int x = 0; //consider also x = 500;
do
System.out.println(x++);
while (x<100);
FOR LOOP
Syntax:
for (initializer; guard; incrementer)
stmt
Semantics:
• initializer is a statement. Runs exactly once, before everything else. (If
a variable is declared, its scope is only within loop. Variable doesn't
have to be declared, it can already exist).
• guard is a boolean expression. Each iteration (including first), this is
checked: true => run stmt; false => exit loop.
• incrementer is a statement. Runs after stmt, each time that stmt runs.
• Note: initializer, guard, and incrementer could each be omitted!
E.g., for (;;) stmt
for (int i = 0; i<10; i=i+1)
System.out.println(i);
• foreach ( iterator : someList) is another version
Example:
LET’S GO OVER THE EXERCISES
ARRAYS
TODAY’S LECTURE
• Review Chapter 3
• Go over exercises
ARRAY TYPES
• The array type is indicated with [ ]'s.
• Monomorphism: Just as variables can only hold one
type of value, Java arrays can only hold one
specified type of value, in every slot.
• Example array types:
int[] double[]
boolean[][]
Person[]
• The type doesn't record the dimension lengths, but
an array value will specify the (unchanging) lengths.
int[][][] xs = new int[3][4][5]; //a 3x4x5 structure of ints.
ACCESSING/MODIFYING ARRAYS
Brackets [ ] are used to access and update values in
arrays.
int a = xs[4]; //accesses 5th elt. of xs.
xs[0] = 7;
//replaces 1st xs elt. with a 7.
Any expression of type int may be used as an index,
regardless of the type in the array:
xs[ a+4 ]
xs [ sc.nextInt() ]
xs[ i ]
ys[ i ][ j ]
The length of an array is available as an attribute:
xs.length
ys[i].length
LET’S GO OVER THE EXERCISES
•
QUESTIONS?