Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS0004: Introduction to Programming Repetition – Do Loops.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CS 100 Introduction to Computing Seminar October 7, 2015.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
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.
Testing Programs with Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Repetition Statements
FOP: While Loops.
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 4 Repetition Statements (loops)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Introduction To Repetition The for loop
CS161 Introduction to Computer Science
Lesson 05: Iterations Class Chat: Attendance: Participation
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Topics Introduction to Repetition Structures
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.
More Repetition While and For Loops Sentinel-Controlled Loops
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Types, Truth, and Expressions (Part 2)
A First Book of ANSI C Fourth Edition
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
IST256 : Applications Programming for Information Systems
Design and Implementation
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
More Repetition While and For loop variations
Topics Introduction to Repetition Structures
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Types, Truth, and Expressions
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
CS2011 Introduction to Programming I Loop Statements (I)
Looping and Repetition
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions
Presentation transcript:

Intro to Computer Science CS1510 Dr. Sarah Diesburg Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg

How can we introduce error checking? Suppose that students are entering numbers into your miles per gallon program that aren’t valid

How can we introduce error checking? Suppose that students are entering numbers into your miles per gallon program that aren’t valid Values not above 0 Ending value that is “smaller” than the starting value Maybe even values above some upper limit How can we correct them and ask them to try again?

Repeating Statements Besides selecting which statements to execute, a fundamental need in a program is repetition repeat a set of statements under some conditions Between selection and repetition, we have the two most necessary programming statements

While and For Statements The while statement is the more general repetition construct. It repeats a set of statements while some condition is True. Often called a sentinel controlled loop The for statement is useful for iteration, moving through all the elements of data structure, one at a time. Often called a count controlled loop

while Loop Top-tested loop (pretest) while boolean expression: test the boolean before running Run the program suite test the boolean before each iteration of the loop while boolean expression: statementSuite

Repeat While the Boolean is True while loop will repeat the statements in the suite while the boolean is True (or its Python equivalent) If the boolean expression never changes during the course of the loop, the loop will continue forever.

While Loop Example x_int = 0 while x_int < 10: print (x_int) x_int = x_int + 1 print() print( "Final value of x_int: ", x_int) What is the Final Value printed by this code?

General Approach to a While outside the loop, initialize the boolean somewhere inside the loop you perform some operation which changes the state of the program, eventually leading to a False boolean and exiting the loop Have to have both!

For and Iteration One of Python’s strength’s is it’s rich set of built-in data structures The for statement is a common statement for manipulation of a data structure for each element in the datastructure perform some operation on that element

For Loop Example numbers = [0,1,2,3,4,5,6,7,8,9] for xInt in numbers: print (xInt) print() print ("Final value of xInt: " + str(xInt) )

How can we introduce error checking? Suppose that students are entering numbers into your miles per gallon program that aren’t valid Values not above 0 Ending value that is “smaller” than the starting value Maybe even values above some upper limit How can we correct them and ask them to try again?