CS100J October 21, 2003 For Loops Reading: Secs 7.5-7.6 Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
For(int i = 1; i
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
1 CS100J February 28, 2006 Loops, iterative statements, or repetitive statements A bit about graphics From news.com, on 23 February 2006, about browser.
1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
Start. More Loops 03/14/11 Look at geometric series example.
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
1 CS100J October 06, 2005 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 CS100J 15 March, 2006 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
How to Be an Effective Teacher The First Days of School By Drs. Harry K. Wong and Rosemary T.Wong.
Simple Python Loops Sec 9-7 Web Design.
Presented by Lee Zenke 2015 Java Programming PT. 2.
1 CS1110, 20 Oct. 2009, Lec 14 Elementary graphics; intro to loops and for-loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive.
The Right Perspective is Everything! Glenna Batts-Gonzalez, MBA.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
1 CS1110, 8 March 2009 Two topics: elementary graphics (for A5); loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
1 CS March 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
1 CS100J 4 March 2008 Two topics: Turtles; loops Start reading Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Unit 01 – Lesson 08 – Inductive Reasoning Essential Question  How can you use reasoning to solve problems? Scholars will  Make conjectures based on inductive.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Sum of Arithmetic Sequences. Definitions Sequence Series.
1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
1 CS100J 06 March 2008 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Sentinel Loops. while Syntax while(expression) statement.
More about Iteration Victor Norman CS104. Reading Quiz.
C++ Iterative Constructs
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
for Repetition Structures
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
CS October 2009 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Repetition-Sentinel,Flag Loop/Do_While
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.
الحلقات التكرارية وجمل الدوران (loops)
For & do/while Loops.
Intro to Programming Week # 4 Control Structure Lecture # 8
CS October 2008 The while loop and assertions
The for-loop and Nested loops
Lists in Python.
Counting Loops.
Programming Training Main Points: - Problems with repetitions.
Бази от данни и СУБД Основни понятия инж. Ангел Ст. Ангелов.
Loops.
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
CS100J 16 Oct, 2007 Assignment A5: loops
CS October 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Presentation transcript:

CS100J October 21, 2003 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself do the thing you have to do, when it ought to be done, whether you like it or not; it is the first lesson that ought to be learned; and however early a man's training begins, it is probably the last lesson that he learns thoroughly. Thomas Henry Huxley Technical Education, 1877 Q1, Q2, and A2 are in the Carpenter basement, to be picked up when a consultant is there.

The for-loop // {n >= 0} int k= 1; x= 0; // inv: x = sum of 1..(k-1) while (k != 20) { x= x + k; k= k+1; } // x = sum of k is a loop counter. Initialized before the loop and changed only in the last statement of the repetend of the while-loop

The for loop // {n >= 0} int k= 1; x= 0; // inv: x = sum of 1..(k-1) while (k != 20) { x= x + k; k= k+1; } // x = sum of k is a loop counter. Initialized before the loop and changed only in the last statement of the repetend of the while-loop // {n >= 0} x= 0; // inv: x = sum of 1..(k-1) for (int k = 1; k != 20; k= k+1) { x= x + k; } // x = sum of 1..19

Syntax and semantics of the for loop k is a loop counter. Initialized before the loop and not changed in the repetend of the for-loop. x= 0; // inv: x = sum of 1..(k-1) for (int k = 1; k != 20; k= k+1) { x= x + k; } // x = sum of for ( initialization ; condition ; progress ) { repetend } meaning of for-loop: initialization while ( condition ) { repetend progress }

Scope of the counter of a for-loop x= 0; // inv: x = sum of 1..(k-1) for ( int k = 1; k != 20; k= k+1) { x= x + k; } // x = sum of System.out.println(k); x= 0; int k= 1; // inv: x = sum of 1..(k-1) while (k != 20) { x= x + k; k= k + 1; } // x = sum of System.out.println(k); Illegal: Scope of k is only the loop.

A for-loop schema to process a range of integers // Process m..n // inv: m..k-1 has been processed and m <= k <= n+1 for (int k= m; k <= n; k= k+1) { Process k; } // Post: m..n has been processed

Use the schema to count number of ‘e’s in a string // Process m..n // inv: m..k-1 processed, m <= k <= n+1 for (int k= m; k <= n; k= k+1) { Process k; } // Post: m..n processed // Store in x the no. of ‘e’s in s[0..s.length()-1] // inv: m..k-1 processed, m <= k <= n+1 for (int k= m; k <= n; k= k+1) { Process k; } // Post: m..n processed Example: s = “sequioa”, x = 1 s = “defense”, x = 2 m: n:

Which loop condition do you like better? // Process 0..n k= 1; // inv: 0..k-1 processed, 0 <= k <= n+1 while (k <= n) { Process k; k= k+1; } // post: 0..n processed // Process 0..n k= 1; // inv: 0..k-1 processed, 0 <= k <= n+1 while (k != n+1) { Process k; k= k+1; } // post: 0..n processed