review for midterm 2 l.
Download
Skip this Video
Download Presentation
Review for Midterm 2

Loading in 2 Seconds...

play fullscreen
1 / 17

Review for Midterm 2 - PowerPoint PPT Presentation


  • 105 Views
  • Uploaded on

Review for Midterm 2. Aaron Bloomfield CS 101-E. Test focus. Test will focus on the material covered since the last midterm Chapters 5 and 6 The test is cumulative, though, and can include any material covered so far. Chapter 5. Logical expressions.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Review for Midterm 2' - zohar


Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
review for midterm 2

Review for Midterm 2

Aaron Bloomfield

CS 101-E

test focus
Test focus
  • Test will focus on the material covered since the last midterm
    • Chapters 5 and 6
  • The test is cumulative, though, and can include any material covered so far
logical expressions
Logical expressions
  • Logical expression has values either true or false
  • Java has the boolean type with values true or false
  • Truth table: method to dissect a logical expression
logical operators
Logical operators
  • Three primary logical operators: and, or, not
  • An and operation is only true when both parts are true
  • An or operation is true when either (or both) parts are true
  • A not operation negates (switches) the value of the expression
  • Logical operators: and is &&, or is ||, not is !
  • Not operator is unary
equality
Equality
  • Two equality operators: == and !=
  • When comparing objects, == compares the references, not the objects themselves
  • Use the .equals() method, when available, to test for object equality
  • Don't test floating point values for equality! Instead, test for “closeness”
ordering
Ordering
  • Ordering operators: <, >, <=, and >=. These only work on primitive types!
  • Relational operators are the equality operators and the ordering operators
  • For booleans, false is less than true
  • For characters, ordering is based on the Unicode numbers of the characters
if statements
If statements
  • An if statement has the form: if (expression) action
  • An if-else statement has the form: if (expression) action1 else action2
  • An if-else-if statement is used when there are many tasks to do, depending on the logical expressions
switches
Switches
  • A switch statement is useful instead of a long-winded if-else-if block
  • Must always put either break at the end of a switch statement block, or a comment such as '// FALL THRU'
  • The default case means any case not matched by any of the other cases
slide10
Misc
  • Operator precedence: (p 187)
  • Short-circuit evaluation: left side is evaluated first. If the result can be determined at that point, right side is not evaluated
  • System.exit() will terminate the program immediately
  • Use consistent indentation!
chapter 6 iteration while loop syntax
Chapter 6: Iterationwhile loop syntax
  • While statements:
    • while ( expression ) action
    • Action is executed repeatedly while expression is true
    • Once expression is false, program execution moves on to next statement
    • Action can be a single statement or a block
    • If expression is initially false, action is never executed
chapter 6 iteration do while loop syntax
Chapter 6: Iterationdo-while loop syntax
  • Do-while statements:
    • do action while ( expression )
    • Action is executed one ALWAYS
      • Then expression is tested after each loop
    • Once expression is false, program execution moves on to next statement
    • Action can be a single statement or a block
    • If expression is initially false, action is never executed
chapter 6 iteration for loop syntax
Chapter 6: Iterationfor loop syntax
  • For statements:
    • for ( forinit; forexpression; forupdate ) action
    • forinit is executed once only (before the loop starts the first time)
    • Action is executed repeatedly while forexpression is true
    • After action is executed at the end of each loop, forupdate is executed
    • Once forexpression is false, program execution moves on to next statement
    • Action can be a single statement or a block
    • If expression is initially false, action is never executed
chapter 6 iteration common pitfalls
Chapter 6: IterationCommon pitfalls
  • Infinite loop: a loop whose text expression never evaluates to false
  • Be sure that your for loop starts and ends where you want it to
    • For example, in an array of size n, it needs to start at 0 and end at n-1
    • Otherwise, it’s called an “off-by-one” error
  • Be sure your loop variable initialization is correct
chapter 6 iteration file access
Chapter 6: IterationFile access
  • Java provides the File class for file I/O
    • Constructor takes in the file name as a String
  • A stream is a name for a input or output method
    • System.out: output stream
    • System.err: error output stream
    • System.in: input stream
    • File: file input or output stream
chapter 6 iteration scanner methods
Chapter 6: IterationScanner methods
  • The Scanner class can be initialized with an File object
    • Scanner filein = new Scanner (new File (filename));
  • The Scanner class has a bunch of methods useful in loops:
    • hasNextInt(): tells whether there is a next int
    • hasNextDouble(): same idea, but with doubles