Title: ENGINEERING COMPUTING ENG1602
1ENGINEERING COMPUTING ENG1602
BACHELOR of ENGINEERING
LECTURES Semester 1 2004
2ENGINEERING COMPUTING ENG1602
Week 6 - Lecture 2 Conditional Loops
Switch Statement
3Conditional 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
41. do while loop
- Chapman p. 97.
- SYNTAX
- do
-
- while ltBoolean
Expressiongt
ltStatement 1gt ltStatement 2gt .. .. ltStatement ngt
Loop Body
5do 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.
6do while Flow chart
ltBody of Loopgt
true
ltBoolean Expressiongt
false
7do 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.
8do while Examples
- Example 1 N 1 do
System.out.print( N ) N N
1 - while (N lt 5)
- Output 1 2 3 4
9Example 2.
- N 1 do System.out.print( N
) while (N lt 5) - Output 1 1 1 1 1 .....Infinite Loop
10Another 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))
112. WHILE LOOP
- while ltBoolean Expressiongt
-
ltStatement 1gt ltStatement 2gt .. ltStatement ngt
12WHILE - 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.
13WHILE Flow chart
ltBoolean Expressiongt
TRUE
ltBody of Loopgt
FALSE
14WHILE 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 ).
15WHILE 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
16Choosing 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.
17Nested IF StatementThree possible outcomes
Statement-1
True
False
IF 1
True
False
IF 2
Statement3
Statement2
Statement1
Statement1
18NESTED IF Statement
Statement-1Before IF if ltExpression 1gt
if ltExpression2 gt Statement3
else Statement2
else Statement1
Statement1 After IF
19Nested 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)
20Choosing 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 .
21switch 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
22switch 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
23switch 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.
24switch 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' )
25switch 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
26switch 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.
27Switch 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
28Switch 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)