Comp1004: Loops and Arrays I Whiles, For and Arrays[]

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

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repeating Actions While and For Loops
Mock test review Revision of Activity Diagrams for Loops,
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Repetition Statements while and do while loops
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CPS120 Introduction to Computer Science Iteration (Looping)
Counting Loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Sophomore Scholars Java
FOP: While Loops.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
CSC 211 Java I for loops and arrays.
Lecture contents Looping Arrays For each loop while do while for
Lecture 4b Repeating With Loops
The switch Statement, and Introduction to Looping
Chapter 5: Control Structures II
Loop Structures.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Computation as an Expressive Medium
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Repeating Instructions And Advance collection
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Outline Altering flow of control Boolean expressions
Introduction to Processing Digital Sounds part 2
Java Language Basics.
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.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Topic 1: Problem Solving
Module 4 Loops.
Lec 4: while loop and do-while loop
Chapter 6: Repetition Statements
Lab5 PROGRAMMING 1 Loop chapter4.
Computing Fundamentals
A LESSON IN LOOPING What is a loop?
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
Loops.
Java LESSON 3 Loops.
PROGRAM FLOWCHART Iteration Statements.
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Welcome back! October 11, 2018.
Loops CGS3416 Spring 2019 Lecture 7.
Looping Structures.
Presentation transcript:

Comp1004: Loops and Arrays I Whiles, For and Arrays[]

HouseKeeping Coursework 1 has been set –Worth 10% of your module marks –“The aim of this coursework is to model a pack of playing cards, write a method to shuffle the pack of cards and write a short game of ‘higher or lower’ ” Deadline: Friday 18th November 2011 at –via Also – Lab Marking!

Overview Looping – while – do while – for Arrays – indexes For each loop

Looping While: The King of Loops

Looping Sometimes we want a computer to do something over and over and over and over and over again We can achieve this with a loop A programming structure that performs a series of instructions repeatedly until some specified condition is met

There are different kinds of loops... while do while for for each (enhanced for) Recursion (not really a loop)

While The fundamental loop All of the other loops can be built from this

While while(condition){ code to run; } Just like an if statement condition if(condition) { code to run; } Zero or More N.B. The condition is checked at the start of the loop – so the code inside may not run at all

