CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy,...

70
CPSC 111 Introduction to Computation October 15 th , 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    213
  • download

    0

Transcript of CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy,...

Page 1: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

CPSC 111Introduction to Computation

October 15th, 2009

Based on slides by Eiselt, Carter, Murphy, Pottinger

Page 2: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

The good news from the exam...45 ********44 ********43 ***************** A+42 ***********41 ********40 *******39 *38 **** A 37 **36 **35 ******34 *******33 **** B32 ****31 *

30 **29 *28 *** C27 *26 **25 ***24 D23 * 22 ***21 **20 * 19 ** F1817 **16

1514 13 **12 *1110 * 9 * 8 7 6 5 4 ** 3 2 1

Average = 79.2%

Page 3: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

And now the bad news…The second midterm exam will be much more challenging than the first exam.

There will be more material.

The material will be more complex.

We'll expect that you'll know the material we've covered so far much better than you know it now.

If your mark on the first midterm exam was 23 or lower, you should come meet with me in office hours.

Page 4: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Administrative stuffYou'll get your exams back in your labs next week.

If you think your exam should be re-evaluated, don't takeyour exam from the lab. You must return the exam to your TA before you leave. We will not re-evaluate any exam that was not returned to the TA in the lab.

Page 5: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

H1N1-related absences

If (you missed the exam because you were sick && you submitted the H1N1 report or a doctor note)

The weight of this midterm (10%) will be shifted to the second midterm (which will then have a weight of 20%)

Page 6: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Administrative Stuff

Page 7: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Administrative StuffBig reading in Big Java (2nd or 3rd edition):

Chapter 1.1 through 1.8Chapter 2.1 through 2.10Chapter 4.1, 4.7 before your next labChapter 3.1 through 3.8Chapter 4 Chapter 5 (3rd ed.) or 6 (2nd ed.)Chapter 6.1-6.5 (3rd ed.) or 7.1-7.5 (2nd ed.)

Page 8: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Review of last class

1. Conditionals in Depth

Page 9: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

To summarize

• We’d been discussing conditionals• In particular, we discussed the if-then statement:

If (condition)

{

//things done

}

else

{

//do something else

}

Page 10: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

What does a condition look like?

• Relational operators (e.g., a < b, b>=a, a != b)• Possibly combined by logical operators:

– a && b (a AND b)– a || b (a OR b)– !a

Page 11: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Today’s plan

1. Comparing Data and More on Data Conversion

2. Introduction to loops: the While statement

Page 12: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Comparing data

Is 0.3 the same thing as 1.0/10.0 + 1.0/10.0 + 1.0/10 ???

Let’s try it out...

Page 13: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Comparing data

Is 0.3 the same thing as 1.0/10.0 + 1.0/10.0 + 1.0/10 ???

No. Math with floating point numbers often gives results that arevery close to what you’d expect but not exactly the same. Thecalculation of 1.0/10.0 + 1.0/10.0 + 1.0/10 yields 0.30000000000000004

So beware. And write tests for “darn near equal” that look like this:

if (Math.abs(f1 - f2) < TOLERANCE) System.out.println (“Essentially equal.”);

where TOLERANCE is a small number appropriate to the problemlike 0.00000001

Page 14: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Comparing data

Is 0.3 the same thing as 1.0/10.0 + 1.0/10.0 + 1.0/10 ???

No. Math with floating point numbers often gives results that arevery close to what you’d expect but not exactly the same. Thecalculation of 1.0/10.0 + 1.0/10.0 + 1.0/10 yields 0.30000000000000004

The details of why this happens are beyond the scope of this course, but the anomaly basically due to the fact that 0.1 cannot be exactly represented in the standard binary floating point notation

It is approximated by the closest 23 bit binary fraction 0.000110011001100110011...

The approximation comes up in funny ways during mathematical operations, as we have seen.

Page 15: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

import java.util.Scanner;

public class ComparingFloats{ public static void main(String[] args) { double result; result = (1.0/10.0+1.0/10.0); System.out.println(result); if (0.2 == result) System.out.println(result + " is equal to 0.2"); else System.out.println("FALSE"); }}

Page 16: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Comparing data

You can compare character types:

'a' < 'b''a' == 'b''a' < 'A'

with your relational operators. Why?

Page 17: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Comparing data

You can compare character types:

'a' < 'b''a' == 'b''a' < 'A'

with your relational operators. Why?

Because each character is associated with a number by the Java coding System (Unicode).

Page 18: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Comparing data

But don’t use relational operators to compare characterstrings.

Strings are objects, so you need to call on the equalsmethod from the String class.

"abc" == "abc"

