Download Java Programming: Understanding Control Structures and Loops - Prof. Orlena C. Gotel and more Exams Computer Science in PDF only on Docsity!
CS121: Computer Programming I
Loops in Java
Dr Olly Gotel
[email protected]
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)
YOU MUST TRY THE EXAMPLES
AND EXERCISES
This week’s Agenda
- More control structures in Java
- Loops:
- while statements
- do-while statements
- for statements
- Infinite loops
- Nested loops
Basically these 3 statements
do the same thing - you can
rewrite loops using different
statements, but sometimes
one type looks neater than
the others
Control Structures - Recap
- Flow of control is the order in which program
statements are executed
- Most programming languages have 2 kinds of
statements to regulate flow of control:
- conditionals – branching/selection statements that
choose one set of statements over one or more
other sets
- loops – repeat a set of statements again & again
until a stopping condition is reached
- They often rely on boolean expressions
Loops
- A group of repeated statements
- Loop body - group of statements to be repeated
- Each repetition of a loop is called an iteration
- Loop condition – mechanism for deciding when & how
the repetition stops
One thing – many times!
My Variant of Turtle Commands
- Assume your turtle can carry out the following
commands:
- forward() – move 10 paces forward
- right() – turn 90 degrees to the right
- Assume you can also ask your turtle a question:
- atGate() – your turtle answers true if he/she is in
front of a gate, false otherwise (remember
Boolean flags)
A Boolean expression that is checked
E.g. adapted from [Winder & Roberts 1998]
A Simple Program
forward()
right()
forward()
right()
forward()
right()
forward()
- Pick a starting point & then sketch out the path your
turtle takes if he/she follows these commands
faithfully
Same Program, Different Starting State
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!
Getting from A to B
- What sequence of commands do you need to give
your turtle to get from A to B?
50 paces
20 paces
B
What Ifs?
- What if:
- your turtle is facing another direction (e.g. West &
not East)?
- your turtle has a different starting position?
- the gate suddenly moves!
- What if you want your program to get your turtle to
the gate no matter where he/she starts, no matter
what direction he/she faces, no matter where the
door?
Remember – programming is about problem solving!
Your program sucks! It is hard-wired to solve
ONE particular problem! What do you do?
A Simple Sub-Problem
- Get the turtle to the gate
- Assumptions:
- turtle is always directly in front of & facing the gate
- no pitfalls between the turtle & the gate
- Naïve solution:
- forward() … a suitable number of times
- need to change your program every time this
distance changes
What would we really like our program to do?
Changing the code
EVERYTIME the
context changes is
not cool!
Your First Flavour of Loop
- While turtle is not at the gate , move forward:
while (!atGate()){
forward();
Loop condition
(test) –
remember –
this yields a
boolean value
loop body
want
executed if
true
Kellogg's® Honey Nut Loops®
We get our turtle to the
gate, however far away!
The while Loop
while (!atGate()){
forward();
}
- Asks the turtle whether he/she is at the gate –
remember atGate() returns true if the turtle is at the
gate & returns false if he/she is not at the gate
- Then says, while condition NOT atGate() is true, move
forwards:
- if atGate() is true, then not(atGate()) is false – the
loop body is not executed & the turtle does not
move
- if atGate() is false, then not(atGate()) is true - the
loop body is executed. This means that the turtle
executes a forward() command & the condition
not(atGate()) is re-tested
More About while
- The while statement allows for sequencing, iteration
& repetition
- Like the if statement, the while statement is based
on Boolean expressions (i.e. testing whether things
are true or false)
Terry the turtle will be back…
while Loops in Java - the SYNTAX
while (condition){
statement1;
statement2;
& again until the condition
becomes false
- The loop body will be executed 0 or more times
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