Chapter 5: Control Structure Part 2 - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Chapter 5: Control Structure Part 2

Description:

for key word control variable name Final value of control variable ... counter 10, the loop would be only executed nine times. ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 36
Provided by: tuonguye
Category:

less

Transcript and Presenter's Notes

Title: Chapter 5: Control Structure Part 2


1
Chapter 5 Control Structure
Part 2
  • Presented by Tuong-Uyen Nguyen

2
Objectives
  • 5.1 Introduction
  • 5.2 Essentials of Counter-Controlled Repetition
  • 5.3 The for Repetition Structure
  • 5.4 Example Using the for Structure
  • 5.5 The Multiple-Selection Structure
  • 5.6 The do/while Repetition Structure
  • 5.7 The break and continue Statements
  • 5.8 The Labeled break and continue Statements
  • 5.9 Logical Operators
  • 5.10 Structured Programming Summary

3
5.1 Introduction
  • Before writing a program to solve a particular
    problem
  • - Understand the problem.
  • - Plan approach to solving the
    problem.
  • When writing a program
  • - Understand the types of building
    blocks
  • that are available and to employ
    proven
  • program construction principles.

4
5.2 Essentials of Counter- Controlled
Repetition
  • Counter-controlled repetition requires
  • 1. The name of control variable (or loop
    counter).
  • 2. The initial value of the control variable.
  • 3. The increment (or decrement) by which the
  • control variable is modified each time
    through
  • the loop.
  • 4. The condition that tests for the final
    value of the
  • control variable.

5
5.2 (cont)
  • Simple applet shown in Fig. 5.1 / page 159

6
5.2 (cont)
  • int counter 1
  • names the control variable (counter),
  • declares it to be an integer.
  • initial value of 1.
  • int counter // declare counter
  • counter 1 // initialize counter to
    1
  • Line 12 in the while structure
  • g.drawLine( 10, 10, 250, counter 10 )
  • Graphics reference g, which refers to the
    applets Graphics object, to send the drawLine
    message to the Graphics object asking it to draw
    a line.

7
5.2 (cont)
  • Line 13 in the while structure
  • counter //increment
  • increments the control variable by 1 each
    iteration of the loop.
  • The program in Fig. 5.1 can be more concise by
    initializing counter to 0
  • while ( counter lt 10 ) //repetition
    condition
  • g.drawLine( 10, 10, 250, counter 10 )

8
5.3 The for Repetition Structure
  • The for repetition structure handles all the
    details of counter-controlled repetition.

9
5.3 (cont)
  • The applets paint method operates as follows
  • When the for structure (line 11) begins
    executing, the control variable counter is
    initialized to 1.
  • Next, the loop-continuation condition counterlt10
  • is checked. The condition contains the
    final value(10) of the control variable.
  • Because the initial value of counter is 1, the
    condition is satisfied, so the body statement
    (line 12) draws a line.

10
5.3 (cont)
  • Variable counter is then incremented in the
    expression counter, the loop begins again with
    the loop-continuation test.
  • This process continues until the control variable
    counter is incremented to 11, this causes the
    loop-continuation test to fail and repetition
    terminates.
  • The program continues by performing the first
    statement after the for structure.

11
5.3 (cont)
  • for key word control variable name
    Final value of control variable
  • for ( int counter 1 counter lt
    10 counter )
  • Initial value of control variable
    Increment control variable

  • Loop-continuation condition
  • is called the for structure header. If there
    is
  • more than one statement in the body of the for
  • Braces ( and ) are required to define the
    body of the loop.

12
5.3 (cont)
  • An off-by-one error
  • Instead of coding counter lt 10, writing
  • counter lt 10, the
    loop would be only executed nine times.
  • The general format of the for structure
  • for ( expression1 expression2
    expression3 )
  • statement

13
5.3 (cont)
  • expression1 names the loops control variable and
    provides its initial value.
  • expression2 is the loop-continuation condition.
  • expression3 increments the control variable.
  • An equivalent while structure
  • expression1
  • while ( expression2 )
  • statement
  • expression

