Understanding Control Flow and Arrays in Java
This lecture covers essential control flow mechanisms in Java, focusing on Boolean expressions, if-else statements, switch statements, and various types of loops including while, do-while, and for loops. We explore Boolean expressions for navigating code blocks, proper syntax, and the semantics of these structures. Additionally, we delve into the concept of arrays in Java, defining types, accessing and modifying array elements, and understanding array lengths. Examples and exercises will enhance comprehension and application of these core Java concepts.
Understanding Control Flow and Arrays in Java
E N D
Presentation Transcript
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 if-else'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;}
'Elif' in Java? There is no 'elif' in Java: just chain "if else" statements together: if (be1) s1 else if (be2) s2else 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 orderuntil exact match is found. • execute allstmt'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)stmtSemantics: • 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 (;;) stmtExample: for (inti = 0; i<10; i=i+1) System.out.println(i);
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 5thelt. of xs. xs[0] = 7; //replaces 1stxselt. 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.lengthys[i].length