Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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!

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

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

4 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.

5 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

6 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; }

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

8 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;

9 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; }

10 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.

11 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

12 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.

13 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.

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

15 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);

16 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); }

17 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

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

19 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

20 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);

21 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); }

22 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

23 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

24 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

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

26 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


Download ppt "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."

Similar presentations


Ads by Google