Iterative Structures (Loops) CS 1401 Spring 2013 Shirley Moore, Instructor February 28, 2013.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

While loops.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Introduction to Computing Science and Programming I
CS0004: Introduction to Programming Repetition – Do Loops.
 Control structures  Algorithm & flowchart  If statements  While statements.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
ITC 240: Web Application Programming
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Selection and Testing Shirley Moore CS 1401 Spring 2013 February 21 and 26,
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
CONTROLLING PROGRAM FLOW
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Computer Programming 12 Mr. Jean March 5 th, 2014.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
while Repetition Structure
Control Structures II Chapter 3
Chapter 5: Loops and Files.
Logical Operators and While Loops
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Control Structure Senior Lecturer
Intro to Nested Looping
3 Control Statements:.
Repetition Control Structure
Selection Statements.
Intro to Nested Looping
Logical Operators and While Loops
Programming Concepts and Database
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Announcements Lab 3 was due today Assignment 2 due next Wednesday
REPETITION Why Repetition?
Presentation transcript:

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 – 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 0.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 – Write down any questions you have – Do CheckPoint questions Do while loop and tracing exercises 1-12 Study for Quiz 4