won’t blow up, it just tests to see if these two strings have the sameaddress or location in memory. They usually don’t. (remember?)

Page 19: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Back to Data ConversionThe arithmetic we do in our head isn’t the same as the maththat’s done by Java. For example, we think of 1 / 3 as0.3333333333333333333333333333333333...

But what happens when we tell Java to do the same thing?Well, it depends...

int a = 1 / 3; // a is 0

double b = 1 / 3; // b is 0.0

int c = 1.0 / 3.0; // Java’s not happy

double d = 1.0 / 3.0; // d is 0.3333333333333333

Page 20: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversionLet’s look at them one at a time:

int a = 1 / 3; // a is 0

The literals 1 and 3 are integers.

Arithmetic with integers results in an integer.

The fractional part is discarded or truncated.

So the value 0 is assigned to a.

Page 21: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversiondouble b = 1 / 3; // b is 0.0

The literals 1 and 3 are still integers. Arithmetic with integers still results in an integer.

But we declared variable b of type double -- a floating point number.

So Java takes the result of 0 that comes from the right hand side of the assignment statement and converts it to its floating point equivalent, 0.0, and assigns that converted value to b.

Page 22: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversion

int c = 1.0 / 3.0; // Java’s not happy

The operands on the right hand side are now floating pointnumbers, so the result of the division is a floating point representation of 0.3333....

But we’ve declared c to be of type int. To convert the result to an integer, Java would have to drop the .3333....

Java doesn’t want to do this unless it’s really sure you want it to happen. More on this in a moment.

Page 23: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversiondouble d = 1.0 / 3.0; // d is 0.3333333333333333

The operands on the right hand side are again floating point numbers, so the result of the division is again a floating point representation of 0.3333....

That’s compatible with the type of the variable d, which is double -- the biggest of the floating point types. Java is happy, we’re happy, except…

… what happened to the infinitely repeating series of 3’s?

A finite number of bits have been allocated to represent The value, so Java has to round off at some point.

Page 24: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversionThere are two categories of data conversions:

Widening conversions go from one data type to anothertype that uses and equal or greater amount of space tostore the value. • Widening conversions are safer because they usually don’t lose

information (though there can be some roundoff).

Narrowing conversions typically go from one type to another type that uses less space to store the value. • Important information may be lost; avoid narrowing conversions.

Page 25: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversionWhich of these is a widening conversion? Which is anarrowing conversion?

int a = 1 / 3; // a is 0

double b = 1 / 3; // b is 0.0

int c = 1.0 / 3.0; // Java’s not happy

double d = 1.0 / 3.0; // d is 0.3333333333333333

Page 26: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversionWhich of these is a widening conversion? Which is anarrowing conversion?

int a = 1 / 3;

double b = 1 / 3; // b is 0.0 - widening

int c = 1.0 / 3.0; // narrowing

double d = 1.0 / 3.0;

Page 27: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversionData conversion can happen in three different ways.

1. Assignment conversion happens when a value of one typeis assigned to a variable of another type and the value mustbe converted to the new type.

As we’ve just seen, Java will not allow a narrowing conversion to happen through assignment, but it’s happyto do widening conversions through assignment.

Page 28: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversionData conversion can happen in three different ways.

2. Promotion can happen when an expression contains mixeddata types. Consider this code snippet:

int hours_worked = 40; double pay_rate = 5.25; double total_pay = hours_worked * pay_rate;

To perform the multiplication, Java promotes the valueassigned to hours_worked to a floating point value to produce a floating point result

Page 29: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversion

What would happen in this case?

int hours_worked = 40; double pay_rate = 5.25; int total_pay = hours_worked * pay_rate;

Would the value in pay_rate be “demoted” to an integer?

Error: Bad types in assignment>

Page 30: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Data conversionData conversion can happen in three different ways.

3. You can force data conversion through casting, if the conversion is possible. A cast is a Java operator that isspecified by a type name in parentheses placed in frontof the value to be converted. For example:

double foo = 3.14159; int bar = foo; // doesn’t work int bar = (int) foo; // this works

What’s the value in bar when it’s all done? Hint: it's an integer.

Page 31: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

So where are we?

• We learned– programming language basics– about classes– about conditionals (if statements)

• That’s a lot, let’s see if we can do something useful, like…

Page 32: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

…Washing dishes (when the dishwasher is broken)

Page 33: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Exercise

• Using only constructs that we have learned so far, write a procedure (in english, not in java) to wash a huge pile of dishes by hand

Page 34: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Exercise

• Hard no?

• We need something that allows java to go around in circles

Page 35: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Today’s plan

1. Comparing Data and More on Data Conversion

