java 03 dse part2

Upload: liesel-huesca

Post on 03-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Java 03 DSE Part2

    1/18

    Chapter 3 Control Statements

    Selection StatementsUsing if and if...else

    Nested if Statements

    Using switch Statements

    Conditional Operator

    Repetition Statements

    Looping: while, do-while,and for

    Nested loops

    Using break and continue

  • 7/28/2019 Java 03 DSE Part2

    2/18

    Repetitions

    while Loops

    do-while Loops

    for Loops

    break and continue

  • 7/28/2019 Java 03 DSE Part2

    3/18

    while Loop Flow Chart

    false

    true

    Statement(s)

    Next

    Statement

    Continuation

    condition?

    while (continuation-condition) {// loop-body;

    }

  • 7/28/2019 Java 03 DSE Part2

    4/18

    while Loop Flow Chart, cont.

    int i = 0;while (i < 100) {System.out.println("Welcome to Java!");

    i++;}

    false

    true

    System.out.println("Welcoem to Java!");

    i++;

    NextStatement

    (i < 100)

    i = 0;

  • 7/28/2019 Java 03 DSE Part2

    5/18

    Example 3.2: Using while Loops// TestWhile.java: Test the while loop

    import javax.swing.JOptionPane;

    public class TestWhile {

    /** Main method */

    public static void main(String[] args) {

    int data;

    int sum = 0;

    // Read an initial data

    String dataString = JOptionPane.showInputDialog(null,

    "Enter an int value, \nthe program exits if the input is 0",

    "Example 3.2 Input", JOptionPane.QUESTION_MESSAGE);

    data = Integer.parseInt(dataString);

    // Keep reading data until the input is 0

    while (data != 0) {

    sum += data;

    // Read the next data

    dataString = JOptionPane.showInputDialog(null,

    "Enter an int value, \nthe program exits if the input is 0",

    "Example 3.2 Input", JOptionPane.QUESTION_MESSAGE);

    data = Integer.parseInt(dataString);

    }

    JOptionPane.showMessageDialog(null, "The sum is " + sum,

    "Example 3.2 Output", JOptionPane.INFORMATION_MESSAGE);

    System.exit(0);

    }

    }

  • 7/28/2019 Java 03 DSE Part2

    6/18

    Caution

    Dont use floating-point values for equality checking in a

    loop control. Since floating-point values areapproximations, using them could result in imprecise

    counter values and inaccurate results. This example uses

    int value for data. If a floating-point type value is used for

    data, (data != 0) may be true even though data is 0.

    // data should be zero

    double data = Math.pow(Math.sqrt(2), 2) - 2;

    if (data == 0)

    System.out.println("data is zero");

    else

    System.out.println("data is not zero");

  • 7/28/2019 Java 03 DSE Part2

    7/18

    do-while Loop

    false

    true

    Statement(s)

    Next

    Statement

    Continue

    condition?

    do {

    // Loop body;

    } while (continue-condition);

  • 7/28/2019 Java 03 DSE Part2

    8/18

    for Loopsfor (initial-action; loop-continuation-condition;

    action-after-each-iteration) {//loop body;

    }

    int i = 0;

    while (i < 100) {

    System.out.println("Welcome to Java! + i);

    i++;

    }

    Example:

    int i;for (i = 0; i < 100; i++) {

    System.out.println("Welcome to Java! + i);

    }

  • 7/28/2019 Java 03 DSE Part2

    9/18

    for Loop Flow Chart

    Initial-Action

    false

    true

    Action-After-

    Each-Iteration

    Statement(s)

    (loop-body)

    Next

    Statement

    Continuation

    condition?

    for (initial-action;loop-continuation-condition;action-after-each-iteration) {//loop body;

    }

  • 7/28/2019 Java 03 DSE Part2

    10/18

    for Loop Example

    i

  • 7/28/2019 Java 03 DSE Part2

    11/18

    Which Loop to Use?

    The three forms of loop statements, while, do, and for, are

    expressively equivalent; that is, you can write a loop in

    any of these three forms.

    I recommend that you use the one that is most intuitive

    and comfortable for you. In general, a for loop may beused if the number of repetitions is known, as, for

    example, when you need to print a message 100 times. A

    while loop may be used if the number of repetitions is not

    known, as in the case of reading the numbers until the

    input is 0. A do-while loop can be used to replace a while

    loop if the loop body has to be executed before testing the

    continuation condition.

  • 7/28/2019 Java 03 DSE Part2

    12/18

    Caution

    Adding a semicolon at the end of the for clause

    before the loop body is a common mistake, as

    shown below:

    for (int i=0; i

  • 7/28/2019 Java 03 DSE Part2

    13/18

    Caution, cont.Similarly, the following loop is also wrong:int i=0;while (i

  • 7/28/2019 Java 03 DSE Part2

    14/18

    The break Keyword

    false

    true

    Statement(s)

    Next

    Statement

    Continuation

    condition?

    Statement(s)

    break

  • 7/28/2019 Java 03 DSE Part2

    15/18

    The continue Keyword

    false

    true

    Statement(s)

    Next

    Statement

    Continue

    condition?

    Statement(s)

    continue

  • 7/28/2019 Java 03 DSE Part2

    16/18

    Using breakExample for using the break keyword: 3.5

    // TestBreak.java: Test the break keyword in the loop

    public class TestBreak {

    /** Main method */

    public static void main(String[] args) {

    int sum = 0;

    int item = 0;

    while (item < 5) {

    item ++;

    sum += item;

    //if (sum >= 6) break;

    }

    System.out.println("The sum is " + sum);

    }

    }

  • 7/28/2019 Java 03 DSE Part2

    17/18

    Using continueExample for using the continue keyword:

    // TestContinue.java: Test the continue keywordpublic class TestContinue {

    /** Main method */

    public static void main(String[] args) {

    int sum = 0;

    int item = 0;

    while (item < 5) {

    item++;

    if (item == 2) continue;

    sum += item;

    }

    System.out.println("The sum is " + sum);

    }

    }

  • 7/28/2019 Java 03 DSE Part2

    18/18

    Review of Terms: Deenas Class

    The 3 essential elements of a programming loop:

    priming, test, and update

    User-controlled loops vs Program-controlled loops

    Some user-controlled loops usesentinels.

    AccumulatorandMarkervariables: how are these

    used in loops?

    What is aflagvariable? When do you use it?