









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










CS121/IS223 Week 3, Slide 1
Loops in Java
Dr Olly Gotel
http://csis.pace.edu/~ogotel
Having problems?
-- Come see me or call me in my office hours
-- Use the CSIS programming tutors (see the
schedule posted on my webpage)
CS121/IS223 Week 3, Slide 2
Basically these 3 statements
do the same thing - you can
rewrite loops using different
statements, but sometimes
one type looks neater than
the others
CS121/IS223 Week 3, Slide 3
commands
language:
commands are executed
commands are actually executed
CS121/IS223 Week 3, Slide 4
statements are executed
statements to regulate flow of control:
choose one set of statements over one or more
other sets
until a stopping condition is reached
CS121/IS223 Week 3, Slide 5
the repetition stops
One thing – many times!
CS121/IS223 Week 3, Slide 6
Forward 50 Right 90 Forward 50 Right 90
Forward 50 Right 90 Forward 50 Right 90
CS121/IS223 Week 3, Slide 7
commands:
front of a gate, false otherwise (remember
Boolean flags)
A Boolean expression that is checked
E.g. adapted from [Winder & Roberts 1998] CS121/IS223 Week 3, Slide 8
forward()
right()
forward()
right()
forward()
right()
forward()
turtle takes if he/she follows these commands
faithfully
CS121/IS223 Week 3, Slide 9
Start
forward()
forward()
forward()
forward()
right()
right() right()
Correct? Statements in Turtle
language are well defined and
I ordered them!
CS121/IS223 Week 3, Slide 10
Start
forward()
forward()
forward()
right() right()
right()
Why is it
important to be
clear which way
your turtle
faces when he
/she starts out?
forward()
Different starting conditions change outcomes of course!
CS121/IS223 Week 3, Slide 11
your turtle to get from A to B?
50 paces
20 paces
CS121/IS223 Week 3, Slide 12
forward()
forward()
forward()
forward()
forward()
right()
right()
right()
forward()
forward()
CS121/IS223 Week 3, Slide 19
& repetition
on Boolean expressions (i.e. testing whether things
are true or false)
Terry the turtle will be back…
CS121/IS223 Week 3, Slide 20
while (condition){
statement1;
statement2;
& again until the condition
becomes false
Loop condition
-boolean expression
that evaluates to
true or false
Loop body – simple
or compound
statements
between {}
It tests the condition first and
then executes the body if true
CS121/IS223 Week 3, Slide 21
int count = 0;
while (count < 10){
System.out.println(“Number = ” + count);
count++;
Loop body generally contains something
that changes the result of the controlling
boolean expression (the loop condition) –
here the value of count changes
What does this do?
Loop condition - evaluates to true or false
CS121/IS223 Week 3, Slide 22
count < 10?
count = count + 1
YES
NO
print…
CS121/IS223 Week 3, Slide 23
CS121/IS223 Week 3, Slide 24
… e.g. 10, 9, 8, 7…..
Call your file Launch.java
Think:
Initial condition
Stopping condition
Step
CS121/IS223 Week 3, Slide 25
number & then uses a while loop to count up to
that number, printing out all the whole numbers up
to the number entered starting from zero
Call your file WhileCounter.java
CS121/IS223 Week 3, Slide 26
Use Scanner for input
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
int num;
num = scan.nextInt();
If you see Keyboard anywhere,
ignore it and use Scanner instead for
input - Keyboard is Java legacy!!!
CS121/IS223 Week 3, Slide 27
do{
statement1;
statement2;
while (condition);
note how this differs from the while loop which may
not execute at all
Boolean expression
Loop body
Need the ; to end
the statement
Loop with attitude
questions later!
CS121/IS223 Week 3, Slide 28
int count = 0;
do{
System.out.println(“Number = ” + count);
count++;
}while (count < 10);
What does this do?
CS121/IS223 Week 3, Slide 29
count < 10?
count = count + 1
YES
NO
print…
Good for dealing with
user interaction
CS121/IS223 Week 3, Slide 30
CS121/IS223 Week 3, Slide 37
sum of the numbers entered is equal to or greater
than 100
numbers & prints out whether the sum is 100
exactly or greater than 100
the loop control variable to something you actually
use to change the value of inside the loop!
Call your file Hundred.java
CS121/IS223 Week 3, Slide 38
number & then uses a do-while loop to count up to
that number, printing out all the whole numbers up
to the number entered from zero
Call your file DoCounter.java
Think of a reason when you would really
benefit from using this sort of loop rather
than a pure while loop…
CS121/IS223 Week 3, Slide 39
do{
enter a number
while (number not valid);
CS121/IS223 Week 3, Slide 40
What happens if the user enters 0?
Fix this problem
Use Scanner for input
CS121/IS223 Week 3, Slide 41
executing statements (or not) based on the result
value of the condition change – else we get stuck in
an infinite loop!
CS121/IS223 Week 3, Slide 42
CS121/IS223 Week 3, Slide 43
gym???
Repetition!
Repetition!
Repetition!
Repetition!
CS121/IS223 Week 3, Slide 44
On paper
CS121/IS223 Week 3, Slide 45
number = 0
while (number < 10):
doChinUp()
number = number + 1
number = 10
while (number > 0):
doChinUp()
number = number - 1
Could count up
Could count down
This is Python code - but
you should get the gist of
what I am saying?
CS121/IS223 Week 3, Slide 46
number = 0
while (number < 50):
doStep()
number = number + 1
number = 50
while (number > 0):
doStep()
number = number - 1
Counting up…
Counting down…
This is Python code - but
you should get the gist of
what I am saying?
CS121/IS223 Week 3, Slide 47
number = 0
while (number < 15):
liftDumbbell()
number = number + 1
number = 15
while (number > 0):
liftDumbbell()
number = number - 1
Counting up…
Counting down…
Ditto - I use it
as pseudocode
CS121/IS223 Week 3, Slide 48
number = 0
while (number < 100000):
doPress()
number = number + 1
number = 100000
while (number > 0):
doPress()
number = number - 1
Counting up…
Counting down…
Ditto - I use it
as pseudocode
CS121/IS223 Week 3, Slide 55
for (int count=0; count < 10; count++){
System.out.println(count);
Now check if count is less than 10
If less than 10, execute loop body, increment the
value of count etc.
If not less than 10, exit
…and so on…
CS121/IS223 Week 3, Slide 56
for (int count=0; count < 10; count++){
System.out.println(count);
prints 0-
Loops can often be written without braces when a
single statement is in the loop body – but best practice
is to always use braces as it is clearer to read
CS121/IS223 Week 3, Slide 57
CS121/IS223 Week 3, Slide 58
for (int count = 1; count < 11; count ++){
System.out.println(count);
Important: count value is incremented after
the loop body has been executed, not before
CS121/IS223 Week 3, Slide 59
CS121/IS223 Week 3, Slide 60
count < 11?
count = count +
YES
NO
print…
count = 1
CS121/IS223 Week 3, Slide 61
out all odd numbers between 1 & 10 (inclusive)
down from 10 to zero (blast off!) & print this out
Call your file OddCounter.java
Call your file BlastOff.java
CS121/IS223 Week 3, Slide 62
CS121/IS223 Week 3, Slide 63
CS121/IS223 Week 3, Slide 64
between 0 & 12:
etc…
Call your file Tables.java
RHS: what general expression
gives us this value?
LHS: what value changes &
what value stays the same &
how can you tie the one that
changes to your loop control
variable?
CS121/IS223 Week 3, Slide 65
single line (if valid)
Call your file Characters.java
CS121/IS223 Week 3, Slide 66
integer
n! = n * (n-1) * (n-2) … * 1
5! – but you must use a loop
Call your program Factorial1.java
CS121/IS223 Week 3, Slide 73
CS121/IS223 Week 3, Slide 74
Terry’s back in town!
CS121/IS223 Week 3, Slide 75
CS121/IS223 Week 3, Slide 76
while not(atGate()):
forwardMarch()
Not Java syntax
illustration!
CS121/IS223 Week 3, Slide 77
field with 4 sides, 1 gate
& no pitfalls
Find an algorithm
CS121/IS223 Week 3, Slide 78
What’s the pseudocode?
CS121/IS223 Week 3, Slide 79
gate (in front of, left of, right of), false otherwise
can move forwards, false otherwise
Conditionals
E.g. adapted from [Winder & Roberts 1998]
New command CS121/IS223 Week 3, Slide 80
while canGoForward():
forward()
right()
while canGoForward():
forward()
right()
…etc
How many times
do we write this?
When does it stop?
CS121/IS223 Week 3, Slide 81
while not(atGate()):
while canGoForward():
forward()
right()
Does this work?
CS121/IS223 Week 3, Slide 82
while not(atGate()):
while (not(atGate()) and canGoForward()):
forward()
right()
Does this work okay?
CS121/IS223 Week 3, Slide 83
while not(atGate()):
right()
while (not(atGate()) and canGoForward()):
forward()
Does this handle
pitfalls?
CS121/IS223 Week 3, Slide 84
This is Python code - but
you should get the gist of
what I am saying?
CS121/IS223 Week 3, Slide 91
game?
you have all the control structures you need to form
a basic working program
you can research and figure that out
CS121/IS223 Week 3, Slide 92
of depth
these can get messy & unreadable – the program
flow can get VERY confusing
CS121/IS223 Week 3, Slide 93
more times
once (they just do it regardless!)
while loop & is great for counting
When in doubt, use a while CS121/IS223 Week 3, Slide 94
things that confuse you
answers to the self-review questions
parts C & D before the end of Feb)
to practice
Java project… remember... I want to see your
design in class on Feb 17
CS121/IS223 Week 3, Slide 95
to practice with all these things in class next week
structures
practice!
CS121/IS223 Week 3, Slide 96
for the first time! Be patient and be persistent!
out (you cannot just read slides and text books)
game then designing and coding your own project
will be easy… but it will take a lot of hard work
getting this all figured out. Allocate time!