Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
4 Control Statements: Part 1.
Chapter 5: Control Structures II (Repetition)
Basic Java Constructs and Data Types – Nuts and Bolts
Copyright © 2003 Pearson Education, Inc. Slide 1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Introduction to programming in java. Input and output to screen with Java program Structure of Java programs Statements Conditional statements.
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Lecture 1 Welcome COMP1681 / SE15 Introduction to Programming.
Chapter 7: Arrays In this chapter, you will learn about
Table 22.1 Stakeholder summary for the Odd Shoe Company
ABC Technology Project
ITP © Ron Poet Lecture 4 1 Program Development. ITP © Ron Poet Lecture 4 2 Preparation Cannot just start programming, must prepare first. Decide what.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
CS12230 Introduction to Programming Lecture 4-x – Consolidation 1.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Lecture 7: Software Design (Part II)
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
CS101: Introduction to Computer programming
Chapter 5 Test Review Sections 5-1 through 5-4.
Control Structures Selections Repetitions/iterations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Addition 1’s to 20.
CS 240 Computer Programming 1
25 seconds left…...
Chapter Three Arithmetic Expressions and Assignment Statements
Week 1.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
We will resume in: 25 Minutes.
1 PART 1 ILLUSTRATION OF DOCUMENTS  Brief introduction to the documents contained in the envelope  Detailed clarification of the documents content.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
Chapter 8 Improving the User Interface
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
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.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
CSC Programming I Lecture 6 September 4, 2002.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
The Hashemite University Computer Engineering Department
Algorithms and Pseudocode
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CSC111 Quick Revision.
Algorithms and Flowcharts
Loop Structures.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Flowcharts and Pseudo Code
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming

SE15: Loops(2)10–2 Todays Learning Objectives Meet do-while loops Learning about tracing and debugging code For you to learn more about techniques for planning the solution to a programming problem For you to recognise the importance of writing clear, readable, well-documented code

SE15: Loops(2)10–3 Lecture Outline do-while loops Top tips for loops Example of a nested loop Tracing code Debugging in Drjava Top tips for writing code

SE15: Loops(2)10–4 do-while Loops int count = 0; do { System.out.print(count +,); count++; } while (count <= 10); !warning: the loop body is always executed once!

SE15: Loops(2)10–5 Hints Watch out for extra semicolons with for loops for (i=0; i < 10; i++); Avoid declaring variables inside loops Avoid break statements if possible You cannot use do-while unless you are certain that the loop can iterate at least once. If you have computation that changes some numeric quantity by some equal amount each time, consider a for statement A while statement is always the safest

SE15: Loops(2)10–6 More torn up code Reconstruct the following fragments of code to produce the following output: public static void main (String [] args) for ( int x = 0; x < 4; x++) class MultipleFors for ( int y = 4; y > 2; y--) System.out.println(x + " " + y); if(x == 1) x++; Head First Java, Sierra & Bates, OReilly

