Transcript week7

Monday
• Chalk board lecture:
 String: the difference between equals and ==
 Random class
week7
1
APCS-A: Java
Control Structures
October 17, 2005
week7
2
Checkpoint
• “Old” Homeworks & Labs
 Menu and Calculator
 StarWars Name
 Guessing Game
• Test next Wednesday
week7
3
If Statements (Review)
if ( << conditional >> ) {
<< body >>
}
else if ( << conditional >> ) {
<< body >>
}
else {
<< body >>
}
• The << conditional >> can be any true or false conditional




week7
A simple boolean like (true)
A check for equality like (x == 5)
A greater than or equal to like (x >= 1)
A combination of the above with &&(and) , ||(or), or another conditional
(( x==5 && y == 2) || (z > 42))
4
If/Else
• Remember, the brackets are technically optional
 BUT only if you want to execute ONE statement after the if or else
statement
if (amount == 0)
System.out.println(“okay”);
else
System.out.println(“nonzero amount”);
____________________________________________
if(amount == 0)
amount = 500;
System.out.println(“amount equal to 0”);
else
amount = 200;
System.out.println(“amount was not equal to 0”);
• In this bottom example, both print statements will print, regardless of
the value of amount
week7
5
Looping
• The if/else code structure lets us change the flow
•
of the program, depending on certain conditions
Looping always us to easily repeat an action,
until a condition has been met
 What situations can you imagine in which this would
be really helpful?
• There are two kinds of loops in Java
 While they are technically interchangeable, each is
syntactically geared to a specific kind of situation
week7
6
While loop
• While loops logically follow the pattern of “while
something is true then perform the following set
of actions”
 This is useful in a situation in which you don’t know
how many times you need to do something, but you
know what the end result needs to be
• The syntax is simple:
while ( << conditional >> ) {
<< body >>
}
week7
7
Example
boolean keepLooping = true;
while (keepLooping){
printMenu();
int choice = getUserInput();
if(choice == 0){ // 0 is the “exit” choice
keepLooping = false;
}
else{
System.out.println(“Good choice”);
// do other stuff
}
}
System.out.println(“Thanks for playing”);
week7
8
For Loops
• We use for loops when we want to do a set of
•
statements a predetermined number of times
The syntax for a for loop is:
for ( <starting value>; <conditional>; <update statement>)
{
<< body >>
}
for (int x = 0; x < 10; x++) {
System.out.println(“x is: “ + x);
}
• The conditional is the same as it is in a while loop
• The update statement is optional, but usually is used to
increment or change the looping variable
week7
9
Class Exercise
• How would we write a method that would
print the numbers between 1 and 100, but
only in increments of 10?
week7
10
APCS-A: Java
Control Structures
October 21, 2005
week7
11
Today
• Everything Due
 Guessing Game, Star Wars, Menu/Calc
• 80 point quiz
week7
12
Switch Statement
int someValue;
// someValue gets a value
switch ( someValue ) {
case 0:
//do something
break;
case 1:
//do something
break;
default:
//do something
}
char someValue;
// someValue gets a character
switch ( someValue ) {
case ‘A’:
//do something
break;
case ‘B’:
//do something
break;
default:
//do something
}
• The someValue needs to be an int or a char
 If no case value is matched, then the optional default case is executed -but it’s a good idea to always have the default case even if you don’t
expect to use it
week7
13
Lab
• Practice loops - 4 tasks
week7
14