Lecture 3: Topics - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Lecture 3: Topics

Description:

Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 15
Provided by: Michael3045
Learn more at: https://cs.nyu.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture 3: Topics


1
Lecture 3 Topics
  • If-then-else
  • Operator precedence
  • While loops
  • Static methods
  • Recursion

2
Boolean expressions continued
  • If I can get an outdoor court tomorrow and its
    not raining or I can get an indoor court, we can
    play tennis
  • English can be ambiguous, but in programming, AND
    always has higher precedence than OR

3
If-then-else
  • if (i gt j)
  • if (a gt b)
  • ____
  • else // which if does it match?
  • ____
  • The else clause matches the most recent
    unmatched if in the same block

4
Whats the difference?
  • boolean x
  • if (x true)
  • ______
  • if (x)
  • ______
  • if (x true)
  • ______

5
Whats the difference?
  • Between x and x
  • They are both short for x x1, but they have
    different meanings in y x and y x
  • The first means
  • y x
  • x
  • while the second means
  • x
  • y x
  • Potentially confusing, better to use the longer
    versions in my opinion

6
Mixing assignments in one line
  • a b c 5
  • assignment operators are evaluated right to left,
    but other binary operators with same precedence
    are evaluated left to right, such as a b c
  • Very confusing! Better to avoid mixing
    assignment statements in one line in my opinion
  • OK to use a5 instead of a a 5, etc, but
    this is not necessary

7
Operator precedence
  • postfix , - -
  • unary , -, prefix , - - (avoid)
  • casting
  • !
  • ,/,
  • binary ,-
  • lt, lt, gt, gt
  • , !
  • (avoid)
  • (avoid)
  • (avoid)
  • , , -, , /, (I avoid except )
  • Use (,) when needed AND ALSO FOR CLARITY!

8
While loop demo
  • Extracting the letters from a string

9
Static methods
  • In older programming languages these were called
    procedures (when they did not return a value)
    and functions (when they returned a value)
  • Modifiers static, public, private
  • Return value void, or a type
  • The formal parameters (or parameters) are the
    ones listed in the method header
  • The actual parameters (or arguments) are the ones
    provided when the method is called
  • Together, the name of the method and its list of
    parameter types constitute the method signature

10
Why do we need methods?
  • To organize programs into manageable chunks of
    code
  • Abstraction, encapsulation
  • Always think of how to simplify code by using
    methods appropriately

11
Local variables and their scope
  • A variable declared inside a method is local to
    the method and cannot be accessed outside it
  • A variable declared in a loop or a block
    cannot be accessed outside it
  • Example
  • for (int i0 ilt10 i)
  • System.out.println(i) // error, i is not
    accessible

12
Method overloading
  • When two methods have the same name but different
    signatures
  • Many examples in the Java libraries

13
The call stack
  • Stack of info about the method call history,
    including space for local variables

14
Recursion (chapter 19!)
  • When a method calls itself
  • classic example factorial
  • but factorial can just as well be programmed with
    a loop
  • recursion really useful when the method makes two
    recursive calls
  • bad example Fibonacci (easy to write recursive
    code but it is very inefficient)
Write a Comment
User Comments (0)
About PowerShow.com