CS 100 Introduction to Computing Seminar October 7, 2015.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Advertisements

CS0007: Introduction to Computer Programming
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Loops and Files.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
The switch Statement, DecimalFormat, and Introduction to Looping
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
REPETITION CONTROL STRUCTURE
The switch Statement, and Introduction to Looping
Topics Introduction to Repetition Structures
CHAPTER 5A Loop Structure
Chapter 5: Repetition Structures
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Outline Altering flow of control Boolean expressions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Topics Introduction to Repetition Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Let’s all Repeat Together
Introduction to Repetition Structures
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

CS 100 Introduction to Computing Seminar October 7, 2015

Topics Introduction to Repetition Structures The while Loop: a Condition-Controlled Loop The for Loop: a Count-Controlled Loop Calculating a Running Total Sentinels Input Validation Loops Nested Loops

Introduction to Repetition Structures Often have to write code that performs the same task multiple times  Disadvantages to duplicating code Makes program large Time consuming May need to be corrected in many places Repetition structure: makes computer repeat included code as necessary  Includes condition-controlled loops and count- controlled loops

The while Loop: a Condition- Controlled Loop while loop: while condition is true, do something  Two parts: Condition tested for true or false value Statements repeated as long as condition is true  In flow chart, line goes back to previous part  General format: while condition: statements

The while Loop: a Condition- Controlled Loop (cont’d.)

In order for a loop to stop executing, something has to happen inside the loop to make the condition false Iteration: one execution of the body of a loop while loop is known as a pretest loop  Tests condition before performing an iteration  Will never execute if condition is false to start with  Requires performing some steps prior to the loop

Infinite Loops Loops must contain within themselves a way to terminate  Something inside a while loop must eventually make the condition false Infinite loop: loop that does not have a way of stopping  Repeats until program is interrupted  Occurs when programmer forgets to include stopping code in the loop

Examples (1) count = 0 if count < 5: print ("Hello, I am an if statement and count is", count) while count < 5: print ("Hello, I am a while and count is", count) count = count + 1

Examples (2) password = " " while password != "cs_100": password = input("Please enter the password: ") if password == "cs_100": print("You have entered the correct password") else: print("Sorry the value entered in incorrect - try again")

Examples (3) flag = True while flag: number = int(input("Please enter a number from 0 to 100: ")) if number >= 0 and number <= 100: flag = False else: print("Sorry number must be between 0 and 100") print("Please try again") print("You entered: ", number) print("This is a valid number. ")

Infinite Loop count = 0 while count < 5: print ("Hello, I am a while and count is", count) count = count - 1

Let’s Work A Problem (1) A student takes 5 exams. Write a program that using looping to collect the test scores. The determine the student’s average for the 5 exams. What do we do first?

Let’s Work A Problem (2) Running on a treadmill burns 6.6 calories per minute. Write a program that uses a loop to display the number of calories burned after 5, 10, 15, 20, 25, and 30 minutes. What do we do first?