1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 6 Decision Making, Control Structures & Error Handling.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Written by: Dr. JJ Shepherd
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
JAVA PROGRAMMING PART II.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
Written by: Dr. JJ Shepherd
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Object Oriented Programming Lecture 2: BallWorld.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Information and Computer Sciences University of Hawaii, Manoa
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
GC211 Data Structure Lecture 1 Sara Alhajjam.
Chapter 4 Assignment Statement
Chapter 4: Making Decisions.
Lecture 2: Data Types, Variables, Operators, and Expressions
Chapter 3 Assignment Statement
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Introduction to Programming in Java
5 Variables, Data Types.
null, true, and false are also reserved.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
An overview of Java, Data types and variables
Recap Week 2 and 3.
Java Programming Review 1
Introduction to Programming
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 6 Decision Making, Control Structures & Error Handling

2 Amadeo Ascó, Adam Moore Previously Variables: hold information Primitive Data Types – boolean  true and false – byte  1byte, -128 to 127 – char  2bytes, from 0 to (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

3 Amadeo Ascó, Adam Moore Overview Decision Making Control Structures Error Handling

4 Amadeo Ascó, Adam Moore Decision Making The Traffic light an example

5 Amadeo Ascó, Adam Moore if red or amber then stop else if stopped then if red or amber then stop else if stopped then if red or amber then stop if red or amber then stop if red or amber then stop else if stopped then start else if red or amber then stop else if stopped then start else if red or amber then stop else if red or amber then stop else if red or amber then stop else if stopped then start else continue if red or amber then stop else if stopped then start else continue if red or amber then stop else if stopped then start else continue if red or amber then stop else if stopped then start else 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

6 Amadeo 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 then stop else if stopped then start else continue if red or amber then stop else if stopped then start else continue if (bRed || bAmber) { … } else { if (bStopped) { … } else { … } if (bRed || bAmber) { … } else { if (bStopped) { … } else { … }

7 Amadeo Ascó, Adam Moore Decision Making int iCounter = 0; // initialise counter... if (iCounter == 100) { // reset counter iCounter = 0; }...

8 Amadeo 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 }

9 Amadeo 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

10 Amadeo 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)

11 Amadeo Ascó, Adam Moore switch Statement – A way to simulate multiple if statements int iCounter = 0; // initialisation int 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; // initialisation int iValue;... switch (iCounter) { case 0: iValue = -1; break; case 1: iValue = -2; break; case 2: iValue = -3; break; default: iValue = -4; } // end switch

12 Amadeo Ascó, Adam Moore – Format switch (expression) { case constant1: statements1 case constant2: statements1 … default: statements } – The expression must be a char, byte, short, int or enum Decision Making int iCounter = 0; // initialisation String 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; // initialisation String strMsg;... switch (iCounter) { case 0: strMsg = "1st "; break; case 1: strMsg = "2nd"; break; case 2: strMsg = "3rd"; break; default: strMsg = "4th"; } // end switch

13 Amadeo Ascó, Adam Moore Control Structures Loops – while – do – for Loop Control – break – continue

14 Amadeo 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

15 Amadeo Ascó, Adam Moore Control Structures int iIndex = 0; // initialise while (iIndex < 4) { System.out.println(iIndex); iIndex += 2; // add two } // end while System.out.println(iIndex);... int iIndex = 0; // initialise while (iIndex < 4) { System.out.println(iIndex); iIndex += 2; // add two } // end while System.out.println(iIndex);... index 024

16 Amadeo 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

17 Amadeo Ascó, Adam Moore int iIndex = 0; // initialise do { System.out.println(iIndex); iIndex += 2; // add two } while (index < 4); System.out.println(iIndex); Control Structures index 024

18 Amadeo Ascó, Adam Moore int iIndex = 4; // initialise do { System.out.println(iIndex); iIndex += 2; // add two } while (index < 4); System.out.println(iIndex); Control Structures int iIndex = 4; // initialise while (iIndex < 4) { System.out.println( iIndex ); iIndex += 2; // add two } // end while System.out.println(iIndex); 4 6 4

19 Amadeo 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 while System.out.println(iIndex);...

20 Amadeo 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

21 Amadeo Ascó, Adam Moore for (int iIndex = 0; iIndex < 4; iIndex += 2) { System.out.println(iIndex); } // end for System.out.println(iIndex); Control Structures index 024

22 Amadeo Ascó, Adam Moore for (int iIndex = 0; iIndex < 4; iIndex += 2) { System.out.println( iIndex ); } // end for System.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 while System.out.println(iIndex);... InitialisationConditionIncrement Initialisation - Condition - Increment

23 Amadeo 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);...

24 Amadeo Ascó, Adam Moore 0 int iCounter = 0; // initialise do { if (iCounter == 1) { iCounter = 4; continue; } else if (iCounter == 5) { break; } ++iCounter; } while (iCounter < 10); Control Structures counter

25 Amadeo 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

26 Amadeo 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

27 Amadeo Ascó, Adam Moore Error Handling /** * Implements the division of the passed value by the passed divisor. * value the divided element on the division. divisor the element to divide by. the result of dividing the value by the. 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. * value the divided element on the division. divisor the element to divide by. the result of dividing the value by the. 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()

28 Amadeo Ascó, Adam Moore – Throwable handling code try { … statements … } catch (ExceptionType excp) { … error notification and recovery statements … } finally { … last statements run … } // end try Error Handling

29 Amadeo Ascó, Adam Moore Error Handling 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 try System.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 try System.out.println("Result: " + result); // result of resetting

30 Amadeo Ascó, Adam Moore Error Handling 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 try System.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 try System.out.println("Result: " + result); value divisor 12 3 Result: 4 Result: 0 value divisor 12 0 Divide by zero Result: -1 Result: 0

31 Amadeo Ascó, Adam Moore Error Handling java.lang.Throwable java.lang.Exception java.lang.RuntimeException OwnException java.lang.ArithmeticException java.lang.NullPointerException java.lang.IndexOutOfBoundsException