While – Example 1 int i = 0; while(i<10){ System.out.println(“The number is ” + i); i= i+1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement?

While – Example 1 int i = 0; while(i<10){ System.out.println(“The number is ” + i); i= i+1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement? i starts as 0 and the loop stops when i becomes equal to 10. So the loop will print the numbers 0-9 and the final statement will print number 10

While – Example 3 int i = 10; while(i<10){ System.out.println(“The number is ” + i); i= i+1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement?

While – Example 3 int i = 10; while(i<10){ System.out.println(“The number is ” + i); i= i+1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement? i starts as 10 and the loop stops when i becomes equal to 10. So the loop will not start at all and the final statement will print number 10

While – Example 4 int i = 0; while(i<10){ System.out.println(“The number is ” + i); i= i*1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement?

While – Example 4 int i = 0; while(i<10){ System.out.println(“The number is ” + i); i= i*1; } System.out.println(“i is now ” + i); What numbers will be printed by the loop and the final statement? This is called an infinite loop. They cause programs to hang and/or crash. Always check that you loops will end!

Looping Other Loops

Do while int count = 1; do { System.out.println("Count is: " + count); count++; } while (count <= 11); One or More N.B. The condition is checked at the end of the loop – so the code inside will always run at least once

For loop for (initialization; condition; statechange) { statement(s) } Counting The for loop is a simple way of repeating a block of code when you know in advance how many iterations (times around the loop) that you want

For loop for (int i = 0; i < 10; i = i + 1) { System.out.println(i); } Counting The for loop is a simple way of repeating a block of code when you know in advance how many iterations (times around the loop) that you want initialization; condition; statechange

For loop for (int i = 0; i < 10; i = i + 1) { System.out.println(i); } Counting The for loop is a simple way of repeating a block of code when you know in advance how many iterations (times around the loop) that you want int i = 0; while(i < 10){ System.out.println(i); i = i + 1; } initialization; condition; statechange You can use a while loop to achieve the same thing. But the for loop is neater

Arrays

Sometimes we want to group things together “things” being objects and primitives The most fundamental collection is an Array If a variable is a cup..... an array is a shelf of cups [ ]

Details of Arrays Arrays are objects But they have their own special syntax To declare an Array: int[] numberStore; or int numberStore[]; Both of these are valid. They do the same thing!

Details of Arrays To instantiate an Array: numberStore = new int[10]; This is the length you want the array to be (the number of cups) N.B that we are creating an object here (using the new keyword), but we don’t call a constructor. Arrays have their own special syntax.

Index An index is the position in the array. Java arrays start at 0 [ ]

Length The length is the number of indexes Here the length is 9 [ ]

Using indexes (insertion)

int[] numStore; numStore int[]

Using indexes (insertion) int[] numStore; numStore = new int[9]; [ ] numStore int[]

Using indexes (insertion) int[] numStore; numStore = new int[9]; numStore[0] = 77; [ ] numStore int[]

Using indexes (insertion) int[] numStore; numStore = new int[9]; numStore[0] = 77; [ ] numStore int[] Declaration Instantiation Assignment

Using indexes (insertion) int[] numStore; numStore = new int[9]; numStore[0] = 77; System.out.println(numStore[0]); [ ] numStore int[] Declaration Instantiation Assignment Retrieval

Array rules An array can hold objects or primitives An array is an object, regardless of what it contains Arrays care about type – if you declare an int[] you can only put int s in it* *(not entirely true, look up ‘implicit widening’ if you are interested)

When you play with arrays you will learn to know and hate the – ArrayIndexOutOfBoundsException This happens when you try and access an index that isn’t there Lets use a for loop to expose this

Iterating Over an Array int numStore = new int[9]; //some missing code to fill the array with values for(int i = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } Arrays are powerful because we can use a variable as the array index. This means we can iterate over them in a loop

Iterating Over an Array int numStore = new int[9]; //some missing code to fill the array with values for(int i = 0; i < 10; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } But make sure the index is always valid (in bounds). Otherwise the program will crash when it reaches the line that tries to use an index that is out of bounds

Iterating Over an Array 2 int numStore = new int[9]; //some missing code to fill the array with values for(int i = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } Iterating over an array is so common that Java now includes a loop specifically to do it.

Iterating Over an Array 2 int numStore = new int[9]; //some missing code to fill the array with values for(int i = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } Iterating over an array is so common that Java now includes a loop specifically to do it. The “for each” loop (aka enhanced for loop) is designed to work with collections like arrays The syntax is: for(type variableName : collection){ }

Iterating Over an Array 2 int numStore = new int[9]; //some missing code to fill the array with values for(int i = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } Iterating over an array is so common that Java now includes a loop specifically to do it. int numStore = new int[9]; //some missing code to fill the array with values for(int n : numStore){ System.out.println(“Number is ” + n); } Like the for loop the ‘for each’ loop is a shortcut, that is a bit neater than writing the code the long way.

Iterating Over an Array 2 int numStore = new int[9]; //some missing code to fill the array with values for(int i = 0; i < 9; i++){ System.out.print(“Number at position ” + i); System.out.println(“ is ” + numStore[i]); } Iterating over an array is so common that Java now includes a loop specifically to do it. int numStore = new int[9]; //some missing code to fill the array with values for(int n : numStore){ System.out.println(“Number is ” + n); } Like the for loop the ‘for each’ loop is a shortcut, that is a bit neater than writing the code the long way. But it can only be used for access (e.g. n++ would not increment the value in the array) And it hides the current index

Summary Looping – while – do while – for Arrays – indexes For each loop