BIT115: Introduction to Programming

Download Report

Transcript BIT115: Introduction to Programming

Lecture 9b
Instructor: Craig Duckett
Lecture 9b/10 Topics
•
•
•
•
•
for statement (loop)
do-while loops
Nested loops
cascading-ifs
switch statements
The for statement (loop)
The for statement provides a compact way to iterate over a range of
values. Programmers often refer to it as the "for loop" because of the
way in which it repeatedly loops until a particular condition is satisfied.
The general form of the for statement can be expressed as follows:
for (initiating statement; conditional statement; next statement)
// usually incremental
{
body statement(s);
}
“ Fo r a s l o n g a s t h i s c o n d i t i o n i s t r u e
. . . d o s o m e t h i n g.”
The for statement (loop)
There are three clauses in the for statement.
•
•
•
The init-stmt statement is done before the loop is started, usually to initialize an iteration
variable (“counter”). After initialization this part of the loop is no longer touched.
The condition expression is tested before each time the loop is done. The loop isn't executed if
the Boolean expression is false (the same as the while loop).
The next-stmt statement is done after the body is executed.
It typically increments an iteration variable (“adds 1 to the counter”).
for(int count = 1; count < 11; count++)
{
System.out.println("Count is: " + count);
}
The for statement (loop)
for (initial statement; conditional; next statement // usually incremental
{
statement(s);
}
“ Fo r a s l o n g a s t h i s i s t r u e . . . d o s o m e t h i n g .”
class ForLoopDemo
{
public static void main(String[] args)
{
for(int count = 1; count < 11; count++)
{
System.out.println("Count is: " + count);
}
}
}
The output of this
program is:
Count
Count
Count
Count
Count
Count
Count
Count
Count
Count
is:
is:
is:
is:
is:
is:
is:
is:
is:
is:
1
2
3
4
5
6
7
8
9
10
initialization;
while(condition)
{
statement;
}
for(initialization; condition; increment)
{
statement;
}
The for loop is shorter, and
combining the intialization,
test, and increment in one
statement makes it easier to
read and verify that it's
doing what you expect.
The for loop is better when
you are counting something.
If you are doing something
an indefinite number of
times, the while loop may
be the better choice.
while loop
class WhileDemo
{
public static void main(String[] args)
{
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
for loop
class ForLoopDemo
{
public static void main(String[] args)
{
for(int count = 1; count < 11; count++)
{
System.out.println("Count is: " +
count);
}
}
}
while loop
class WhileDemo
{
public static void main(String[] args)
{
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
for loop
class ForLoopDemo
{
public static void main(String[] args)
{
for(int count = 1; count < 11; count++)
{
System.out.println("Count is: " +
count);
}
}
}
SEE: for_while_test.java
ICE: for loops
do-while loops
The Java programming language also provides a do-while statement, which can
be expressed as follows:
do {
statement(s)
} while (expression);
The difference between do-while and while is that do-while evaluates its expression at the bottom
of the loop instead of the top. Therefore, the statements within the do block are always executed
at least once, as shown in the following DoWhileDemo program:
class DoWhileDemo
{
public static void main(String[] args)
{
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
}
}
while loop
EVALUATES AT THE TOP
class WhileDemo
{
public static void main(String[] args)
{
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
for do-while loop EVALUATES AT THE BOTTOM
class DoWhileDemo
{
public static void main(String[] args)
{
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
}
SEE: for_while_do_while _test.java
}
Nested Loops
INSTRUCTOR NOTE: Show demos
• NestWhileTest.java
• NestForWhileTest.java
• NestedForsClock.java
And Now,
For Your Coding Pleasure …
Some Helpful Hints for Assignment 3
•
•
•
•
•
•
•
HINT #1: This assignment is using RobotSE which contains additional methods that can be used in the
code for this assignment. For example: turnRight(), turnaround(), isfacingEast(), isFacingSouth(),
isFacingWest(), isFacingNorth(), etc. See the Becker Library for reference.
HINT #2: You will need to declare and initialize five (5) instance variables!
These will be used to keep a running tally of the following:
- Total Number of Moves Made
- Total Number of Moves East
- Total Number of Moves South
- Total Number of Moves West
- Total Number of Moves North
HINT #3: You should create a method (e.g., movesCounted()) that will first keep a running tally of all
the moves mentioned above and then move(). EXAMPLE: if the robot is facing east, tally move.
HINT #4: You should create a method (e.g., printEverything()) that will print out the tally of all the
moves made at the end of the NavigateMaze() method.
HINT #5: Instead of using move() in the NavigateMaze() method, use movesCounted() which will not
only move, but tally the move to the instance variables.
HINT #6: All you need to add to the NavigateMaze() method to successfully navigate the Maze is
fourteen (14) lines of code—and this includes the squiggles! If you need to use more lines with your
logic, that’s okay too (it doesn’t only have to be 14). Also, the robot will only put down a thing if there
is a thing in the backpack to put down, otherwise will still move without putting down a thing.
HINT #7: Make sure and add backpack items to the robot object, don, down in main! Changing the 0
to at least 100 would be nice  (I will be testing your code with 100 items in backpack, and 33, and
10, and 0).
Maze.java
LECTURE 9b:
ICE Part 2:
16