110 likes | 202 Vues
Iterative Structures (Loops). CS 1401 Spring 2013 Shirley Moore, Instructor February 28, 2013. Agenda. Announcements (5 min) CS Seminar today, 1:30-2:30, Cybershare Viz Lab, Dr. Na Li (faculty candidate), Topic: Privacy Awareness in Social Networks
E N D
Iterative Structures (Loops) CS 1401 Spring 2013 Shirley Moore, Instructor February 28, 2013
Agenda • Announcements (5 min) • CS Seminar today, 1:30-2:30, CybershareViz Lab, Dr. Na Li (faculty candidate), Topic: Privacy Awareness in Social Networks • Changes in Unit 2 Plan schedule and assignments • Lab 4 extra credit • Research mini-projects • Retest IPO Algorithm/Programming (20 min) • Collect homework • Hand back quiz 3 • Video clips and discussion on software reliability (15 min) • Homework problems (10 min) • While loops and tracing (25 min) (continue as homework) • Wrapup (5 min)
Software Reliability • Video clips • The Machine That Changed the World, Part 5 42:00-52:00 • Tacoma Bridge disaster • http://science.howstuffworks.com/engineering/civil/bridge10.htm • Why is testing generally insufficient to ensure that a complex program is 100% correct? • What other strategies can we use to achieve software reliability? (research mini-project)
Programming Exercise 3.19 • (Compute the perimeter of a triangle) Write a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid if the sum of every pair of two edges is greater than the remaining edge.
Programming Exercise 3.20 • (Science: wind-chill temperature) The formula for wind-chill temperature on p. 78 for problem 2.17 is valid when the temperature is in the range -58 to 41 Fahrenheit and the wind speed is greater than or equal to 2 mph. Write a program that inputs the temperature and wind speed and either outputs the wind-chill temperature if the inputs are in range or outputs a message indicating which input(s) is(are) invalid if one or both inputs are out of range.
Programming Exercise 3.22 • (Geometry: point in a circle?) Write a program that prompts the user to enter a point (x,y) and checks whether the point is within the circle centered at (0,0) with radius 10. (Hint: A point is in the circle if the distance from the point to (0,0) is less than or equal to 10).
Programming Exercise 3.29 • (Geometry: two circles) Write a program that inputs the center coordinates and radii of two circles and determines whether one of the circles is inside the other, the circles intersect, or the circles do not overlap.
Iteration • Repeating a statement or set of statements • Need stopping criterion – why? • Types of iteration • We know ahead of time how many times we want to repeat the statements (although this could be in a variable) • We want to do the statement as long as some condition is true (or false) • Can test the condition before or after executing the statements • Iterative structures are called “loops”.
While Loop Syntax and Semantics • Syntax while(boolean-expression) { statements } • Semantics: When the computer comes to a while statement, it evaluates the boolean-expression, which yields either true or false as its value. If the value is false, the computer skips over the rest of the while loop and proceeds to the next part of the program. If the value of the expression is true, the computer executes the statement or block of statements inside the loop. Then it returns to the beginning of the while loop and repeats the process. That is, it re-evaluates the boolean-expression, ends the loop if the value is false, and continues it if the value is true. This will continue over and over until the value of the expression is false; if that never happens, then there will be an infinite loop.
While Loop Example int number; // The number to be printed. number = 1; // Start with 1. while ( number < 6 ) { // Keep going as long as number < 6 System.out.println(number); number = number + 1; // Go on to the next number. } System.out.println("Done!"); • Trace the above code
Assignments for Tuesday, March 5 • Finish programming exercises 3.1, 3.22, 3.29 (algorithm + program) to turn in • Read textbook 4.1-4.2 • Write down any questions you have • Do CheckPoint questions • Do while loop and tracing exercises 1-12 • Study for Quiz 4