1 CS100J February 28, 2006 Loops, iterative statements, or repetitive statements A bit about graphics From news.com, on 23 February 2006, about browser.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
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.
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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
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.
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:
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
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.
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.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Visual Basic Programming
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.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Counting Loops.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
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.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
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.
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.
Chapter 7 Introduction to High-Level Language Programming.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 02 Data and Expressions.
Chapter 4 Repetition Statements (loops)
while Repetition Structure
Start reading Sec and chapter 7 on loops
Department of Computer Science
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.
C# and the .NET Framework
Java Programming: Guided Learning with Early Objects
Chapter 4 – Control Structures Part 1
Programming Fundamentals
While Loops Chapter 3.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Arrays, For loop While loop Do while loop
CS October 2008 The while loop and assertions
Outline Altering flow of control Boolean expressions
Lecture Notes – Week 3 Lecture-2
Control structures to direct program flow
Lecture 8:The For Loop AP Computer Science Principles.
For loops Taken from notes by Dr. Neil Moore
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Introduction to Computer Science
Class code for pythonroom.com cchsp2cs
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.
Chapter 4: Loops and Iteration
Presentation transcript:

1 CS100J February 28, 2006 Loops, iterative statements, or repetitive statements A bit about graphics From news.com, on 23 February 2006, about browser security. From U.C. Berkeley talk by David Wagner. In 2004, Internet Explorer was "unsafe" 358 days of the year, i.e. it contained a publicly known, remotely exploitable hole for which no patch was available. It would take 463 days to install all known IE patches to make IE secure. 34 IE bugs were without patches. Opera was "safe" 300 days of the year. None of Opera's bugs went without a patch. It would take 93 days to fix them. Firefox was "safe" 339 days. Two of its bugs went without a patch. It would take 43 days to install its fixes. Start reading Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.

2 Problem with leading zeros: found by CS100J student Miguel Salas. p216 DecOct Why did I get a Christmas card on Halloween? In Java, a leading 0, as in 010 means the integer is intepreted as octal, so 010 represents the decimal 8. Also, 09 is illegal. Java should never have made this choice, because is flies against mathematical conven- tion and can cause confusion and hard-to-find bugs

3 The for loop, for processing a range of integers x= 0; // add the squares of ints // in range to x x= x + 2*2; x= x + 3*3; … x= x + 200; repetend: the thing to be repeated. The block: { x= x + i*i; } for each number i in the range , add i*i to x. The for-loop: for (int i= 2; i <= 200; i= i +1) { x= x + i*i; } loop counter: i initialization: int i= 2; loop condition: i <= 200; increment: i= i + 1 repetend or body: { x= x + i*i; }

4 Execution of the for-loop The for-loop: for (int i= 2; i <= 4; i= i +1) { x= x + i*i; } loop counter: i initialization: int i= 2; loop condition: i <= 200; increment: i= i + 1 repetend or body: { x= x + i; } Called a “flow chart” To execute the for-loop. 1.Execute initialization. 2.If loop condition is false, terminate execution. 3.Execute the repetend. 4.Execute the increment and repeat from step 2. i= 2; i <= 4 i= i +1; true false x= x + i*i;

5 Execution of the for-loop The for-loop: for (int i= 2; i <= 4; i= i +1) { x= x + i*i; } loop counter: i initialization: int i= 0; loop condition: i <= 200; increment: i= i + 1 repetend or body: { x= x + i; } Trace execution of for-loop. We do it as shown below, rather than using a single box, for x and one for i, so that we can keep track of when events happened. x i2345

6 The pattern for processing range of integers: range a..b range c..d–1 for (int i= a; i <= b; i= i + 1) { Process integer i; } // Print the indices of all ‘e’s in String s for (int i= 0; i != s.length(); i= i +1) { if (s.charAt(i) == ‘e’) System.out.println(i); } for (int i= c; i != d; i= i + 1) { Process integer i; } // Store in double variable v the sum // 1/1 + 1/2 + …+ 1/n v= 0; for (int i= 0; i <= n; i= i +1) { v= v / i; }

7 The pattern for processing range of integers: range a..b range c..d–1 for (int i= a; i <= b; i= i + 1) { Process integer i; } // Store in double variable v the sum // + 1/1 – 1/2 + 1/3 – 1/4 + 1/5 – … + 1/n v= 0; for (int i= 0; i <= n; i= i +1) { if (i % 2 == 1) v= v / i; else v= v – 1.0 / i; } for (int i= c; i != d; i= i + 1) { Process integer i; }

8 The pattern for processing range of integers: range a..b for (int i= b; i >= a; i= i – 1) { Process integer i; } // Store in m the sum of the even integers in m= 0; for (int i= 10; i <= n; i= i + 2 ) { v= v + i; }

9 Graphical User Interfaces (GUIs): graphics. A JFrame, with a panel on which you can draw jframe= new JFrame("Turtle window"); panel= new JPanel(); panel.setPreferredSize(new Dimension(width, height)); panel.setBackground(Color.white); jframe.getContentPane().add(panel, BorderLayout.CENTER); jframe.pack(); jframe.setVisible(true); graphics= panel.getGraphics(); A “panel” in which you can draw You don’t have to learn all this unless you want to. We will be telling you more and more about GUIs as the course progresses.

10 Commands to draw // Draw a line from (10, 10) to (50, 40). d.graphics.drawLine(10,10,50, 40); // Draw a rectangle with top-left point (2, 5), width 40, and height 60 d.graphics.drawRect(2, 5, 40, 60); // Fill a rectangle with top-left point (50, 70), width 40, and height 60 d.graphics.fillRect(50, 70, 40, 60); (0,0) (0,1) (0, 2) … (1,0) (1,1), (1,2) … (2,0) (2,1), (2,2) … … The panel: each pair (i,j) is a “pixel” or picture element.

11 Commands to draw // Draw string s at (40, 30) d.graphics.drawString(s, 40, 30); // set the pen color to red d.graphics.setColor(Color.red); // Store the current color in c Color c= d.graphics.getColor(); // Draw an oval with top-left point (2, 5), width 40, and height 60 d.graphics.drawRect(2, 5, 40, 60); // Fill an oval with top-left point (50, 70), width 40, and height 60 d.graphics.fillRect(50, 70, 40, 60); (0,0) (0,1) (0, 2) … (1,0) (1,1), (1,2) … (2,0) (2,1), (2,2) … … For more on graphics, see class Graphics in the Java API and page 1-5 in the CD ProgramLive. For more on GUIs, read chapter corresponding part of the CD is much easier!