Week 4 Power Point Slides

Download Report

Transcript Week 4 Power Point Slides

Switch Statement
A way to eliminate confusing if/else statements
switch (month)
Note: You can only switch on Integer type
{ case 9:
variables: char, byte, short, int, and long
case 4:
case 6:
case 11: days = 30;
break;
case 2: days = 28;
if (year % 4 == 0) days = 29;
break;
default: days = 31;
}
System.out.println
( “Month” + month + “ has” + days + “ days”);
Iterations (Three kinds)
Again
?
Body
Initialize
Again
?
Body
Again?
Body
Pre-test
Post-test
The Java syntax for each kind
Is somewhat different
Adjust
Counter
Counter controlled
Pre Test Loop
while (<condition>) { <block of instructions> }
Sum numbers until a user enters a negative
int x = 0, sum = 0;
while (x >= 0)
{
x = IO.readInt
(“Enter an integer to sum”);
if (x >=0) sum +=x;
}
System.out.println(“The sum is ” + sum);
Note: The body of the loop is a compound statement (enclosed by { and })
Post-test loop (another way)
do { <block of instructions> } while (<condition>);
Sum numbers until a user enters a negative
int x = 0, sum = 0;
do
{
sum += x;
x = IO.readInt
(“Enter an integer to sum”);
} while (x >= 0);
System.out.println(“The sum is ” + sum);
Sum number another way
int x = 0, sum = 0;
while (true)
{
x = IO.readInt
(“Enter an integer to sum”);
if (x <0) break;
sum +=x;
}
System.out.println(“The sum is ” + sum);
Note: The break statement jumps out of the loop
Counter controlled loop
for (<initialize>; <condition>; <increment>) { <block of instructions> }
Sum numbers from one to one-hundred
int sum;
for (int i=1; i<=100; i++)
{
sum += i;
}
System.out.println
(“The sum is ” + sum);
i=1
i <= 100
sum+=i
i++
Question: How to sum numbers from -100 to +100?
Question: How to sum just the even numbers?
Pre-test loop: sum 1 through 100
• Sum 1 through 100
int sum = 0, i = 1;
while (i<=100) { sum += i++; }
Question: What happens if the ++ was left off?
• Sum even numbers 2 through 100 (one way)
int sum = 0, i = 1;
while (i<=100) { if (++i%2!=0) continue;
sum += i; }
• Sum even numbers 2 through 100 (another way)
int sum = 0; i = 2;
while (i<=100) { sum += i; i += 2; }
Note: The continue statement iterates to the next value
Post-test loop to sum numbers
• Sum 1 through 100
int sum = 0, i = 1;
do { sum += i++; } while (i<=100);
Question: What happens if the ++ was left off?
• Sum even numbers 2 through 100 (one way)
int sum = 0, i = 1;
do { if (++i%2!=0) continue;
sum += i; } while (i<=100);
• Sum even numbers 2 through 100 (another way)
int sum = 0; i = 2;
do { sum += i; i += 2; } while (i<=100);
A more difficult problem
Sum all the non-primes up to 1000
int sum = 0;
for (int num=1; num < 1000); num++)
{
for (int i=2; i<num; i++)
{
if (num%i == 0) { sum += num;
break;
}
}
}
Note: A prime is a number that is divisible by only itself and one
Primitive verses Object variables
• Primitive variable types: long, int, short, byte, float, double, boolean
• Object variable types: String and others that we will be creating.
• How do we compare to primitive variables for equality?
– Answer: We use ==
– Example: if (x == y)
• How do we compare object variables for equality?
– Answer: We use the object’s .equals method
– Example: if (str.equals(“quit”));
• Question: Why the difference?
Answer: Because == will compare where in memory the object is and
not its contents.
Note: There is more to this, but for now, this explanation will suffice
Structure an input loop
• Loop until the user enters a negative
while ((value = IO.readInt("Enter value: "))>=0)
{
// Do something with the value variable
}
• Loop until the user types quit
do
{
}
// Do some kind of processing
more = IO.readString("Please Enter a String");
while (more!=null && !more.equals("quit"));
Review
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
When would you use a pre-test, post-test, or counter controlled loop?
What is an infinite loop? Give an example of when it can happen.
What is the difference between continue and break?
Which loop allows you to initialize a variable as part of its initialization?
What is the loop condition for? What is the loop initialization for?
When would you use a switch statement?
What is a limitation of using a switch statement?
What is a nested loop, condition, or a switch?
Why is a loop a good thing to apply to the lab projects we did so far?
What is a prime number?
Which loop requires a semicolon as part of its syntax?
When are the braces needed in connection with the loop body?
Define the terms: iteration and body