For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
ITERATION CSC 171 FALL 2004 LECTURE 10. Simple Branching If (test){ A;} start end A; test.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
String Tokenization What is String Tokenization?
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Alice in Action with Java Chapter 10 Flow Control in Java.
Previous Exam 1.0. Question 1 - a Is the following statement true or false? Briefly explain your answer. A && B is the same as B && A for any Boolean.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
UNIT II Decision Making And Branching Decision Making And Looping
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Grouping Related Items
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.
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.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
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.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 StringTokenization Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures II
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Chapter 5: Control Structures II
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
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
If Statement if (amount
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Decisions Bush decision making.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5: Control Structures II
Chapter 5: Control Structures II
TK1114 Computer Programming
SELECTION STATEMENTS (1)
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 6 More Conditionals and Loops
Outline Altering flow of control Boolean expressions
The University of Texas – Pan American
Introduction to Java Programming
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.
Perfect squares class identifySquareButLessClever {
CSS161: Fundamentals of Computing
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

For Loops & Iterators CSC 171 FALL 2001 LECTURE 7

History: Vannevar Bush Vannevar Bush, MIT, built a large-scale differential analyzer with the additional capabilities of integration and differentiation. Funded by the Rockefeller Foundation, the differential analyzer was perhaps the largest computational device in the world in 1930

While loop

While loop Code int year = 0 ; While (year<=20){ balance += balance * interest; year++; }

For loop

For loop Code for (int year = 1;year<=20;year++){ balance += balance * interest; }

For Loop Initialization Test Body Increment for(initialzation;test;increment){ //Body }

Nested Loops Sometimes, we want to perform 2D operations Tables – Addition – Multiplication – Interest rates

Multiplication Table What is the output? int size = 5; // DON’T HARDCODE NUMS for(int i = 0 ; i<size;i++) { for(int j = 0 ; j<size;j++) { System.out.println(String.toString(i*j)); }

Multiplication Table Fixed int size = 5; for(int i = 0 ; i<size;i++) { for(int j = 0 ; j<size;j++) { System.out.print(String.toString(i*j) + “ “); } System.out.println(); }

Enumerations Integers are nice, because we always have a clear idea of what the next one is. But sometimes, we have an orderd set or list of things that aren’t numbers – {Hearts, Spades, Diamonds, Clubs} – {Bob, Carol, Ted, Alice} We would like to go through them one at a time

public interface Enumeration An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:

General Case Enumeration e = v.elements() ; while(e.hasMoreElements()) { System.out.println(e.nextElement()); }

Our Fave: StringTokenizer A String Tokenizer breaks strings up into tokens (surprize!) String “Hello CSC 171, How are you” Tokens: – “Hello”,“CSC”,”171,”,”How”,”are”,”you” StringTokenizer tokenizer = new StringTokenizer(inputLine);

String Tokenizer import java.util.StringTokenizer; public class Split { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); boolean done = false; while (!done) { String inputLine = console.readLine(); if (inputLine == null) done = true; else { // break input line into words

String Tokeizer II StringTokenizer tokenizer = new StringTokenizer(inputLine); while (tokenizer.hasMoreTokens()) { // print each word String word = tokenizer.nextToken(); System.out.println(word); }

The switch statement A sequence of if/else that compares a single integer against constants can be implemented as a switch statement Consider the problem of naming digits 1 -> “one” 2 -> “two” …. 9 – “nine” Exercise – Write : public String digit2name(int n)

If solution public String digit2name(int n) { String name; if (n == 1) name = “one”; else if (n == 2) name = “two”; else if (n == 3) name = “three”; // etc... else if (n == 9) name = “nine”; else name = “”; return name; }

switch solution public String digit2name(int n) { String name; switch(n) { case 1: name = “one”; break; // check your breaks! case 2: name = “two”; break; // etc case 9: name = “nine”; break; default: name= “”; } return name; }

Constructor & Accesor Methods In order to prevent inadvertent (buggy) changes to an object, we want to limit access to the object’s data

Some instance varriables public class Student { public String studentName; public int studentNumber; }

Usage Student s1 = new Student(); s1.studentName = "Holly Yashi"; s1.studentNumber = ;

Control access with a constructor public class Student { private String studentName; private int studentNumber; public Student(String name; int number) studentName=name; studentNumber = number; }

Control access with accessors public class Student { private String studentName; private int studentNumber; public Student(String name; int number) studentName=name; studentNumber = number; } public String getname() { return studentName;} public int getnumber(){return studentNumber;} }

Usage Student s1 = new Student(“Holly Yashi”, ); System.out.println( s1.getname() + “’s student number is :” + s1.getnumber); // allows setting at construction time only