CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Advertisements

CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 04 / 2008 Instructor: Michael Eckmann.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
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
DAT602 Database Application Development Lecture 5 JAVA Review.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
BIT115: Introduction to Programming
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 08 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
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.
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
Control Statements Loops.
Lab5 PROGRAMMING 1 Loop chapter4.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Arrays in Java.
Control Statements Loops.
Presentation transcript:

CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? Loops for repeated execution –break; and continue; in loops –nested loops –labeled break and continue arrays

break; We've seen how break; reacts in switch statements. break; acts similarly within the curly braces of a while loop, for loop or do-while loop. It terminates the loop and program control continues after the loop as if it ended normally. Michael Eckmann - Skidmore College - CS Spring 2008

break; int x = 0; while (x <= 10)‏ { if (x == 5)‏ break; System.out.println(“x = “ + x); x++; } // what's this code going to do? Michael Eckmann - Skidmore College - CS Spring 2008

continue; The continue; is a way to cause your loop to skip the rest of the current iteration of the loop and continue on to the next iteration. So, what it does, it basically skips all the code after it and then goes to the next iteration. It acts slightly differently in for loops vs. while or do-while loops. After the rest of the current iteration is skipped, for a for loop, the next thing that happens is the expression3 then expression2 is evaluated to determine if it should go again. Michael Eckmann - Skidmore College - CS Spring 2008

continue; However, continue; within a while or a do-while loop acts like: After the rest of the current iteration is skipped, the next thing that happens is the condition is tested to determine if it should go again. Michael Eckmann - Skidmore College - CS Spring 2008

continue; for (int x = 0; x <= 10; x++)‏ { if (x == 5)‏ continue; System.out.println(“x = “ + x); } // what's this code going to do? Michael Eckmann - Skidmore College - CS Spring 2008

continue; int x = 0; while (x <= 10)‏ { if (x == 5)‏ continue; System.out.println(“x = “ + x); x++; } // what's this code going to do? Michael Eckmann - Skidmore College - CS Spring 2008

exercise Write an application that keeps displaying in the command window the powers of the integer 2, namely 2, 4, 8, 16, 32,... Your loop should not terminate (i.e. you should create an infinite loop.)‏ Michael Eckmann - Skidmore College - CS Spring 2008

Nested loops The body of a loop can contain another loop. We say that the inner loop is nested inside (or within) the outer loop. It is important to understand that the inner loop executes fully (all its iterations) within each iteration of the outer loop. For example, if the inner loop of a nested loop always iterates 10 times and the outer loop always iterates 5 times --- the code within the inner loop (its body) will end up executing a total of 50 times --- that is, 10 inner-loop iterations for each of the 5 outer-loop iterations. Michael Eckmann - Skidmore College - CS Spring 2008

