Lecture11 Slides PDF
Lecture11 Slides PDF
Lecture11 Slides PDF
Computer Science 1
Week 4 (Lecture 11)
Peter Mooney
Department of Computer
Science
Eolas Building.
Email: peter.mooney@mu.ie
●
This lecture
corresponds to
Chapter 7 in
your CS161
textbook
Recap from Lecture 10
CONDITION
false
true
Go back
LOOP to start loop
CODE again
Update LOOP
LOOP
CODE Go back
to start loop
again
CONDITION
false
Code after loop
Test your self – Which ONE of the options
below shows the output of this code?
Answer provided in lecture only
A: 8
B: 24 25 30
C: 20 22 24 26 27 28 30
IMPORTANT – notice in this example how the
loop STARTS at i = 20.
Variable i Variable
sum
Start = 24 Start = 0
24 24
25 24
26 50
27 50
28 78
29 78
The key to this loop is that our counter or 30 108
index starts at x (where x is not 0). The value
STOP STOP
of x must be <= y.
●
LIVE CODE DEMONSTRATION
Lecture 11 Example 2 – Write out all numbers
divisible by x and y between integers h and k
(while loop)
●
Initialisation: We will need some variables as
divisors – x and y – we will need our lower
and upper numbers h and k.
● Loop code: Check both numbers x and y – if
these numbers are divisible with integers
between h and k.
●
Condition: When we have iterated over every
value from h to k.
Lecture 11 Example 2 – Write out all numbers
divisible by x and y between integers h and k
(while loop)
We have a large number of The while loop has a condition that
variables to declare and initialise for will prevent currentNumber
this. becoming larger than the upper
We use currentNumber as our number k.
index or counter.
Lecture 11 Example 2 – Write out all numbers
divisible by x and y between integers h and k
(while loop)
●
Live code demonstration
Lecture 11 Example 3 – print a list of all 4 digit
numbers where the sum of the digits is less than
15 (do-while loop)
●
Initialisation: We will need a iterator or
counter variable. We will need some variables
to calculate the sum of the digits.
●
Loop code: Break up the four digit number
into the separate digits. This will take a few
steps. Then we check if the sum of these
digits < 15.
●
Condition: When we have iterated over every
four digit number available (1000, 9999).
Lecture 11 Example 3 – print a list of all 4 digit
numbers where the sum of the digits is less than
15 (do-while loop)
●
Breaking 4 digit numbers into the individual
digits – in-lecture demo – DOC CAMERA
Lecture 11 Example 3 – print a list of all 4 digit
numbers where the sum of the digits is less than
15 (do-while loop)
●
Loop code: Break up the four digit number into the
separate digits. This will take a few steps. Then we
check if the sum of these digits < 15.
6752
●
LIVE CODE
Lecture 11 Example 3 – print a list of all 4 digit
numbers where the sum of the digits is less than
15 (while loop)
In class exercise – write the code
for this problem
●
Write a Java program which prints out a list of
all positive TWO DIGIT numbers where the SUM
of the squares of the individual digits IS LESS
THAN the number itself.
● For example 92 – digits 9 and 2 – then 9*9 +
2*2 = 81+4 = 85 (85 < 92) then print 92
● For example 87 – digits 8 and 7 – then 8*8 +
7*7 = 64 + 49 = 131 (113 < 87) FALSE
Answer not shown on the lecture slides
Test your self – Which ONE of the options
below shows the output of this code?
Answer provided in lecture only
A: k = 20
B: k = 7
C: k = 6
Some advice when writing code
involving loop structures
●
1: LOOP CODE: Think about your loop code first –
very often this is the most complicated part of the
whole iterative process. Remember: This is why we
are using a loop – to repeat this code!
●
2: INITIALISATION: Think about how your loop starts
– is there a specific value you need to start with?
●
3: CONDITION(S): How are you going to stop the loop
from executing? What CONDITION do you need to
become false to stop execution of the loop?
As we have seen, most inner loop code is the
same. The difference between loop structures is
subtle, but very important
●
For loops: When you know
exactly how many times you
need to loop or iterate (even if
this is only once)
●
While loops: When you do not
know the number of times you
need to loop or iterate – the
condition is the deciding factor.
●
Do-while loops – like while
loops but you must iterate once
before you check the condition.
LECTURE 12 – To do list
WEEK 4 – In-lecture-Quiz – on for
loops, while loops, do-while loops,
and lecture 10, 11, and 12.
Week 4 – Lab 3 – OPEN ON MULE
IMPORTANT: You, your labs, and MULE
Email: firstcs@mu.ie