ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.

Slides:



Advertisements
Similar presentations
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Advertisements

Computer Science 1620 Loops.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
1 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
COMP Loop Statements Yi Hong May 21, 2015.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Arrays. Arrays are objects that help us organize large amounts of information.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Lecture 5 array declaration and instantiation array reference
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Chapter 6 More Conditionals and Loops
Loop Structures.
Arrays, For loop While loop Do while loop
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
What output is produced by the following fragment?
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
Chap 7. Advanced Control Statements in Java
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Arrays.
Presentation transcript:

ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays

ECE122 L11: For loops and Arrays March 8, 2007 Outline °Problem: How can I perform the same operations a fixed number of times? °Considering “for loops” Performs same operations as while and do-while °Structure provides more compact representation °Arrays Efficient representation of large amount of data

ECE122 L11: For loops and Arrays March 8, 2007 The for loop °A for statement has the following syntax: for ( initialization ; condition ; increment ) statement; The initialization is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration

ECE122 L11: For loops and Arrays March 8, 2007 Logic of a for loop statement true condition evaluated false increment initialization Does initializing, pretest, increment and posttest …..

ECE122 L11: For loops and Arrays March 8, 2007 The for Statement °A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; } Know how to write the same functionality in ALL loops.

ECE122 L11: For loops and Arrays March 8, 2007 The for Statement °An example of a for loop: for (int count=1; count <= 5; count++) System.out.println (count); °The initialization section can be used to declare a variable °Like a while loop, the condition of a for loop is tested prior to executing the loop body °Therefore, the body of a for loop will execute zero or more times

ECE122 L11: For loops and Arrays March 8, 2007 The for Statement °The increment section can perform any calculation °A for loop is well suited for executing statements a specific number of times °Question: Can I use num outside of the loop? for (int num=100; num > 0; num -= 5) System.out.println (num);

ECE122 L11: For loops and Arrays March 8, 2007 The for Statement °  Each expression in the header of a for loop is optional °If the initialization is left out, no initialization is performed °If the condition is left out, it is always considered to be true, and therefore creates an infinite loop °If the increment is left out, no increment operation is performed °continue keyword °break keyword

ECE122 L11: For loops and Arrays March 8, 2007 Break & Continue °Continue statement means “skip to the end of the loop and do the next iteration” °Break means “skip to the end of the loop and exit the loop”. for(int idx = 0; idx < 10; idx++) { if(idx < 4) continue; if(idx > 7) break; System.out.println(“Counter is “ + idx); }

ECE122 L11: For loops and Arrays March 8, 2007 Example of break int sum = 0; int item = 0; while (item < 5) { item ++; sum += item; if (sum >= 6) break; } System.out.println("The sum is " + sum); Break makes it difficult to determine how many times the loop Is executed

ECE122 L11: For loops and Arrays March 8, 2007 Example of continue int sum = 0; int item = 0; while (item < 5) { item++; if (item == 2) continue; sum += item; } System.out.println("The sum is " + sum); Continue can be used for special conditions

ECE122 L11: For loops and Arrays March 8, 2007 For Loop Advice °Don’t use the index counter after exit from the loop. °Don’t modify the index value inside the loop °Use continue and break with caution. Having one place where the exit criteria of the loop is stated is a good thing

ECE122 L11: For loops and Arrays March 8, 2007 For Loop Advice °Try to limit nesting to no more than three levels of loops in one section of code °Try to keep the bodies of loops fairly small, ideally within one page view. °Be even more careful about using breaks or continues in very long loop bodies for (int num=100; num > 0; num -= 5) { if (num == 10); break; }

ECE122 L11: For loops and Arrays March 8, 2007 From while to for int i = startValue; while (i < endValue) {..... i++; } for (int i=startValue; i<endValue; i++) {... }

ECE122 L11: For loops and Arrays March 8, 2007 Nesting for-loops °Inside the loop body of a for-loop, we can put another for- loop °Each time through the 1 st for-loop, we execute the 2 nd loop until its guard is false °Handy for printing tables like this:

ECE122 L11: For loops and Arrays March 8, 2007 Simple example for (int i=0; i<5; i++) { for (int j=0; j<3; j++) { System.out.print(i+“ ”); } System.out.println(); } How many values are printed out by these loops?

ECE122 L11: For loops and Arrays March 8, 2007 Arrays °An array is an ordered list of values An array of size N is indexed from zero to N-1 scores The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9

ECE122 L11: For loops and Arrays March 8, 2007 Arrays °A particular value in an array is referenced using the array name followed by the index in brackets °For example, the expression scores[2] refers to the value 94 (the 3rd value in the array) °Expression represents a place to store a single integer Can be used wherever an integer variable can be used scores

ECE122 L11: For loops and Arrays March 8, 2007 Arrays °For example, an array element can be assigned a value, printed, or used in a calculation just like any other variable, BUT the array name and the specific entry or item in the array must be unambiguously used in the expression : scores[2] = 89; a = scores[10]; scores[first] = scores[first] + 2; mean = (scores[0] + scores[1])/2; System.out.println ("Top = " + scores[5]); Need both array name and index together!

ECE122 L11: For loops and Arrays March 8, 2007 Arrays °The values held in an array are called array elements °An array stores multiple values of the same type – the element type °  The element type can be a primitive type or an object reference °We can create an array of integers, an array of characters, an array of String objects, etc °  In Java, the array itself is an object that must be instantiated (will show ahead)

ECE122 L11: For loops and Arrays March 8, 2007 Arrays °Another way to depict the scores array: scores This is telling you that scores (itself) is a Reference! ‘Scores’ points to the array.

ECE122 L11: For loops and Arrays March 8, 2007 Declaring Arrays °The scores array could be declared as follows: int[] scores = new int[10]; °Note the syntax, the ‘reference’ and the new object! It also says that there will be 10 scores referenced. °The type of the variable scores is int[] (“an array of integers” or “an array of ints.”) °Note that the array type does not specify its size, but each object of that type has a specific size °The reference variable scores is set to a new array object that can hold 10 integers

ECE122 L11: For loops and Arrays March 8, 2007 Declaring Arrays °Some other examples of array declarations: float[] prices = new float[500]; boolean[] flags; flags = new boolean[20]; char[] codes = new char[1750];

ECE122 L11: For loops and Arrays March 8, 2007 Summary °For loop generally requires an index Indicates number of times loop will be executed °For loop contains an initializer, condition, and condition modifier °Arrays represent data is a series of memory locations All data has same array name Specific locations located with subscripts °For loops and arrays go together well Fixed number of loop iterations and array size