SE15: Loops(2)10–7 What does the following code do? class LoopTest { public static void main(String [] args) { int y = 7; for(int x = 1; x < 8; x++) { y++; if(x > 4) System.out.print(++y + " "); if(y > 14) { System.out.println("x = " + x); break; }

SE15: Loops(2)10–8 UML Activity Diagrams (Flow diagrams) for(int i = 0; i < 10; i++) { System.out.println(i); } System.out.println(done); Is i < 10? false true Declare int i Set i = 0 Enter loop body Print the value of i Increment i print done Initial node Final node Action node

SE15: Loops(2)10–9 Top tips for writing programs Put the scaffolding in place, then stop, think and plan If the problem seems complex, simplify it or break it down into more manageable pieces Write down in English the steps you need to take For each of the main steps of your solution, write a comment in your main method Use the comments to remind you what to do at each point in the program

SE15: Loops(2)10–10 Simplifying the Problem Convert only from °C to °F Perform a single conversion Steps become 1. Read temperature in °C from keyboard 2. Calculate temperature in °F 3. Output result of calculation to screen

SE15: Loops(2)10–11 Commenting Your Code // Temperature conversion program // Written by Nick Efford, public class Temperature { public static void main(String[] args) { // Read temperature in Celsius // Convert to Fahrenheit // Output result of calculation } }

SE15: Loops(2)10–12 Top Tips Adopt a good coding style Add a small amount of code at a time Compile and run after each new addition of code Fix errors before adding more code! Use temporary println statements to test for correct behaviour, or run in the debugger

SE15: Loops(2)10–13 Writing Readable Programs Use a good coding style Descriptive names for classes, methods, variables… Sensible use of blank lines and indentation Consistency! Use an appropriate level of commenting Derive them from your pseudocode Comment as you go, dont add them all at the end!

SE15: Loops(2)10–14 Iteration 1 (Pseudocode) Read a temperature in Celsius from the keyboard Convert temperature from Celsius to Fahrenheit Output Fahrenheit temperature to screen

SE15: Loops(2)10–15 Iteration 1 (UML) Read temperature in Celsius from keyboard Convert temperature from Celsius to Fahrenheit Output Fahrenheit temperature to screen Initial node Final node Action node

SE15: Loops(2)10–16 Iteration 2 (Pseudocode) Read a temperature from the keyboard Read temperature scale from the keyboard If temperature scale starts with C or c: Convert temperature from Celsius to Fahrenheit Output Fahrenheit temperature to screen Otherwise if temperature scale starts with F or f: Convert temperature from Fahrenheit to Celsius Output Celsius temperature to screen Otherwise: Print an error message on the screen

SE15: Loops(2)10–17 Iteration 2 (UML) Read temperature in Celsius from keyboard Convert temperature from Celsius to Fahrenheit Output Fahrenheit temperature to screen Convert temperature from Fahrenheit to Celsius Output Celsius temperature to screen Read temperature scale from keyboard [ starts with C or c ] [ starts with F or f ] Print error message Decision node Guard condition

SE15: Loops(2)10–18 Iteration 3 (Pseudocode) Repeat: Read a temperature from the keyboard Read temperature scale from the keyboard If temperature scale starts with C or c: Convert temperature from Celsius to Fahrenheit Output Fahrenheit temperature to screen Otherwise if temperature scale starts with F or f: Convert temperature from Fahrenheit to Celsius Output Celsius temperature to screen Otherwise: Print an error message on the screen Ask user whether another calculation is required While user's response starts with Y or y

SE15: Loops(2)10–19 Iteration 3 (UML) Read temperature Read temp. scale Convert to °F and output Print error message Another conversion? Convert to °C and output [ Y or y ] [ C or c ][ F or f ] Merge node

SE15: Loops(2)10–20 Your Turn! How would you simplify Coursework 1? What would you attempt to do in your first iteration? What would the pseudocode / activity diagram look like?

SE15: Loops(2)10–21 Iteration 1: Read numbers, stopping at –1 Read a number from the keyboard While the number last read is not equal to –1: Read another number from keyboard int score = keyboard.nextInt(); while (score != -1) { // Do stuff here... score = keyboard.nextInt(); } Read number Compare with –1 Read another number [ equal ] [ not equal ]

SE15: Loops(2)10–22 Summary We have Looked at do-while loops Looked at tracing programs and debugging Seen how solutions can be expressed as pseudocode Looked at an alternative, graphical representation for solutions: the UML activity diagram Emphasised the importance of writing readable code

SE15: Loops(2)10–23 Follow-up Work Reading from Savitch Section 3.3 (using pseudocode to specify loops) Section 2.4 (commenting and coding style) Apply what you've learned today to Assignment 1 Go to the SE15 Off-Site Resources web page and visit How NOT to do a programming assignment How NOT to do a programming assignment