Nested loop - what prints? public class Printing { public static void main(String args[])‏ { for ( int i = 1; i <= 10; i++)‏ { for ( int j = 1; j<=5; j++)‏ { } System.out.println(); } Michael Eckmann - Skidmore College - CS Spring 2008

Nested loop - what prints? public class Printing2 { public static void main(String args[])‏ { int i = 1, j = 1; for ( ; i <= 10; i++)‏ { for ( ; j<=5; j++)‏ { } System.out.println(); } Michael Eckmann - Skidmore College - CS Spring 2008

Nested loop - what prints? public class Printing3 { public static void main(String args[])‏ { int i = 1, j = 1; while (i <= 10)‏ { while ( j<=5)‏ { j++; } System.out.println(); i++; } Michael Eckmann - Skidmore College - CS Spring 2008

Nested loop - what prints? public class Printing4 { public static void main(String args[])‏ { int i = 1; while (i <= 10)‏ { int j = 1; while ( j<=5)‏ { j++; } System.out.println(); i++; } Michael Eckmann - Skidmore College - CS Spring 2008

Pay attention to control variable The lesson is, to pay attention to the control variables. Does the inner loop's control variable get reset each time through the outer loop?, etc. Michael Eckmann - Skidmore College - CS Spring 2008

labelled break & continue statements labelled break statements within nested structures (while, for, do while or switch structure) –A label can be given to a block of code. A block of code is defined as code enclosed in a pair of curly braces. –The labelled break statement can specify the block to “jump out of.” Non-labelled break statements could only “jump out of” the immediately enclosed structure, whereas, labelled break statements can “jump totally out of” a set of nested structures. Michael Eckmann - Skidmore College - CS Spring 2008

labelled break & continue statements labelled continue statements within nested structures (while, for, or do while structure) –A label can be given to a structure. –The labelled continue statement causes execution to skip the code in the enclosing structures and continues with the next iteration of the structure with the specified label. Michael Eckmann - Skidmore College - CS Spring 2008

labelled break & continue statements Let’s look at an example of how to label a block and nest some loops so that we can do a labelled break; Michael Eckmann - Skidmore College - CS Spring 2008

Arrays Oftentimes we find ourselves needing to store a list of items (e.g. a list of ages, grades, names, etc.)‏ Up until now, if we wanted to store all the titles of each course at Skidmore in a separate variable, we would need to declare hundreds of differently named variables of type String. This would get out of hand quickly. Fortunately there's a data structure called an array that allows us to store a list of items of the same type. Michael Eckmann - Skidmore College - CS Spring 2008

Arrays A data structure of related data items of the same type They store this data in contiguous memory locations Each data item in an array is an element. Arrays are denoted with square brackets. When you want to select one element of the array, use its index in square brackets. Array indices start at 0. When we speak of arrays: array means the full array, and array element is one element of the array. Michael Eckmann - Skidmore College - CS Spring 2008

Arrays Arrays can be of any type (user-defined class types, Java API class types, or primitive types.)‏ Arrays of primitive types: –Might use an array of integers to store test scores. (better than creating individual variables of type int for reasons which should become clear.)‏ –Might use an array of doubles to store a list of temperatures. Arrays of Java API class types: –Might use an array of Strings to store a list of people’s names. Michael Eckmann - Skidmore College - CS Spring 2008

Arrays (declaring and allocating)‏ Arrays, like any variables need to be declared. Arrays also need to have memory allocated for a specific number of elements. Example: int test_scores[]; // declares an array of ints test_scores = new int[ 16 ]; // the line above allocates enough memory for 16 ints Michael Eckmann - Skidmore College - CS Spring 2008

Arrays (declaring and allocating)‏ Arrays can be declared and allocated in one line. Example: // declares an array of ints & allocates enough memory for 16 ints int test_scores[] = new int[ 16 ]; Michael Eckmann - Skidmore College - CS Spring 2008

Arrays (allocating and initializing)‏ When arrays are allocated, the array elements are all initialized to the following values: Arrays of numeric primitive types (byte, short, long, int, float, double, char) have their elements initialized to 0. Arrays of booleans (the only other primitive type) have their elements initialized to false. Arrays of any non-primitive data type, (classes in the Java API or user-defined classes) have their elements initialized to null. Michael Eckmann - Skidmore College - CS Spring 2008

Arrays (declaring, allocating and initializing)‏ Arrays can be declared, allocated and initialized to user defined values all in one line. Example: int test_scores[] = { 130, 120, 111, 44, 181, 183, 160, 165, 148, 147, 146, 153, 139, 150, 153, 122 }; // declares an array of ints & allocates enough memory for 16 ints and // initializes each element to the value listed Michael Eckmann - Skidmore College - CS Spring 2008

Arrays In our example of the test_scores array, each element of this array is an int. So, we can refer to say the 10th element of the array by test_scores[9] and this is an int, so we can use it anywhere an int can be used. REMEMBER: Array indices start at 0 (zero) and end at the (size of the array – 1). –E.g. Indices for an array of size 36, go from 0 to 35. Array values can be set one element at a time by supplying the index like so: // stores the value 130 in the 0 element of the array test_scores test_scores[0] = 130; // stores the value 120 in the 1 element of the array test_scores test_scores[1] = 120; // etc … Michael Eckmann - Skidmore College - CS Spring 2008

Arrays Each array knows its own length, use the array name followed by.length to get access to its length. In our case for the test_scores example, the length is 16 (0 to 15.)‏ Arrays are easily manipulated with loops. Let’s look at an example. Michael Eckmann - Skidmore College - CS Spring 2008

Array code example // Code to populate (store values into the elements of) an array int test_scores[] = new int[ 16 ]; String user_input; for (int i=0; i < test_scores.length; i++)‏ { user_input = JOptionPane.showInputDialog( “Enter an integer”); test_scores[i] = Integer.parseInt(user_input); } Michael Eckmann - Skidmore College - CS Spring 2008

Array code example // Code to find the lowest element of an array of ints. int least = test_scores[0]; for (int i=0; i < test_scores.length; i++)‏ { if (test_scores[i] < least)‏ least = test_scores[i]; } System.out.println("The lowest value in the array is: " + least); Michael Eckmann - Skidmore College - CS Spring 2008

Array code example Let's write a program to find the average test score. What info do we need to compute the average score? Michael Eckmann - Skidmore College - CS Spring 2008