Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture11 Slides PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

CS161 Introduction to

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

 There are 3 different types of loops in Java


 while loops
 for loops
 do while loops

 There are three important parts to a loop:


 The initial value (control variable etc)
 The condition (loop code)
 Update (moving the loop on)
Recap: The flowchart for any loop
for or while loop in Java

CONDITION
false
true
Go back
LOOP to start loop
CODE again

Update LOOP

Code after loop


Recap: The flowchart for any loop
do-while loop in Java

LOOP
CODE Go back
to start loop
again

Update LOOP true

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.

A loop can be initialised to start at ANY valid


value. We don’t have to start at zero
Further examples of loops
Lecture 11 – The loops lecture

There is no need to show a ●
Lecture 11 Example 1 – find
for loop, while loop, and the sum of all even
numbers between integers x
do-while loop for every and y (for loop)
example.

Lecture 11 Example 2 –

In the next examples – we Write out the list of numbers
will use one loop for each divisible by x and y between
integers h and k (do-while
example
loop)

As a study exercise (for ●
Lecture 11 Example 3 – print
your own study) you can a list of all positive 4 digit
try to write the other two numbers where the sum of
the digits is less than 15
loops in each example.
(while loop)
Lecture 11 Example 1 – find the sum of all even
numbers between integers x and y (for loop)

● Initialisation: We will need some variables – x


and y – we will need a counter variable also.
We will need a variable to hold the sum also
● Loop code: Check each number between x and
y – if the number is even then it gets added to
the value in sum.

Condition: When we have iterated over every
value from x to y.
Lecture 11 Example 1 – find the sum of all even
numbers between integers x and y (for loop)

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.

However, we still move i forward one at a


time – using i++.
Lecture 11 Example 1 – find the sum of all even
numbers between integers x and y (for loop)


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

6752 % 10 – gives us the right most digit


(2).

6752 / 10 – gives us the left most digit (6)

We need to access the 75

6752 % 1000 gives 752


So then 752 / 100 gives 7

752 % 100 gives 52


Then 52 / 10 gives 5
Lecture 11 Example 3 – print a list of all 4 digit
numbers where the sum of the digits is less than
15 (while loop)
We need to test every 4
We can digit number from 1000
declare to 9999 inclusive.
variables
INSIDE the
LOOP CODE The LOOP CODE is
block the difficult part
here. Our condition
(line 26) is relatively
simple. We also
move the loop
onwards in the usual
way


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

Q1 to Q5 can be done, at any time


between Monday 12 noon and Friday
5pm. Using MULE on any device, in
Maynooth, outside Maynooth, in
Ireland, outside Ireland etc etc. No
restrictions

Q6, Q7 are the hidden questions. They


are VISIBLE when YOUR LAB STARTS
each week
5. Hidden 1
These MUST be done in the LAB –
using a DESKTOP COMPUTER in the
6. Hidden 2
LAB.

MUST be submitted before Friday 5pm


All questions about the Labs
– please ask Mark Noone
(Lab manager and Teaching
assistant)

Email: firstcs@mu.ie

You might also like