15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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

Chapter 4 Computation Bjarne Stroustrup
Copyright © 2003 Pearson Education, Inc. Slide 1.
Research Methods Lecturer: Steve Maybank
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Department of Computer Science and Information Systems Autumn 2013 Preliminary.
Introduction to Programming Java Lab 7: Loops 22 February JavaLab7 lecture slides.ppt Ping Brennan
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Computer Systems
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 4: Formatted Output and Strings 1 February JavaLab4 lecture slides.ppt Ping Brennan
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
Introduction to Programming
25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
25 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
29 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
By: Bahman Ravaei Farhad Rad نیمسال
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
The IF function Bernard Liengme. Objectives To know how to: Construct a condition using the comparison operators =, >=, >, ; Construct a formula using.
Introduction to Computer Systems
CS110 Programming Language I
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
COM S 207 IF Statement Instructor: Ying Cai Department of Computer Science Iowa State University
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Copyright © 2013 by John Wiley & Sons. All rights reserved. DECISIONS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 15,
13 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Decisions Bush decision making.
22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
ITP © Ron Poet Lecture 5 1 Branching. ITP © Ron Poet Lecture 5 2 CookTime After Midnight  We want to improve our program so that we can cook meals after.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Introduction to Programming
28 Formatted Output.
Decisions.
Introduction to Programming
Introduction to Programming
Chapter Goals To implement decisions using if statements
Program Control Flow: Making Decision
Decisions Chapter 4.
Introduction to Programming
Introduction to Programming
DECISIONS.
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
SELECTIONS STATEMENTS
The Java switch Statement
Introduction to Programming
Introduction to Programming
Introduction to Programming
CS 1054 Introduction to Programming in Java
Presentation transcript:

15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2013 Week 6: if statement

Overview Java Lab 4, Exercises 2 and 3 Example of a class and an object if statement See Java for Everyone, Ch February 2013Birkbeck College, U. London2

JavaLab 4 Exercise 2 The data is included in the program rather than read in from the keyboard. int a1=28, b1=418, c1=-89, d1=-3007; Find the width in characters of the field in which the numbers are to be printed. 15 February 2013Birkbeck College, U. London3

Exercise 2: first part The format specifier is %5d, where 5 is the field width and d stands for decimal integer. Use four print statements, one for each line of the output. System.out.printf(a1:%5d\n, a1); System.out.printf(b1:%5d\n, b1); //etc. 15 February 2013Birkbeck College, U. London4

Exercise 2: second part double a2 = , b2 = -1.2; double c2 = , d2 = 587.2; Find the width of the field: 15 February 2013Birkbeck College, U. London5 d2:587.20

Format Specifier Use %9.2f, where f: fixed floating point, 9: field width, 2: number of places to the right of the decimal point. Note the rightmost 0. Define a format specifier that places adjacent to the colon. 15 February 2013Birkbeck College, U. London6 d2:587.20

Print Statements System.out.printf(a2:%9.2f\n, a2); System.out.printf(b2:%9.2f\n, b2); System.out.printf(c2:%9.2f\n, c2); System.out.printf(d2:%9.2f\n, d2); 15 February 2013Birkbeck College, U. London7

JavaLab 4, Exercise 3 String a1=Tom, b1=Jerry; /* Print true if a1 precedes b1 in lexicographic order, otherwise print false. Recall string1.compareTo(string2). This method returns an integer, but a boolean result is required. */ 15 February 2013Birkbeck College, U. London8

Exercise 3 System.out.println(a1.compareTo(b1)<=0); /* And similarly for the other pairs of words. */ 15 February 2013Birkbeck College, U. London9

Example of a Class public class BankAccount { private double balance; // data held in each object public BankAccount() // constructor to make objects { balance = 0; // each new object has balance=0 } public void payIn(double payment) // pay into the account { balance = balance+payment; } 15 February JFE Chapter 7.3

Creating an Object BankAccount ba = new BankAccount(); /* Create an object ba in the class BankAccount. The object ba has its own balance which is 0.*/ double payment = 4.23; ba.payIn(payment); /* Add £4.23 to the balance in ba. */ 15 February 2013Birkbeck College, U. London11

The if Statement int actualFloor; if (floor >13) { actualFloor = floor-1; } else } actualFloor = floor; } 15 February 2013Birkbeck College, U. London12

Alternative Code int actualFloor = floor; if (floor > 13) { actualFloor = floor-1; } 15 February 2013Birkbeck College, U. London13

Flow Chart for if Statement 15 February 2013Birkbeck College, U. London14 floor > 13? actualFloor = floor-1 actualFloor= floor truefalse

Flow Chart for if Statement with no else Branch 15 February 2013Birkbeck College, U. London15 floor > 13? actualFloor = floor-1 truefalse

Brackets Note the alignments { … } Brackets can be omitted for single statements (not recommended). if (floor > 13) actualFloor = floor-1; 15 February 2013Birkbeck College, U. London16

Avoid Code Duplication if (floor > 13) { actualFloor = floor-1; System.out.println(Actual floor: +actualFloor); } else { actualFloor = floor; System.out.println(Actual floor: +actualFloor); } 15 February 2013Birkbeck College, U. London17

Duplication Removed if (floor > 13) { actualFloor = floor-1; } else { actualFloor = floor; } System.out.println(Actual floor: +actualFloor); 15 February 2013Birkbeck College, U. London18

Multiple Alternatives 15 February 2013Birkbeck College, U. London19 The Richter Scale for Earthquakes ValueEffect 8Most structures fall 7Many buildings destroyed 6Many buildings considerably damaged. Some collapse. 4.5Damage to poorly constructed buildings

Multiple if Statements if (richter>=8.0) {System.out.println(Most structures fall);} else if (richter >= 7.0) {System.out.println(Many buildings destroyed);} else if (richter >= 6.0) {System.out.println(Considerable damage);} else if (richter >= 4.5) {System.out.println(Damage to poorly constructed buildings);} else {System.out.println(No destruction of buildings);} 15 February 2013Birkbeck College, U. London20

Result As soon as one of the tests succeeds the message is printed and no further tests are made. If no test succeeds then the final else clause applies. 15 February 2013Birkbeck College, U. London21

Discussion What happens if the order of the tests is reversed? What happens if the all the else words (and the final print statement) are removed? 15 February 2013Birkbeck College, U. London22

Squares on a Chess Board 15 February 2013Birkbeck College, U. London abcdefgh char file =`a`; int row = 3; /* square a3 */

Nested if Statements if (file == `a` || file == `c` || file == `e` || file == `g`) { if (row%2 == 1) {colour = black;} else {colour = white;} } else { if (row%2 == 0) {colour = black;} else {colour = white;} } 15 February 2013Birkbeck College, U. London24

Dangling else Problem double shippingCharge = 5.00; if (country.equals(USA)) if (state.equals(HI)) shippingCharge = 10.00; else shippingCharge=20.00; 15 February 2013Birkbeck College, U. London25

Problem Avoided by Using Brackets double shippingCharge = 5.00; if (country.equals(USA)) { if (state.equals(HI)) { shippingCharge = 10.00; // Hawaii is more expensive } else { shippingCharge = 20.00; // As are shipments outside the USA } 15 February 2013Birkbeck College, U. London26