1 g54prg programming lecture 1 amadeo ascó adam moore 6 decision making, control structures &...
Post on 17-Dec-2015
219 Views
Preview:
TRANSCRIPT
1
G54PRG ProgrammingLecture
1
Amadeo Ascó Adam Moore
6
Decision Making, Control Structures & Error Handling
2Amadeo Ascó , Adam Moore
Previously
• Variables: hold information• Primitive Data Types
– boolean true and false
– byte 1byte, -128 to 127
– char 2bytes, from 0 to 65536 (short 2bytes number)
– int 4bytes, from Integer.MIN_VALUE to Integer. MAX_VALUE
– long 8bytes, from Long.MIN_VALUE to Long. MAX_VALUE
– float 4bytes , from Float.MIN_VALUE to Float. MAX_VALUE
– double 8bytes , from Double.MIN_VALUE to Double. MAX_VALUE
• Other Data Types: Objects
3Amadeo Ascó , Adam Moore
Overview
• Decision Making
• Control Structures
• Error Handling
4Amadeo Ascó , Adam Moore
Decision Making
The Traffic lightan example
5Amadeo Ascó , Adam Moore
if red or amber thenif red or amber thenif red or amber thenstop
if red or amber thenstop
if red or amber thenstop
elseif stopped then
startelse
if red or amber thenstop
elseif stopped then
startelse
if red or amber thenstop
else
if red or amber thenstop
else
if red or amber thenstop
elseif stopped then
startelse
continue
if red or amber thenstop
elseif stopped then
startelse
continue
if red or amber thenstop
elseif stopped then
startelse
continue
if red or amber thenstop
elseif stopped then
startelse
continue
Decision Making• Traffic lights
– Red or amber then stop– Green and stopped then start– Green and car running continue
• Some decisions are required• Pseudocode
6Amadeo Ascó , Adam Moore
• if Statement– if-else branching
Examples:Count and re-set to zero when counter is 100
Decision Making
if red or amber thenstop
elseif stopped then
startelse
continue
if red or amber thenstop
elseif stopped then
startelse
continue
if (bRed || bAmber) { …} else { if (bStopped) { … } else { … }}
if (bRed || bAmber) { …} else { if (bStopped) { … } else { … }}
7Amadeo Ascó , Adam Moore
Decision Making
int iCounter = 0; // initialise counter...
if (iCounter == 100) {// reset counteriCounter = 0;
}...
8Amadeo Ascó , Adam Moore
• Format
Decision Making
if (condition) { statements}
if (condition) { statements}
if (condition) { statements1} else { statements2}
if (condition) { statements1} else { statements2}
if (condition1) { statements1} else if (condition2) { statements2} else { statements3}
if (condition1) { statements1} else if (condition2) { statements2} else { statements3}
9Amadeo Ascó , Adam Moore
• Conditions result in boolean values• Condition can be
– A boolean– Expressions that result in a boolean– Combination of
Rational Operators and Method that return boolean
Decision Making
10Amadeo Ascó , Adam Moore
• Rational Operators== is equal to!= is not equal to> is bigger than>= is bigger or equal to< is smaller than<= is smaller or equal to|| logical OR&& logical AND
Decision Making
counterA == counterB
counterA != counterB
counterA > counterB
counterA >= counterB
counterA < counterB
counterA <= counterB
(counterA > counterB) || (counterA < counterB)
(counterA > counterB) && (counterA < counterB)
11Amadeo Ascó , Adam Moore
• switch Statement– A way to simulate multiple if statements
int iCounter = 0; // initialisationint iValue;...
if (iCounter == 0) { iValue = -1;} else if (iCounter == 1) { iValue = -2;} else if (iCounter == 2) { iValue = -3;} else { iValue = -4;}
Decision Making
int iCounter = 0; // initialisationint iValue;...
switch (iCounter) { case 0: iValue = -1; break; case 1: iValue = -2; break; case 2: iValue = -3; break; default: iValue = -4;} // end switch
12Amadeo Ascó , Adam Moore
– Formatswitch (expression) {
case constant1:statements1
case constant2:statements1
…default:
statements}
– The expression must be a char, byte, short, int or enum
Decision Makingint iCounter = 0; // initialisationString strMsg;...switch (iCounter) { case 0: strMsg = "1st "; break; case 1: strMsg = "2nd"; break; case 2: strMsg = "3rd"; break; default: strMsg = "4th";} // end switch
int iCounter = 0; // initialisationString strMsg;...switch (iCounter) { case 0: strMsg = "1st "; break; case 1: strMsg = "2nd"; break; case 2: strMsg = "3rd"; break; default: strMsg = "4th";} // end switch
13Amadeo Ascó , Adam Moore
Control Structures
• Loops– while
– do
– for
• Loop Control– break
– continue
14Amadeo Ascó , Adam Moore
Control Structures
• while loop
while (condition) { … statements … } // end while
– The condition must result in a boolean– It may not pass through the statements even once
15Amadeo Ascó , Adam Moore
024
Control Structures
int iIndex = 0; // initialise
while (iIndex < 4) { System.out.println(iIndex); iIndex += 2; // add two} // end whileSystem.out.println(iIndex);...
int iIndex = 0; // initialise
while (iIndex < 4) { System.out.println(iIndex); iIndex += 2; // add two} // end whileSystem.out.println(iIndex);...
indexindex
024
16Amadeo Ascó , Adam Moore
Control Structures
• do loop
do { … statements … } while (condition);
– The condition must result in a boolean– At least it will pass through the statements once
17Amadeo Ascó , Adam Moore
int iIndex = 0; // initialise
do { System.out.println(iIndex); iIndex += 2; // add two} while (index < 4);System.out.println(iIndex);...
024
Control Structures
indexindex
024
18Amadeo Ascó , Adam Moore
int iIndex = 4; // initialise
do { System.out.println(iIndex); iIndex += 2; // add two} while (index < 4);System.out.println(iIndex);
Control Structuresint iIndex = 4; // initialise
while (iIndex < 4) {
System.out.println(iIndex);
iIndex += 2; // add two
} // end whileSystem.out.println(iIndex);4
64
19Amadeo Ascó , Adam Moore
Control Structures
int iIndex = 0; // initialise
do { System.out.println(iIndex); iIndex += 2; // add two} while (index < 4);System.out.println(iIndex);...
int iIndex = 0; // initialise
while (iIndex < 4) {
System.out.println(iIndex);
iIndex += 2; // add two
} // end whileSystem.out.println(iIndex);...
20Amadeo Ascó , Adam Moore
Control Structures
• for loop
for (from; condition; change) { … statements … } // end for
– The from is a definition or initialisation (optional)– The condition must result in a boolean– The change must be an statement(s)– It may not pass through the statements even once
21Amadeo Ascó , Adam Moore
for (int iIndex = 0; iIndex < 4; iIndex += 2) { System.out.println(iIndex);} // end forSystem.out.println(iIndex);
024
Control Structures
indexindex
024
22Amadeo Ascó , Adam Moore
for (int iIndex = 0; iIndex < 4; iIndex += 2) { System.out.println(iIndex);} // end forSystem.out.println(iIndex);...
Control Structures
int iIndex = 0; // initialise
do { System.out.println(iIndex); iIndex += 2;} while (iIndex < 4);System.out.println(iIndex);...
int iIndex = 0; // initialise
while (iIndex < 4) { System.out.println(iIndex); iIndex += 2;} // end whileSystem.out.println(iIndex);...
InitialisationConditionIncrementInitialisation - Condition - Increment
23Amadeo Ascó , Adam Moore
Control Structures
• break– Stop the loop and leave it
• continue– Go to the starting of the
loop and continue the execution
int counter = 0; // initialise do { if (counter == 2) { counter = 4; continue; } else if (counter == 5) { break; } ++counter;} while (counter < 10);...
int counter = 0; // initialise do { if (counter == 2) { counter = 4; continue; } else if (counter == 5) { break; } ++counter;} while (counter < 10);...
24Amadeo Ascó , Adam Moore
0int iCounter = 0; // initialise do { if (iCounter == 1) { iCounter = 4; continue; } else if (iCounter == 5) { break; } ++iCounter;} while (iCounter < 10);...
514
Control Structures
countercounter
25Amadeo Ascó , Adam Moore
Error Handling
• Help to increase robustness
• Elegant way to handle errors
• Allows to detect errors easily
• Keep handling code separate from generating error
26Amadeo Ascó , Adam Moore
Error Handling
• Format– Generating error
• It is thrown where the error is found
throw new ExceptionType(…);
• The method where it is thrown from must identify the thrown exception with the keyword throws
public static void main(String[] astrArgs) throws ExceptionType
• RuntimeExceptions don’t need to be declared in the signature of the method that throws them
27Amadeo Ascó , Adam Moore
Error Handling/** * Implements the division of the passed value by the passed divisor. * * @param value the divided element on the division. * @param divisor the element to divide by. * @return the result of dividing the value by the. * @throws ArithmeticException when the divisor is zero. */public int divide(int value, int divisor) throws ArithmeticException { if (divisor == 0) { throw new ArithmeticException("Divide by zero"); }
return (value / divisor);} // divide()
/** * Implements the division of the passed value by the passed divisor. * * @param value the divided element on the division. * @param divisor the element to divide by. * @return the result of dividing the value by the. * @throws ArithmeticException when the divisor is zero. */public int divide(int value, int divisor) throws ArithmeticException { if (divisor == 0) { throw new ArithmeticException("Divide by zero"); }
return (value / divisor);} // divide()
28Amadeo Ascó , Adam Moore
– Throwable handling code
try { … statements …
} catch (ExceptionType excp) { … error notification and recovery statements … } finally {
… last statements run … } // end try
Error Handling
29Amadeo Ascó , Adam Moore
Error Handlingint result;
try { // Dividing result = divide(value, divisor); System.out.println("Result: " + result); // result of the division} catch (ArithmeticException excp) { System.err.println(excp.getMessage()); result = -1; // assignment System.out.println("Result: " + result); // result of assignment} finally { // Resetting result = 0;} // end trySystem.out.println("Result: " + result); // result of resetting
int result;
try { // Dividing result = divide(value, divisor); System.out.println("Result: " + result); // result of the division} catch (ArithmeticException excp) { System.err.println(excp.getMessage()); result = -1; // assignment System.out.println("Result: " + result); // result of assignment} finally { // Resetting result = 0;} // end trySystem.out.println("Result: " + result); // result of resetting
30Amadeo Ascó , Adam Moore
Error Handlingint result;
try { // Dividing result = divide(value, divisor); System.out.println("Result: " + result);} catch (ArithmeticException excp) { System.err.println(excp.getMessage()); result = -1; // assignment System.out.println("Result: " + result);} finally { // Resetting result = 0;} // end trySystem.out.println("Result: " + result);
int result;
try { // Dividing result = divide(value, divisor); System.out.println("Result: " + result);} catch (ArithmeticException excp) { System.err.println(excp.getMessage()); result = -1; // assignment System.out.println("Result: " + result);} finally { // Resetting result = 0;} // end trySystem.out.println("Result: " + result);
valuedivisor
12 3
Result: 4Result: 0
valuedivisor
12 0
Divide by zeroResult: -1Result: 0
31Amadeo Ascó , Adam Moore
Error Handlingjava.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
OwnException
java.lang.ArithmeticExceptionjava.lang.NullPointerException
java.lang.IndexOutOfBoundsException
top related