2. Introduction to loops: the While statement

Page 36: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Repetition, iteration, and loopsOne of the most important attributes of a computer is its ability to perform the same task many many times. We givecomputer programs this ability through the use of iterationstatements, also called repetition statements, but more commonly just called loops.

We see repetitive operations -- loops -- in real life all the time.For example...

Page 37: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

How do I get up the stairs?

Am I at the top of the stairs?No.Climb up one step.Am I at the top of the stairs?No.Climb up one step.Am I at the top of the stairs?No.Climb up one step.Am I at the top of the stairs?No.Climb up one step.Am I at the top of the stairs?No.Climb up one step.

...and so on...

Page 38: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

The while statementThe simplest form of a loop in Java is the while statement. A while statement evaluates a Boolean expression, just like the if statement, and executes a statement (or block of statements) if the expression is true.

while ( <some Boolean expression goes here> ) { <one or more statements go here> // the body }

After the statement or statements (called the loop body) are executed, the Boolean expression is evaluated again. If the expression is still true, the body is executed again. This repetition continues until the expression is false, when processing continues with the statement after the body of the while loop.

Page 39: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

if versus while statement

booleanexpression

statement

true false

how the if statement works

Page 40: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

if versus while statement

booleanexpression

statement

true false

booleanexpression

statement

true false

how the if statement works how the while statement works

Page 41: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

if versus while statement

booleanexpression

statement

true false

booleanexpression

statement

true false

What can make this boolean expression false if it was previously true?

Page 42: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

if versus while statement

booleanexpression

statement

true false

booleanexpression

statement

true false

Oh, by the way, diagrams like these are called flowcharts.

Page 43: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

Here's the while statement

Page 44: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

Here's the boolean expression

Page 45: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

Here's the body of the while statement

Page 46: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

Here's the first statement following the while statement. Flow of control resumes here when the boolean expression is false.

Page 47: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

So what happens when we execute this?

Page 48: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit 3

Page 49: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter3 1

Page 50: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter

Is counter <= limit? yes

3 1

Page 51: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter

"The square of 1 is 1" printed on monitor

3 1

Page 52: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter3 2

Page 53: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter

Is counter <= limit? yes

3 2

Page 54: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter

"The square of 2 is 4" printed on monitor

3 2

Page 55: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter3 3

Page 56: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter

Is counter <= limit? yes

3 3

Page 57: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter

"The square of 3 is 9" printed on monitor

3 3

Page 58: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter3 4

Page 59: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter

Is counter <= limit? Nooooooo!

3 4

Page 60: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

limit counter

"End of demonstration" printed on monitor

3 4

Page 61: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

How do I get up the stairs again?

Am I at the top of the stairs?No.Climb up one step.Am I at the top of the stairs?No.Climb up one step.Am I at the top of the stairs?No.Climb up one step.Am I at the top of the stairs?No.Climb up one step.Am I at the top of the stairs?No.Climb up one step.

...and so on...

Page 62: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

How do I get up the stairs again?

while (I'm not at top of stairs) { Climb up one step. }

Going upstairs is a while loop!

Page 63: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

How will this change the program's behavior?

The body of the loop is never executed.

Page 64: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= counter) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

Using the while statement

What will this do now?

Page 65: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= counter) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); }}

The infinite loop

If the boolean expression never evaluates to false, the loop will never end. This is commonly referred to as an infinite loop -- it goes forever.

Page 66: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter - 1; } System.out.println("End of demonstration"); }}

The infinite loop

And now what happens?

Page 67: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo{ public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter - 1; } System.out.println("End of demonstration"); }}

The infinite loop

Here the termination condition is good, but the process never gets closer tothat termination condition. Another way to get the infinite loop.

Page 68: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo2{ public static void main (String[] args) { int limit = 0; int counter = 9; while (counter != limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter - 2; } System.out.println("End of demonstration"); }}

The infinite loop

This one can be a little bit more tricky.

Page 69: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class WhileDemo2{ public static void main (String[] args) { int limit = 0; int counter = 9; while (counter != limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter - 2; } System.out.println("End of demonstration"); }}

The infinite loop

Here the process gets closer to the termination condition, but never actuallysatisfies the termination condition and goes past it.

Page 70: CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

public class PrintFactorials{ public static void main (String[] args) { int limit = 10; int counter = 1; int product = 1; while (counter <= limit) { System.out.println("The factorial of " + counter + " is " + product); counter = counter + 1; product = product * counter; } System.out.println("End of demonstration"); }}

Another while statement example

This statement accumulates an ever-growing product obtained by multiplyingthe integers from 1 through the current value of counter.