Arrays After a while. Remember for loops? for(int i = 0; i < 10; i++){ System.out.println(i); } -But what if we are unsure of how many cycles we need?

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Arrays.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
ITC 240: Web Application Programming
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using.
Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01. OutlineOutline  The for statement  The for statement’s format  The for statement’s flow  Comparing.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CPS120 Introduction to Computer Science Iteration (Looping)
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 8: Arrays.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
David Stotts Computer Science Department UNC Chapel Hill.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Arrays Art &Technology, 3rd Semester Aalborg University Programming David Meredith
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Research Topics in Computational Science. Agenda Survey Overview.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
CPS120 Introduction to Computer Science Iteration (Looping)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table,
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Arrays.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
While ( number
Loops and Arrays Chapter 19 and Material Adapted from Fluency Text book.
Comp1004: Loops and Arrays I Whiles, For and Arrays[]
Follow up from lab See Magic8Ball.java Issues that you ran into.
CSC 211 Java I for loops and arrays.
Lecture 4b Repeating With Loops
[Array, Array, Array, Array]
Chapter 5 Structures.
Arrays, For loop While loop Do while loop
Arrays .
LabVIEW.
[Array, Array, Array, Array]
Building Java Programs
The for loop suggested reading:
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Week 7 - Monday CS 121.
Presentation transcript:

Arrays After a while

Remember for loops? for(int i = 0; i < 10; i++){ System.out.println(i); } -But what if we are unsure of how many cycles we need? 1.declare and define a counter variable 2. boolean condition, checked every iteration 3. update counter variable every iteration

while loops Allow us to loop for an indeterminate amount of cycles Example: while(door.isLocked()){ ringDoorBell(); waitForResponse(10); } New Keyword boolean condition inside parentheses Curly braces encompass code to be cycled over

Beware of infinite loops! If the boolean condition of a while loop is always true, the loop will never exit This is called an infinite loop and can be a really annoying bug to fix Used to cause computers to freeze, now OS’s are more robust Still bad news for your program

A Problem Until now, we have used variables that hold a single object/value What if we want to hold lots of data? Imagine trying to store the first 20 numbers of the Fibonacci sequence? int _firstNum; int _secondNum; int _thirdNum;... int _twentiethNum; This is annoying, and what about storing the first 1,000 or 1,000,000?

Arrays to the rescue! Arrays store a specified number of homogenous(same type) data elements (variables) You can refer to a specific element by its index Indexing starts at 0 and ends at the size of the array – 1 This is a visualization of an array of size 20:

Real life “arrays”

Array Syntax Declaration: int[] fibNumbers; Definition pt. 1: fibNumbers = new int[5]; Type of variable the array will hold Square brackets denote array Array Name Array size

Initializing Elements of the array Definition pt.2: fibNumbers[0] = 1; fibNumbers[1] = 1; fibNumbers[2] = 2; fibNumbers[3] = 3; fibNumbers[4] = 5; But wait a minute, this isn’t any better than before!

A Cleverer Way to Initialize Using a for loop to initialize the elements of an array is much better Let’s do this for the Fibonacci sequence