1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, 6.3-6.4, 8.1-8.2 Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
1 Repetition structures Overview while statement for statement do while statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
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.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
Java Unit 9: Arrays Declaring and Processing Arrays.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 4. Controlling Execution SPARCS JAVA Study.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Slides by Evan Gallagher
Chapter 9 Repetition.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
Chapter 5 Repetition.
Conditional Loops.
Outline Altering flow of control Boolean expressions
LRobot Game.
An Introduction to Java – Part I, language basics
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Control Statements Loops.
Lecture Notes – Week 4 Chapter 5 (Loops).
CHAPTER 21 LOOPS 1.
Control Statements Loops.
Repetition Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
Arrays Introduction to Arrays Reading for this Lecture:
MIS 222 – Lecture 12 10/9/2003.
Presentation transcript:

1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops - Examples

2 Break and Continue in Loops The break statement causes execution to “break” out of the repetitive loop execution (goes to just outside the loop’s closing “}”) The continue statement causes execution to “continue” at the end of this pass of a loop (goes to just inside the loop’s closing “}”) Both are discouraged because an alternative way of coding the logic is usually available

3 Break and Continue in Loops Bad practice to use an infinite loop with only break statements to exit the loop: while (true) { if (normal exit condition) break; // body of loop }

4 Break and Continue in Loops Accepted practice for a loop with a normal exit condition to use break statements for exiting the loop on error condition(s): while (normal exit condition) { if (some error condition) { // print an error message e.g. break; } // rest of body of loop }

5 Break and Continue in Loops Not a good practice to use continue at all: while (normal exit condition) { if (condition1) continue; // rest of body of loop } Use an if statement without continue as on the next slide

6 Break and Continue in Loops Use if alone rather than continue: while (normal exit condition) { if (condition2) { // rest of body of loop } Note: condition2 == !condition1

7 “for-each” with Arrays We can use “for-each” loops to access the elements in an array: Example Code: boolean [] array = {true, false, true}; // for-each loop – note difference with for for (boolean entry : array) System.out.println(entry); Example Run: true false true

“for-each” with Arrays Note limitation of “for-each” versus “for” We can not initialize or update the element values in the array using a “for-each” loop for(int num : nums) num = 5; // doesn’t update element We must use a regular “for” loop for that for (int i = 0; i < nums.length; i++) nums[i] = 5; 8

Arrays and Loops - Examples public class BasicArray { public static void main (String[] args) { final int LIMIT = 15, MULTIPLE = 10; int[] list = new int[LIMIT]; // Initialize the array values for (int index = 0; index < LIMIT; index++) list[index] = index * MULTIPLE; list[5] = 999; // change one array value // Print the array values for (int value : list) System.out.print (value + " "); } > run BasicArray > 9

Arrays and Loops - Examples public class ArrayExample { public static void main(String [] args) { char [] vowels = {'a', 'e', 'i', 'o', 'u'}; int [] counts = new int[vowels.length]; String s = "Now is the time for all good men to come to the aid of their country."; for (int i = 0; i < vowels.length; i++) { for (int j = 0; j < s.length(); j++) if (vowels[i] == s.charAt(j)) counts[i]++; } for (int i = 0; i < vowels.length; i ++) System.out.println(vowels[i] + "\'s = " + counts[i]); } > run ArrayExample a's = 2 e's = 6 i's = 4 o's = 9 u's = 1 > 10