21
Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 1 Lecture 12 Log into Windows/ACENET. Start MSVS and open "QuadrantProgram". Start MSVS, again, and create a new Console project "ObjectDropProgram" and rename "Program.cs" to "ObjectDrop.cs" (as well as class "Program" to "ObjectDrop". Questions?

CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 1

Lecture 12

Log into Windows/ACENET. Start MSVS and open "QuadrantProgram". Start MSVS, again, and create a new Console project "ObjectDropProgram" and rename "Program.cs" to "ObjectDrop.cs" (as well as class "Program" to "ObjectDrop".

Questions?

Page 2: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 2

Outline

Finish QuadrantProgram exercise Conditional repetition

while-loop do-while loop

Page 3: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 3

In-Class Exercise, Last Class

A line drawn from the origin resides in a quadrant that is determined by the angle, , the line makes with the positive x-axis as shown below to the right:

x

y

Quadrant 1: Between 0 and 90 degrees

Quadrant 2: Between 90 and 180 degrees

Quadrant 4: Between 270 and 360 degrees

Quadrant 3: Between 180 and 270 degrees

Page 4: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 4

In-Class Exercise, Last Class

Angles 0, 90, 180, and 270 degrees are not in any quadrant and are the positive x-axis, the positive y-axis, the negative x-axis, and the negative y-axis, respectively.

Write a program that asks the user for a positive angle in (whole) degrees for a line from the origin, then computes and displays the quadrant the line is in, or the axis it is, as appropriate.

Page 5: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 5

In-Class Exercise, Last Class

Three example runs:Enter a positive integer angle less than 360: 125125 degrees is in quadrant 2

Enter a positive integer angle less than 360: 270270 degrees is the negative y­axis

Enter a positive integer angle less than 360: 400400 is an invalid input

Page 6: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 6

In-Class Exercise, Last Class

The Main method should do the following: Ask the user for a positive integer angle and read it in

from the console. If the angle is not at least 0 and less than 360, an error

message should be displayed on the console. Hint: an if-statement with an else section can separate this part from the following parts.

If the angle is 0, 90, 180, or 270 degrees, it should display the corresponding axis on the console.

Otherwise, the ComputeQuadrant method should be called and the result displayed on the console. Hint: a switch statement comparing the angle value can be used to implement the last two parts together.

Page 7: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 7

In-Class Exercise, Last Class

The program must be implemented as follows: It must have a method ComputeQuadrant that

receives a positive integer angle in degrees and returns the quadrant number (1, 2, 3, or 4) for a line of that angle. It assumes that the angle is not 0, 90, 180, or 270. Hint: the best way to do this is to use a multi-branch if-statement.

Page 8: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 8

Repetition

So far, we have seen two types of flow of control: simple sequence and selection.

This week we will cover the third type of flow of control: repetition (or iteration).

As the word implies, repetition contructs allow a programmer to specify steps to be repeated. They are also called loops, and the steps to be repeated are called the loop body.

Page 9: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 9

Conditional Repetition

There are two types of repetition constructs: conditional repetition and counted repetition.

Conditional repetition is used when the programmer does not know how many times the steps are to be repeated.

Instead, the programmer specifies a loop condition that must be true in order for the steps to be repeated. As with if-statement conditions, a loop condition is simply a boolean expression.

Page 10: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 10

Conditional Repetition

The loop condition may be tested either before the loop body begins (a pretest loop) or after the loop body ends (a posttest loop). Due to the syntax used to implement these types of loops, they are often called while loops and do-while loops, respectively.

For both loop types, when the loop condition becomes false, control flows to the first statement following the loop.

Page 11: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 11

While Loop

The syntax for the while loop statement is:while (<loop condition>){    // loop body; steps to repeat}

The loop condition is tested, and when it is true, the loop body is executed, then flow of control goes back to the beginning of the statement.

Page 12: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 12

While Loop

In addition to the loop condition and the loop body, most loops also must have one or more initialization statements to set up the

loop condition one or more update statements in the loop body

that will cause the loop condition to become false – without this, there is a potential for an infinite loop.

Page 13: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 13

While Loop Example

A physics professor wants you to write a program that displays the effect of Earth's gravity on a free falling object by computing the height of an object (e.g., a ball) as it falls after being dropped (not thrown) from the top of a building.

Page 14: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 14

While Loop Example

The output of this program should look like:Enter the building height (in meters): 100Enter the time between table lines (in seconds): 0.5

      Time    Height

      0.00    100.00      0.50     98.78      1.00     95.10      1.50     88.98      2.00     80.40      2.50     69.38      3.00     55.90      3.50     39.89      4.00     21.60      4.50      0.77             SPLAT!!

Page 15: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 15

While Loop Example

That is, the professor would like to be able to input the building height and the time interval between the lines of a table that shows the height of the object for every time period that it is falling.

The formula for computing the distance traveled by a dropped object is:

where g is Earth's gravity constant (9.8 m/s2) and t is the time the object has been falling.

distance=12

g t 2

Page 16: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 16

While Loop Example

Some observations At time t = 0, the height of the object is the height of

the building. While the object is falling, the height of the object is

the building height minus the distance it has fallen. Free fall has ended when height of the object is less

than or equal to 0.0.

Page 17: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 17

While Loop Example

Identify the data needed by this problem: buildingHeight, objectHeight, t (time elapsed), deltaT (time between table lines)

Identify program steps:

1. Ask the user for input values (buildingHeight and deltaT) and read them from the console. Convert them to numbers

2. Initialize the loop variables, objectHeight and t, and display a table heading

(continued on next slide)

Page 18: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 18

While Loop Example

3. While the objectHeight is greater than 0.0 do

3.1. Display t and objectHeight in a table row

3.2. Compute the next time t and the next objectHeight

4. Display "SPLAT!"

Write the code to implement this program in the ObjectDropProgram project.

Page 19: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 19

Do-While Loop

So far, when we run a program, it does its computation only once. It would be nice if we could have the program repeat itself, when we want it to. For example, the Quadrant program.

One way to do this is to ask the user at the end of the program whether they want to do the computation again, and if the answer is "yes", loop back to the beginning of the program.

Page 20: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 20

Do-While Loop

Since we want to run the program at least once, the loop test happens after the loop body is executed. I.e., it is a posttest loop.

A do-while loop is used to implement this. It has syntax:do   // note: *no* semicolon here{    // loop body; steps to repeat}while (<loop condition>);  // semicolon *here*

Page 21: CS 205 Lecture 12 - University of Evansvilleuenics.evansville.edu/~hwang/s11-courses/cs205/lecture12-while-loo… · Monday, February 7 CS 205 Programming for the Sciences - Lecture

Monday, February 7 CS 205 Programming for the Sciences - Lecture 12 21

Do-While Loop Example

As with the while loop, there must be one or more statements in the do-while loop body that will cause the loop condition to become false.

We can have the Quadrant program repeat for as long as we want by adding the do-while loop in the Main method like so:

1. Do1.1. Prompt user for and read in angle1.2. Determine the correct axis or quadrant 1.3. Ask the user if they want to do another computation and read in the response

2. While userInput is "Y" or "y"