Lecture10 Slides PDF
Lecture10 Slides PDF
Computer Science 1
Week 4 (Lecture 10)
Peter Mooney
Department of Computer
Science
Eolas Building.
Email: [email protected]
●
This lecture
corresponds to
Chapter 7 in
your CS161
textbook
Ternary Operator Revisited
int a = -100, b = 20;
//Get the maximum value
int min = (a < b) ? a : b;
System.out.println("Minimum value is: " + min);
thanks!
CS161: The NEXT MAJOR CONCEPT
●
We’ve seen class structure, variables, data types,
conditions, IF statements, ternary operators, ...
●
Now we move onto LOOPS or ITERATION within
Java programs.
●
LOOPS or ITERATION are one of the key ways that
computer programming outperforms human
computational ability
●
LOOPS or ITERATION allows a computer program
to repeat the same calculation or computation
over and over again (and to do so very quickly!)
Let’s introduce iteration (loops)
for
do…while
Syntax of while loops in Java
Initialisation; // control variables, index, counters etc.
while(condition is true)
{
statement1;
statement2;
….
update;
}
As long as the condition being tested is true,
whatever is between the braces will execute again and
again, possibly forever!
Syntax of do-while loops
Initialisation; // control variables, index, counters etc.
do
{
statement1;
statement2;
….
update;
}
While (condition is true)
As long as the condition being tested is true, whatever is
between the braces will execute again and again, possibly
forever! But statement1, statement2, etc will be excuted at
least once
Syntax of for loops
for(initial; condition is true; ++/--){
statement1;
statement2;
………
}
The update in a for loop is uusually an ++ or - - operator.
All statements inside the { } are executed a known number of
times until the condition becomes false.
Comparing the three loops and
their syntax and structure
Examples of loop structures
●
We are going to look at several examples of
loop structures
●
For the first few examples, we will create the
code for the three types of loops.
●
Then, for other examples, we will just create
the code for one of the three types of loops.
●
We’ll also do some live coding of an example
(in lecture 10 or lecture 11)
Example 1: Printing numbers
Our condition –
once this
becomes FALSE
we are finished
the loop
Increment –
move the value
of i on by 1 each
time.
Our condition –
once this
becomes FALSE
we are finished
the loop
Increment –
move the value
of i on by 1 each
time.
Our condition –
once this
becomes FALSE
we are finished
the loop
Increment –
move the value
of i on by 1 each
time.
When i = 0, execute the code on line 12
– then update i on line 18. If condition on
line 19 is TRUE – go back and do the
loop again.
When i = 1, execute the code on line 12
The do-while loop curly – then update i on line 18. If condition on
Backets (block) line 19 is TRUE – go back and do the
loop again.
●
The loop will iterate in steps of 1
each time (update)
●
We need to have a variable to store
or remember the sum of numbers
divisible by 7. This is like a
“running total”.
●
Each time we find a number
divisible by 7 we should add it to
the current SUM or TOTAL
●
The loop should stop or terminate
when our step or index is greater
than N.
Example 2: Adding numbers
(for loop code) as our counter
Initial value of i –
Our condition –
once this
becomes FALSE
we are finished
the loop
Increment – move
the value of i on by 1
each time.
When i = 0, line 11 is FALSE .. Then i++
Our condition –
once this
becomes FALSE
we are finished
the loop
Increment – move
the value of i on by 1
each time.
.......
When i = 1001 check condition on line 9
– FALSE – then STOP we skip past the
Example 2: Printing numbers
(do-while loop code)
A: 13 B: 54 C: 4
Answer provided live in the
lecture
So, remember, here’s how you choose
the correct loop structure
ALL Loop structures require
several key parts in their design
●
This is true for loop structures in any programming
language, not just Java
Structure Description
INITIALISATION: Initial conditions This is how our loop starts. What are the values of
or the starting conditions the variables at the very start of the repetition?
Usually, this involves a counter or index.
CONDITION: The condition that Very similiar to conditions in IF statements – we
stops or terminates the loop need a condition (evaluating to true or false) that
structure controls the loop. If the condition becomes false
then the loop stops
ITERATION: Iteration – what is the This is the code inside the body or the loop block
code that is being iterated or (essentially inside the { } curly braces). It is the
repeated inside the loop? code that you want repeated or looped
UPDATE: How is the loop To continually repeat the same code (as outlined in
structure updated? the iteration step) we have to update the variables
that are part of the CONDITION.
The for loop, while loop, and do-
while loop have many similarities
but they are not identical
Structure The for loop The while loop The do-while loop
INITIALISATION: Usually created within the Usually happens BEFORE the Usually happens BEFORE the do-while
Initial conditions for loop syntax – by while loop is written. loop is written.
declaring and initialising a
or the starting
variable
conditions
CONDITION: The Generally, a simple Usually, the condition is a Usually, the condition is a boolean
condition that condition involving the boolean condition which can condition which can involve
initialisation variable. involve initialisation variables initialisation variables
stops or
terminates the
loop structure
ITERATION: This can be any valid Java This can be any valid Java This can be any valid Java code. The
Iteration – what is code. The initialisation code. The initialisation variable initialisation variable is normally
variable is normally NOT is normally changed with in changed with in this code.
the code that is
altered in this code. this code.
being iterated or
repeated inside
the loop?
UPDATE: How is By ++ or – of the By changing the initialisation By changing the initialisation variable
the loop structure initialisation variable variable
updated?
Your choice of loop depends on
the computational task involved
Usage The for loop The while loop The do-while loop
When should I When you want to execute a When you do not know how The do-while loop is used when you
set of statements (iteration) many times you want to have a situation similiar to a while
use this loop? a specific number of times. execute a set of statements loop.
(iteration).
That is, you know exactly You do not know how
how many times you want You may want to execute 0
the loop to repeat or iterate. times, 1 times, or 10,000 times! many times you want to
execute a set of
The while loop iteration is
controlled by the condition statements (iterations)
but you know that they
must be executed at
least once.
LECTURE 12 – To do list
Bring your laptop or phone and/or
pen/paper – We will have our WEEK 4
IN LECTURE QUIZ
Week 4 – Lab 3 – OPEN ON MULE
All questions about the Labs
– please ask Mark Noone
(Lab manager and Teaching
assistant)
Email: [email protected]