Outline PowerPoint PPT Presentation

presentation player overlay
1 / 33
About This Presentation
Transcript and Presenter's Notes

Title: Outline


1
Outline
  • Dangling if
  • Loops
  • for
  • while
  • do/while
  • break and continue statements
  • Arrays

2
Recap
  • Blocks, scopes
  • Conditions
  • String comparisons
  • Decision making
  • if/else
  • switch

3
Dangling if
  • int a 40
  • int b 2
  • int c 10
  • boolean isCGreatest true
  • if ( c gt a )if ( c gt b )
  • isCGreatest true
  • else
  • isCGreatest false
  • isCGreatest will be true according to this code!

4
Why?
  • In the absence of braces separating the if and
    the else, the else condition is associated with
    the last if preceding it
  • It is a common mistake to assume it is associated
    with the first if statement

5
Equivalent Code to Dangling if
  • int a 40
  • int b 2
  • int c 10
  • boolean isCGreatest true
  • if ( c gt a )
  • if ( c gt b )
  • isCGreatest true
  • else
  • isCGreatest false

6
Intended Code for Dangling if
  • int a 40
  • int b 2
  • int c 10
  • boolean isCGreatest true
  • if ( c gt a )
  • if ( c gt b )
  • isCGreatest true
  • else
  • isCGreatest false
  • else
  • isCGreatest false

7
Loops
  • Execute a statement or block repeatedly
  • May have an associated
  • control variable
  • initialization of control variable
  • condition involving the control variable
  • modification to the control variable on each
    iteration

8
for loop
9
for loop format
  • for (initial condition step) statement
  • Statement can be a compound statement
  • E.g.
  • for (int i 0 i lt 10 i ) System.out.print
    ln(Value of i is i)

10
for loop example
  • public class ForLoop
  • public static void main(String arg)
  • int sum 0
  • for( int counter 0 counter lt 100 counter
    )
  • sum counter
  • System.out.println( The sum of first
    counter
  • numbers is sum )

11
for loop, continued
  • The scope of counter is the for loop, however, so
    this example would not work (counter is used
    outside the for loop).
  • The loop in the above example calculates the sum
    of all numbers from 0 to 100.

12
while loop
13
while loop format
  • while ( condition ) statement
  • Any variables used in the condition must be
    declared beforehand
  • Any update to them must be done in the statement
    or the condition itself
  • Continues executing the statement or statement
    block until the condition evaluates to false

14
while loop example
  • public class WhileLoop
  • public static void main(String arg)
  • int counter 1
  • int sum 0
  • while( counter lt 100 )
  • sum counter
  • counter
  • System.out.println(sum is sum , counter
    is
  • counter)
  • // outputs sum is 5050, counter is 101

15
do-while loop
16
do-while loop format
  • do statementwhile( condition )
  • The loop is executed at least once, since the
    condition is checked after the loop, not before
  • Otherwise, exactly the same as a while loop

17
do-while loop example
  • import java.io.
  • public class DoWhileLoop
  • public static void main(String arg) throws
    Exception
  • String input
  • BufferedReader reader new BufferedReader(new
  • InputStreamReader(Sy
    stem.in))
  • do
  • System.out.print(Enter no to end )
  • input reader.readLine()
  • while(!input.equals(no))

18
break statement
  • Immediately exits the loop
  • E.g.
  • do
  • input reader.readLine()
  • if (input.equals(no))
  • break
  • while( true )

19
continue statement
  • Skips to the next loop iteration
  • E.g., to add only odd numbers
  • int sum 0
  • for( int counter 0 counter lt 100 counter )
  • if ( counter 2 0 )
  • continue
  • sum counter
  • System.out.println(The sum is sum)

20
Nested Loops
  • import java.io.
  • public class NestedLoop
  • public static void main(String arg) throws
    Exception
  • BufferedReader reader new BufferedReader(new

  • InputStreamReader(System.in))
  • while( true )
  • System.out.print(Enter a number, or no to
    quit )
  • String input reader.readLine()
  • if ( input.equals(no) )
  • break
  • int factorial 1
  • for( int i Integer.parseInt(input) i gt0
    i-- )
  • factorial i
  • System.out.println(Factorial is
    factorial)

21
Summary (loops)
  • Be careful when nesting if/else statements
  • Use for, while, and do-while to repeatedly
    execute a block of code
  • Use break to exit a loop prematurely
  • Use continue to skip to the next iteration

22
Arrays
  • A data structure that holds a collection of data
    all of the same type
  • Conceptually, a numbered list of data elements
    size is fixed
  • Items can be accessed by their position in the
    array (starting at 0)

23
Array as a Collection
  • a0 0
    a
  • a1 10
  • a2 11
  • a3 59
  • a4 43
    You can only store
  • a5 31
    values of the same
  • a6 89
    type in an array.
  • a7 104
    Array name is a
  • a8 6
    reference to the array.
  • a9 10

24
Declaring arrays
  • An array has to be declared
  • Memory for the array has to be allocated with the
    new operator
  • Declaring and allocating an array lttypegt
    ltnamegt new lttypegtltsizegt
  • The following code declares and allocates memory
    for an array of 10 integers int myArray new
    int10

25
Declaring vs. Allocating
  • It is possible to declare an array variable, and
    use it without explicitly allocating memory for
    it
  • E.g.,
  • int anArray
  • int anotherArray anArray new int20
  • anotherArray anArray
  • Both variables access the same data

26
Accessing array elements
  • Need
  • Array variable
  • Position of the desired element in the array -
    the element index. (Indexes start from 0, not 1)
  • Use ltvariable namegtltindexgt
  • E.g., this is an expression evaluating to the
    value of the 6th element of array anArray
  • anArray5

27
Array example
  • int a new int10
  • a0 10
  • a1 1
  • for( int i2 ilt10 i )
  • ai -1
  • Note that accessing a10, a11, etc. would
    cause an error to occur

28
Array size
  • The size is the number of elements an array
    holds, and is fixed when the array is allocated
  • The member variable length stores the arrays
    size (you cannot modify it!)
  • E.g.,
  • int a new int12System.out.println(a.lengt
    h) // Prints out 12

29
Initializing arrays
  • Typically, loops are used
  • If all values are known beforehandint a
    0, 1, 2, 3, 4, 5
  • No need to specify how many elements - inferred
    from the assignment.
  • E.g.,double d 0.5, 1.2, 500.201 char
    vowels a, e, i, o, u

30
Example reversing an array
  • public class ArrayReverser
  • public static void main(String args)
  • int a 1, 2, 3, 4, 5
  • int temp for( int i0 i lt (a.length / 2)
    i )
  • temp ai
  • ai a a.length - i - 1
  • aa.length - i - 1 temp
  • for( int i0 i lt a.length i
    ) System.out.println(i ai)

31
Summary (arrays)
  • Use arrays to store collections of similar data
  • Declaring an array variable does not allocate it
    new allocates it
  • Elements are numbered 0..length-1
  • Initialize using comma-separated lists in s,
    or loops

32
Quiz 1
  • Will be held at start of class
  • 60 minutes
  • Closed book/notes, all you need is a pen/pencil
    and your student ID.
  • Covers first four lectures
  • Marks break-up Multiple choice (20),
    true/false(10), Programming(20)
  • Review session time/place TBD

33
Quiz 1 Material
  • Java basics
  • variables, data types, operators, expressions,
    simple I/O
  • Statements, compound statements
  • Conditions
  • Control flow
  • if/else, switch
  • Loops for, while, do-while
  • Loop control break, continue
  • Arrays
Write a Comment
User Comments (0)
About PowerShow.com