14
5.4 Examples Using the for Structure
  • Fig. 5.4 Flowcharting a typical for repetition
    structure.

15
5.4 (cont)
  • 1. Vary the control variable from 1 to 100 in
    increments of 1.
  • for ( int I 1 i lt 100 i )
  • 2. Vary the control variable form 100 to 1 in
    increments of 1 (decrements of 1).
  • for ( int i 100 i gt 1 i-- )
  • 3. Vary the control variable from 20 to 2 in
    steps of 2
  • for ( int i 20 i gt 2 i -
    2 )

16
Fig. 5.5 Summation with for (even integer
from 2-100)
17
Fig. 5.6 Calculating compound
interest with for
18
5.5 The switch Multiple-Selection
Structure
  • The switch multiple-selection structure contains
    a series of decisions in which a variable or
    expression is tested separately for each of the
    constant integral values.

19
Fig. 5.7 An Example Using switch
20
5.5 (cont)
  • 7 int choice
  • Line 7 in applet SwitchTest defines instance
    variable choice of type int.
  • 9 public void init( )
  • Line 9 init declares local variable input of
    type String at line 11.
  • 13 input
    JOptionPane.showInputDialog(
  • 14
    Enter 1 to draw lines n
  • 15
    Enter 2 to draw rectangles n
  • 16
    Enter 3 to draw ovals n )
  • Lines 13-16 display the input dialog with static
    method JOptionPane.showInputDialog

21
Fig. 5.8 The switch multiple-
selection structure
22
5.6 The do/while Repetition
Structure
  • Tests the loop-continuation condition after
  • the loop body is performed.
  • The loop body is always executed once.
  • When a do/while terminates, execution continues
    with the statement after the while
  • statement
  • clause.
  • do
  • while ( condition )

23
Fig. 5.9 Using the do/while
Repetition Structure
24
Fig. 5.10 Flowchart the do/while
Repetition Structure
action(s)
true
condition
false
25
5.7 The break and continue Statements
  • The break statement, when executed in a while,
    for, do/while or switch structure, causes
    immediate exit from that structure.
  • The breaks common use is to escape early from
    the loop or to skip the remainder of a switch
    structure.
  • The continue statement, when executed in a while,
    for or do/while structure, skips the remaining
    statement in the body, and proceeds with the next
    iteration of the loop.

26
Fig. 5.11 Using the break statement
in a for structure
27
Fig. 5.12 Using the continue statement
in a for structure
28
5.8 The Labeled break and continue
Statements
  • To break out of a nested set of structures.
  • Commonly used to terminate nested looping
    structures containing while, for, do/while or
    switch structure.

29
Fig. 5.13 Using a labeled break statement
in a nested for structure
30
5.9 Logical Operators
  • (logical AND) gender 1 age gt 65
  • (boolean logical AND)
  • // (logical OR) semesterAverage gt 90 //
    finalExam gt 90
  • / ( boolean logical inclusive OR)
  • (boolean logical exclusive OR)
  • ! (logical NOT)

31
Truth Tables of 4 Operators
32
Fig. 5.14 Using labeled continue statement in
a nested for structure
33
5.10 Structured Programming Summary
  • Three forms of control are needed
  • 1. Sequence
  • 2. Selection
  • 3. Repetition
  • Sequence is trivial. Selection is implemented in
    3 ways
  • 1. if structure (single selection)
  • 2. if/else structure ( double selection)
  • 3. switch structure ( multiple selection)
  • Repetition is implemented in 3 ways
  • 1. while structure
  • 2. do/while structure
  • 3. for structure

34
Fig. 5.21 on page 188
35
QUESTIONS???
Web-Site
http//students.ou.edu/N/Tuong-Uyen.P.Nguyen-1/MIS
3033
The End.
Write a Comment
User Comments (0)
About PowerShow.com