Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

How SAS implements structured programming constructs
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.
Repeating Actions While and For Loops
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.
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.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
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.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for 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.
CS101 Computer Programming I Chapter 4 Extra Examples.
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
FOP: While Loops.
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 4 Repetition Statements (loops)
Introduction To Repetition The for loop
CS161 Introduction to Computer Science
Topics Introduction to Repetition Structures
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.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
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
Intro to Computer Science CS1510 Dr. Sarah Diesburg
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)
Loops CIS 40 – Introduction to Programming in Python
IST256 : Applications Programming for Information Systems
Design and Implementation
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
Based on slides created by Bjarne Stroustrup & Tony Gaddis
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.
Looping and Repetition
Types, Truth, and Expressions (Part 2)
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 input into a program that can’t be valid Seating a party of -4 people 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 your loop variables that make up the Boolean expression somewhere inside the loop you perform some operation which changes the state of the program, eventually leading to a False Boolean expression and exiting the loop Have to have both!

For and Iteration One of Python’s strength’s is its 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 data-structure 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 program that aren’t valid Seating a party of -4 people How can we correct them and ask them to try again?