School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 1 Progress By now you should have: Worked through units 1 to 5 of the learning.

Slides:



Advertisements
Similar presentations
Chapter 04 (Part III) Control Statements: Part I.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
 Control structures  Algorithm & flowchart  If statements  While statements.
Objectives In this chapter, you will learn about:
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
School of Computing Science – CMT1000 Slide 1 Ed Currie Introduction to Programming CMT1000 Lecture 1A.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
The University of Texas – Pan American
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 4: Control Structures II
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Algorithm Design.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Chapter 5: Control Structures II
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Chapter 7 Problem Solving with Loops
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Flowcharts and Pseudo Code
Chapter 4 - Control Structures: Part 1
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 1 Progress By now you should have: Worked through units 1 to 5 of the learning materials. Done all reading from Deitel and Deitel detailed in the lecture plan in the module handbook. Completed short programming exercises 1, 2 and 3. Made a start on the mini-project. If you haven’t, then you are behind schedule, and in danger of failing the module. Remember, 10 hours/week of study outside classes!

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 2 CMT1000: Introduction to Programming Ed Currie Lecture 6: Loops

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 3 Overview While loops counter-controlled repetition initialisation structured programming sentinel controlled loops

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 4 Exercise You have been told to write out –I must not sleep in class 100 times as a punishment. Write a Java program to do it for you.

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 5 Plan Set a counter to 1 While the counter is <=100 Print the message Add 1 to the counter

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 6 Program Fragment public static void sleep () { int counter; counter = 1; while (count <= 10) { System.out.println( "I must not sleep in class "); counter = counter + 1; }

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 7 Skeleton while ( test ) { instructions to be repeated }

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 8 Initialisation Loops often use some form of counter – to determine how many times the loop should be executed This counter must be initialised before the loop is entered –given a starting value int count; count = 1; A declaration and initialisation assignment can be combined int count =1;

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 9 Counter-controlled Loops Many loop programs have the same structure –the loop is executed a fixed number of times controlled by a counter counter = 1; while(counter<=number of repetitions) { instructions to be repeated; counter = counter + 1; }

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 10 Exercise Write a program to print the sum of the first n numbers, where n is input by the user.

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 11 Plan Initialise total and counter to 0 Get n from the user. While the counter is <= to n update the total print counter total so far add 1 to counter

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 12 Summary If you need to do something over and over again - use a while loop If you know the number of times the loop is to be executed, in advance, use a counter-controlled loop. Remember to initialise any counter you use.

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 13 Sentinel-controlled Loops Example Write a program which adds a series of numbers input by the user. It is not known in advance how many values will be input. All the (legal) numbers will be positive.

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 14 Plan 1. initialise total and latest number to 0 2. While the number is a legal value update the total input a new number. 3. Print out the total.

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 15 public static void addSeries () { int latestNum; int total = 0; String numStr; numStr = JOptionPane.showInputDialog( ”First number?: " ); // convert from String to int latestNum = Integer.parseInt(numStr);

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 16 while (latestNum != -1) { total = total + latestNum; numStr = JOptionPane.showInputDialog( ”Next number?: " ); latestNum = Integer.parseInt(numStr); } JOptionPane.showMessageDialog (null,"Total is " + total); }

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 17 Sentinels Counter-controlled loops are one common form of loop. Another is the sentinel-controlled loop On each repetition a value is input The repetition stops when a particular value is input –the sentinel value

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 18 Skeleton initialise valueInput; while(valueInput!=sentinelValue) { manipulate valueInput; get next value; }

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 19 Details The program should tell the user what to enter to indicate end of input. The sentinel value may be declared as a constant using keyword final

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 20 public static void addSeries () { final int sentinel = -1; int latestNum; int total = 0; String numStr; JOptionPane.showMessageDialog (null, ”Enter +ve numbers, - 1 to end: " ); numStr = JOptionPane.showInputDialog ( ”First number?: " ); // convert from String to int latestNum = Integer.parseInt(numStr);

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 21 while (latestNum != sentinel) { total = total + latestNum; numStr = JOptionPane.showInputDialog (”Next number?: " ); latestNum = Integer.parseInt(numStr); } JOptionPane.showMessageDialog (null,"Total is " + total); }

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 22 Exercise Write a program which inputs a series of student's marks and tells each student whether they have passed or failed. You may assume that each valid mark lies in the range 0-100

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 23 Control Structures assignment, input, output statements do the actual work control structures determine the order in which those statements are executed normally sequential –ie one after the other All programs can be written using just 3 control structures –sequence –selection (if.. else) –repetition (while) The variations are just for convenience

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 24 Structured Programming Structured Programming is a methodology for writing programs just using structure statements –variants of 3 control structures The opposite is spaghetti programming –allow arbitrary jumps (goto) eg l0: if (x==0) goto 12; l1: a=5 goto lo: l2: goto end; Using jumps is a bad way to program

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 25 Object-oriented development Structured programming is good for helping us to write single algorithms (methods) Object-oriented software development builds on this by providing a way of modelling solutions to problems in a natural and intuitive way. These models enable us to put algorithms and data together to construct robust, easily maintainable software systems. We’ll learn a little about this in CMT1000, and lots more in subsequent modules.

School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 26 Summary If the number of repetitions is not known in advance, –use a sentinel-controlled loop. The sentinel value is an otherwise illegal value Structured programming involves using only variations of sequence, selection, repetition for program control –i.e., single-entry, single-exit Object orientation is a means of analysing, designing and coding software systems