ENGINEERING COMPUTING ENG1602 - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

ENGINEERING COMPUTING ENG1602

Description:

System.out.println('A Leap Year'); break; case 3: System.out.println('Maybe Next Year' ... System.out.println( 'Not a Leap Year' ); switch Example2 ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 30
Provided by: dsu3
Category:

less

Transcript and Presenter's Notes

Title: ENGINEERING COMPUTING ENG1602


1
ENGINEERING COMPUTING ENG1602
BACHELOR of ENGINEERING
LECTURES Semester 1 2004
2
ENGINEERING COMPUTING ENG1602
Week 6 - Lecture 2 Conditional Loops
Switch Statement
3
Conditional Loops
  • The For .. Loop construct considered previously
    repeats a set of instructions a number of times -
    the number of times being controlled by the index
    range of the loop variable.
  • There are two other possibilities which are
    controlled by an associated condition being
    either True or False
  • while loop
  • do while loop

4
1. do while loop
  • Chapman p. 97.
  • SYNTAX
  • do
  • while ltBoolean
    Expressiongt

ltStatement 1gt ltStatement 2gt .. .. ltStatement ngt
Loop Body
5
do while Loop - Notes
  • Loop Body always Executes at least once.
  • ltBoolean Expressiongt true Statements
    executed again from do.
  • ltBoolean Expressiongt false Program
    goes to next statement following while and
    continues execution.

6
do while Flow chart
ltBody of Loopgt
true
ltBoolean Expressiongt
false
7
do while The Trap!
  • To STOP a do while Loop, one of the
    variables used in the controlling ltBoolean
    expressiongt must change within the loop each
    time the loop is executed.
  • Otherwise the ltBoolean Expressiongt wilI never
    come false, and the loop will never terminate
    thus becoming Endless or Infi
    nite.

8
do while Examples
  • Example 1 N 1 do
    System.out.print( N ) N N
    1
  • while (N lt 5)
  • Output 1 2 3 4

9
Example 2.
  • N 1 do System.out.print( N
    ) while (N lt 5)
  • Output 1 1 1 1 1 .....Infinite Loop

10
Another do while Example
  • StdIn in new StdIn()
  • int Number
  • do
  • System.out.println(Input a Number 0-10)
  • Numberin.readInt()
  • while ((Number gt 10) (Number lt 0))

11
2. WHILE LOOP
  • while ltBoolean Expressiongt

ltStatement 1gt ltStatement 2gt .. ltStatement ngt
12
WHILE - LOOP
  • Loop Only Executes if ltBoolean Expressiongt is
    TRUE at the start.
  • Once started something must change in the Loop
    Body to insure ultimate termination.

13
WHILE Flow chart
ltBoolean Expressiongt
TRUE
ltBody of Loopgt
FALSE
14
WHILE Example
  • Read Numbers from the Keyboard until a negative
    number is detected.
  • THEN Determine the AVERAGE of all entered numbers
    excluding the last ( negative number used to
    terminate the process ).

15
WHILE Example
  • double Next,Sum,Average
  • int Count
  • StdIn in new StdIn()
  • Sum 0//Preset Variables
  • Count 0
  • Nextin.readDouble()//Read First Number
  • while ( Next gt 0 )
  • Count Count 1 Sum Sum
    Next Nextin.readDouble()
  • Average Sum / Count

16
Choosing Optionsswitch Statement
  • IF - THEN Allows selection and execution
    of ONE Option if TRUE.
  • IF - THEN - ELSE Allows selection and
    execution of one of two Alternatives depending
    upon whether TRUE or FALSE.
  • NESTED IF s Allow selection and execution
    of ONE of a number of Options. However as the
    number of options increases the construction
    becomes clumsy.

17
Nested IF StatementThree possible outcomes
Statement-1
True
False
IF 1
True
False
IF 2
Statement3
Statement2
Statement1
Statement1
18
NESTED IF Statement
Statement-1Before IF if ltExpression 1gt
if ltExpression2 gt Statement3
else Statement2
else Statement1
Statement1 After IF
19
Nested IF Statement -Example
if (Guess gt Number) System.out.println(Too
High) else if (Guess lt Number)
System.out.println(Too Low) else
System.out.println(Correct)
20
Choosing Optionsswitch Statement
  • Chapman p. 82
  • The switch Statement Simple
    statement that allows selection and execution of
    one of many options.
  • The Branching Decision is based upon the value of
    a variable of type integer or character .

21
switch Statement
Statement 1
Evaluate Controlling Expression
on Label List A
on Label List B
on Label List N
Statement 2A
Statement 2B
Statement 2C
Statement 3
22
switch Statement Syntax
  • Statement 1 Before switch switch
    ltExpressiongt ltLabel List Agt
    ltStatement 2Agt ltLabel List Bgt ltStatement
    2Bgt Etc. ltLabel List Ngt
    ltStatement 2Ngt Stateme
    nt 2After CASE

23
switch Statement - Limitations
  • The Values used in the Lists must be literals or
    constants of the same type as the controlling
    expression
  • As a general rule ltExpressiongt must evaluate
    to either TYPE INTEGER or TYPE CHAR.
  • REAL or STRING TYPES are NOT Allowed.

24
switch Example 1
  • Assuming Variable "Year" in an INTEGER set to
    have the value of the current year Eg. 2001,
    then the following switch statement may be
    written
  • switch ( Year 4 )
  • case 0
  • System.out.println(A Leap Year)
  • break
  • case 3
  • System.out.println(Maybe Next Year)
  • break
  • case 1
  • case 2
  • System.out.println( 'Not a Leap Year' )

25
switch Example2
  • switch ltExpressiongt evaluates to a label not to
    be found in the label list.
  • switch (N)
  • case 2
  • System.out.println(Value of N is 2)
  • case 4
  • System.out.println(Value of N is 4)
  • What happens if N 0,1,3,5,6 etc. ? ( i.e. a
    label not in the list ) ?
  • No branch executed if want to catch these then
    use default branch

26
switch Example 3
  • switch ( N )
  • case 2
  • System.out.println(N is 2)
  • break
  • case 4
  • System.out.println(N is 4)
  • break
  • default
  • System.out.println(N is not 2 or 4)
  • break
  • This construction gives an output for all
    possible values of N.

27
Switch example 4
  • Assume Mark is int and grade is char
  • switch (Mark/10)
  • case 8 case 9 case 10
  • Grade'H'
  • break
  • case 7 Grade'D'
  • break
  • case 6 Grade'C'
  • break
  • case 5 Grade'P'
  • break
  • case 0 case 1 case 2 case 3 case 4
  • Grade'N'
  • break

28
Switch notes
  • break is placed between case branches to prevent
    fall through. Actual meaning of switch
    statement is that execution jumps to FIRST branch
    with matching label and then follows through all
    subsequent branches unless break is encountered
    to break out of switch.
  • Thus a break after the last branch is superfluous
    BUT YOU ARE ADVISED PUT ONE IN BY HABIT. This
    avoids bugs when add to the cases and forget to
    go back and put in a break after what was the
    last branch.

29
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com