BIS120 Business Applications of Java - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

BIS120 Business Applications of Java

Description:

Typically, a break statement is at the end of each case block, which causes the ... If we took out the break statements, when the user chose option 1, the program ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 23
Provided by: M644
Category:

less

Transcript and Presenter's Notes

Title: BIS120 Business Applications of Java


1
BIS120Business Applications of Java
  • 09 / 24 / 2002
  • Michael Eckmann

2
Topics to be covered
  • Nested loops example (exercise 5.5)
  • switch structure (case labels, break statements,
    ...)
  • do while structure
  • break and continue statements
  • labeled break and continue statements
  • Homework assignment to be sent out by email and
    posted on our webpage before 5pm today.

3
Exercise 5.5 what prints?
  • public class printing
  • public static void main(String args)
  • for ( int i 1 i
  • for ( int j 1 j
  • System.out.print("_at_")
  • System.out.println()

4
switch structures
  • With if else structures, we made one test
    (condition) and were able to select among two
    different pieces of code to execute.
  • switch structures give us the ability to select
    among many different pieces of code to execute.
    However, they only work with integral values,
    that is values of the following types (long, int,
    short, byte and char.)
  • Note that char is in this list. A variable of
    type char is actually considered an integral type
    --- that is, it is stored internally as a 16 bit
    integer number.

5
switch structures
  • So, if you have many possible values for some
    expression or variable of one of the integral
    types, and you wish to do something different
    based on these values, then a switch statement is
    a good choice.
  • It is excellent for menu type selections ---
    when a user makes a selection from a list of
    possible numbers.
  • Example (from Deitel and Deitel figure 5.7) menu
    asking for input
  • 1. draw lines
  • 2. draw rectangles
  • 3. draw circles
  • Enter a number

6
switch structures
  • // typical general syntax of the switch
    structure
  • switch ( expression_or_variable )
  • case integral_value1
  • statements_to_do_if_expression
    _or_variable_equals_integral_value1
  • break
  • case integral_value2
  • statements_to_do_if_expression_or_v
    ariable_equals_integral_value2
  • break
  • (can have as many case statements as you
    want ...)
  • default
  • statements_to_do_if_by_default
  • // end switch

7
switch structures
  • The switch expression or variable is evaluated
    (lets call this the switch value.
  • This switch value is compared with the first case
    statement.
  • If they are equal, then the statements within the
    case are executed. If they are not equal, the
    next case statement is compared with the switch
    value and so on until one is found that is equal.
    When a case statement is equal to the switch
    value, then the statements within the case are
    executed.

8
switch structures
  • Typically, a break statement is at the end of
    each case block, which causes the execution to
    jump out of the switch structure (to the end of
    it, and start executing statements after the ,
    right curly brace.)
  • If there is not a break statement at the end of
    the case block, then execution continues (falls
    through) into the next case block and so on
    until a break is encountered or it gets to the
    end of the switch structure.
  • An optional default case may appear at the end of
    the switch structure.

9
example switch
  • switch( choice )
  • case 1
  • g.drawLine( 10, 10, 250, 10
    i 10 )
  • break
  • case 2
  • g.drawRect( 10 i 10, 10
    i 10,
  • 50 i
    10, 50 i 10 )
  • break
  • case 3
  • g.drawOval( 10 i 10, 10
    i 10,
  • 50 i
    10, 50 i 10 )
  • break
  • default
  • JOptionPane.showMessageDialog(
  • null, "Invalid value entered" )
  • // end switch (from Deitel and Deitel
    figure 5.7)

10
switch structures
  • In the last example, we saw break statements
    within each case block. These caused the program
    execution to jump out of the switch structure.
  • If we took out the break statements, when the
    user chose option 1, the program would draw
    lines, then rectangles, then circles, then also
    show a dialog with invalid value message.
    Option 2 would draw rectangles, then circles then
    the message. Option 3 would draw circles then
    the message.

11
example switch
  • switch( choice )
  • case 1
  • g.drawLine( 10, 10, 250, 10
    i 10 )
  • case 2
  • g.drawRect( 10 i 10, 10
    i 10,
  • 50 i
    10, 50 i 10 )
  • case 3
  • g.drawOval( 10 i 10, 10
    i 10,
  • 50 i
    10, 50 i 10 )
  • default
  • JOptionPane.showMessageDialog(
  • null, "Invalid value entered" )
  • // end switch (modified figure 5.7
    from Deitel and Deitel)

12
switch structures
  • Note that the choice variable in the switch was
    of type int.
  • Common mistakes with switch structures include
    the use of non integral types in the switch.
  • Example
  • String input_str
  • // set input_str somehow here
  • switch ( input_str ) // this is an error
    because input_str is a String
  • case A
  • // . . .

13
switch structures
  • break statements are almost always desired at the
    end of each case block. It is a common mistake
    to omit these breaks and therefore have the
    program execute code in more than one case block
    unintentionally.
  • Example (bad because no break statements)
  • switch ( choice )
  • case 1
  • System.out.println(Use entered 1)
  • case 2
  • System.out.println(Use entered 2)
  • // logic error this
    will get printed when choice 1 or 2

14
do while structures
  • the do while structure is very similar to the
    while structure with one main difference.
  • In a do while loop, the condition is tested after
    each iteration of the loop (in contrast, the
    while loop which weve seen, the condition is
    tested before each iteration of the while loop.)
  • Therefore, with a do while loop, the body of the
    loop executes at least once always.
  • In contrast, the while loop tests the condition
    first and if it is false, the while loop body
    would not execute even once.

15
do while structures
  • General syntax of a do while loop.
  • do
  • statements
  • while (condition) // end of do while loop

16
do while structures
  • Lets convert a program we did with a while loop
    to instead use a do while loop.
  • int N1
  • do
  • System.out.println(" " N
  • " " 10N
  • " " 100N
  • " " 1000N)
  • N
  • while (N

17
break and continue statements
  • break statement within a while, for, do while or
    switch structure
  • as we saw with the switch structures, break
    statements cause the immediate exit from the
    switch structure. Program execution continues
    with the statement after the switch structure.
  • break statements can be used not only in switch
    structures but also, while loops, do while loops,
    and for loops. They cause the immediate exit
    from the structure and program execution
    continues with the statement after the structure.

18
break and continue statements
  • continue statement within a while, for, or do
    while structure
  • a continue statement when executed in these
    structures, causes the program to skip the
    remaining code in the loop body and proceeds with
    the next iteration of the loop.
  • in while and do while loops, the condition is
    tested immediately after the continue statement
    executes
  • in a for loop, after the continue statement
    executes, the increment expression executes then
    condition is tested

19
break and continue statements
  • Figure 5.11 and 5.12 in Deitel and Deitel

20
labeled break continue statements
  • labeled break statements within nested structures
    (while, for, do while or switch structure)
  • A label can be given to a block of code. A
    block of code is defined as code enclosed in a
    pair of curly braces.
  • The labeled break statement can specify the block
    to jump out of. Non-labeled break statements
    could only jump out of the immediately enclosed
    structure, whereas, labeled break statements can
    jump totally out of a set of nested structures.

21
labeled break continue statements
  • labeled continue statements within nested
    structures (while, for, or do while structure)
  • A label can be given to a structure.
  • The labeled continue statement causes execution
    to skip the code in the enclosing structures and
    continues with the next iteration of the
    structure with the specified label.

22
labeled break continue statements
  • Figure 5.13 and 5.14 in Deitel and Deitel
Write a Comment
User Comments (0)
About PowerShow.com