Lecture 7: Whiles - Indefinite Loops Input and while loops Katie Coons CS 305J February 2, 2007.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Lecture 19: Reading Input from Files Announcements & Review Lab 6 Due Thursday arrays of objects more flight reservations Last time: static methods vs.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
CS 445 Lecture #2 Lexical Analysis. Regular Expressions ε is a r.e. Any char in the alphabet is a r.e. If r and s are r.e.’s then r | s is a r.e. If r.
Building Java Programs
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
1 CS 177 Week 4 Recitation Slides Conditionals. 2 Announcements Project 1 is due on Feb. 7th 9pm (extended) Project 2 will be posted today (Feb. 5th)
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Using Objects CS 101-E Aaron Bloomfield. Announcements Midterm 1 is a week from this Wednesday Midterm 1 is a week from this Wednesday TESTS ARE IN CHM.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
CS1101: Programming Methodology Aaron Tan.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Java Fundamentals Expanded SE1021 Dr. Mark L. Hornick 1.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
Lec 6 Logical Operators String comparison. Review – if statement syntax OR.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
COMP String and Console I/O Yi Hong May 18, 2015.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
1 Chapter 4 - Control Statements Conditions if Statement && Logical Operator || Logical Operator ! Logical Operator switch Statement while Loop do Loop.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Iteration. Java looping Options –while –do-while –for Allow programs to control how many times a statement list is executed.
Objects. Getting classy Your current job –Gain experience creating and manipulating objects from the standard Java types Why –Prepares you for defining.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Week 3 - Friday.  What did we talk about last time?  Operations on boolean values  !, &&, ||  Operations on char values  +, -  Operations on String.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
1 Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield.
A: A: double “4” A: “34” 4.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
Lecture 4: For Loops Announcements & Review Announcements Examples posted after lecture on schedule Discussion Section exercises on line before 9am Tuesdays.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Introduction to programming in java
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Chapter 3 Spring 2005 CS 101 Aaron Bloomfield
Java for Beginners University Greenwich Computing At School DASCO
Objects.
CS305J Introduction to Computing
Computer Programming Methodology Input and While Loop
Iteration.
Building Java Programs Chapter 4
CS 177 Week 4 Recitation Slides
Review Operation Bingo
CS Week 7 Jim Williams, PhD.
An Introduction to Java – Part I, language basics
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Lec 4: while loop and do-while loop
CS 200 Arrays Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
Chapter 3 Spring 2006 CS 101 Aaron Bloomfield
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Building Java Programs
Announcements & Review
Announcements & Review
Building Java Programs
Random Numbers while loop
CSS161: Fundamentals of Computing
Building Java Programs
Computer Science Club 1st November 2019.
Presentation transcript:

Lecture 7: Whiles - Indefinite Loops Input and while loops Katie Coons CS 305J February 2, 2007

Lecture 7: Whiles - Indefinite Loops About Katie Coons Hometown: Falls Church, VA (near DC) First CS class: Summer 1998 DMP Summer Program, Summer 2004 BS in CS, University of Virginia, 2001 MS in CS, UT Austin, 2007? PhD in CS –Specialty in compilers and architecture –UT Austin, 2012? I hope? After that: Who knows?

Lecture 7: Whiles - Indefinite Loops Announcements & Review Announcements: Lab 2 Due –Thursday 10pm –1 file per pair Last Time: objects versus values strings & their operators

Lecture 7: Whiles - Indefinite Loops Review "excellence"a b String a = "excellence"; String b = a; alphabet abcdefghijklmnopqrstuvwyz String alphabet = "abcdefghijklmnopqrstuvwxyz"; char c1 = alphabet.charAt(9); // ‘j’ int i1 = alphabet.indexOf(“de”); // 3

Lecture 7: Whiles - Indefinite Loops Review "excellence"a b String a = "excellence"; String b = “excellence”; alphabet abcdefghijklmnopqrstuvwyz String alphabet = "abcdefghijklmnopqrstuvwxyz"; char c1 = alphabet.charAt(9); // ‘j’ int i1 = alphabet.indexOf(“de”); // 3 "excellence"

Lecture 7: Whiles - Indefinite Loops Today Input Using the Scanner class Indefinite Loops while (condition) { …. // somewhere in loop, condition becomes false …. }

Lecture 7: Whiles - Indefinite Loops Reading Input Features // Scanner is an “extra” Java class, so we // need to tell the compiler where to find it import java.util.*; // allocates an object of class Scanner Scanner stdin = new Scanner(System.in); String name = stdin.next(); int age = stdin.nextInt(); double commuteDistance = stdin.nextDouble(); boolean parent = stdin.nextBoolean(); // peeking to make sure the format is right // -- we will use these in Lecture 8 boolean isString = stdin.hasNext(); boolean isInt = stdin.hasNextInt(); boolean isDouble = stdin.hasNextDouble(); boolean isBoolean = stdin.hasNextBoolean();

Lecture 7: Whiles - Indefinite Loops Reading Input Example // Scanner is an “extra” Java class, so we // need to tell the compiler where to find it import java.util.*; // allocates an object of class Scanner Scanner stdin = new Scanner(System.in); System.out.print("Enter a word: "); String word = stdin.next(); int wordLength = word.length(); System.out.println("Word " + word + " has length " + wordLength + ".");

Lecture 7: Whiles - Indefinite Loops BlueJ Examples

Lecture 7: Whiles - Indefinite Loops Indefinite Loops boolean condition = true; // keep doing the loop while condition==true while (condition) { …. // somewhere in the loop the condition // becomes false. Why? if (test) { condition = false; } Lecture 7: While Loops

Lecture 7: Whiles - Indefinite Loops What does this do? int i = 0; while (i < 10) { System.out.println(“I’ve got soul, but I’m not a soldier.”); i++; }

Lecture 7: Whiles - Indefinite Loops What does this do? int i = 0; while (i < 10) { System.out.println(“I’ve got soul, but I’m not a soldier.”); i++; } // a “for” is more succinct in this case for (int i = 0; i < 10; i++) { System.out.println(“I’ve got soul, but I’m not a soldier.”); }

Lecture 7: Whiles - Indefinite Loops When do we while? boolean condition = true; // keep doing the loop while condition==true while (condition) { …. // somewhere in the loop the condition // becomes false. Why? if (test) { condition = false; }

Lecture 7: Whiles - Indefinite Loops Reading Input Features String word1 = stdin.next(); int number1 = stdin.nextInt(); double dnum1 = stdin.nextDouble(); boolean progress1 = stdin.nextBoolean(); // peeking to make sure the format is right boolean isString = stdin.hasNext(); boolean isInt = stdin.hasNextInt(); boolean isDouble = stdin.hasNextDouble(); boolean isBoolean = stdin.hasNextBoolean();

Lecture 7: Whiles - Indefinite Loops Reading Input Example Scanner stdin = new Scanner(System.in); // Is there anything wrong with this code? System.out.println("Enter a box width: "); int width = stdin.nextInt();

Lecture 7: Whiles - Indefinite Loops BlueJ Examples

Lecture 7: Whiles - Indefinite Loops Questions?