1 / 16

CS 211 Control Flow Arrays

CS 211 Control Flow 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.

farren
Download Presentation

CS 211 Control Flow Arrays

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. CS 211Control FlowArrays

  2. Today’s Lecture • Review Chapter 2 • Go over exercises

  3. Control Flow • Boolean expressions • if / if-else • switch • while, do-while • for (original, Iterator versions) • (break, continue)

  4. 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.

  5. 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;}

  6. '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

  7. 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.

  8. 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);

  9. 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);

  10. Let’s Go Over the Exercises

  11. Arrays

  12. Today’s Lecture • Review Chapter 3 • Go over exercises

  13. 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.

  14. 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

  15. Let’s go over the exercises

  16. Questions?

More Related