CSC 1051 Data Structures and Algorithms I

Similar documents
Algorithms and Conditionals

CSC 1051 Data Structures and Algorithms I

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Data Structures and Algorithms I

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Selection and Repetition Revisited

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

Improved algorithm. Java code. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm

Java code. Updated program. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm. Improved algorithm

Selection and Repetition

Selection Statements and operators

Selection Statements and operators

Selection and Repetition Revisited

Java I/O and Control Structures

Java I/O and Control Structures Algorithms in everyday life

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Selection and Repetition

2: Basics of Java Programming

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Introduction to Arrays

Lecture 3: Variables and assignment

Iterators and File Input

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Chapter 3. Selections

Conditional Statements

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Using Classes and Objects

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

IT 1033: Fundamentals of Programming Loops

Arrays, Part 3: Multidimensional arrays

File I/O and Exceptions

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Introduction to Computer Science Unit 2. Exercises

CS111: PROGRAMMING LANGUAGE II

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Conditionals and Loops Chapter 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

Where do objects come from? Good question!

Basics of Java Programming variables, assignment, and input

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

COMP 202 Java in one week

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Java Coding 3. Over & over again!

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Conditional Programming

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

COMP 202. Java in one week

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Repetition CSC 121 Fall 2014 Howard Rosenthal

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CMPT 125: Lecture 4 Conditionals and Loops

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Data Representation Classes, and the Java API

Designing Classes part 2

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Arrays - Review. Two-Dimensional Arrays. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Chapter 4: Control structures. Repetition

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

LEC03: Decisions and Iterations

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Chapter 4: Control structures

Happy Cinco de Mayo!!!!

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

AP COMPUTER SCIENCE A

Control Structures: if and while A C S L E C T U R E 4

CS 112 Introduction to Programming

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators

Practice Midterm 1 Answer Key

Top-Down Program Development

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

STUDENT LESSON A12 Iterations

Building Java Programs

Happy Cinco de Mayo!!!!

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

Data Structure and Programming Languages

Transcription:

Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus

Topic Thread 2.1 Character Strings 2.2 Variables, Assignment 2.3 Data Types, in particular int, double 2.4 Expressions (simple) 2.5 Data Conversion 2.6 Interactive Programs 5.1 Boolean Expressions 5.2 The if Statement 5.4 The while Statement

Flow of Control The order of statement execution Unless specified otherwise, the order of statement execution through a method is linear Some programming statements allow us to: decide whether or not to execute a particular statement execute a statement over and over, repetitively These decisions are based on boolean expressions (or conditions) that evaluate to true or false

Flow of Control The order of statement execution Unless specified otherwise, the order of statement execution through a method is linear Some programming statements allow us to: decide whether or not to execute a particular statement execute a statement over and over, repetitively These decisions are based on boolean expressions (or conditions) that evaluate to true or false

The while Statement A while statement has the following syntax: while ( condition ) statement; If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false

Logic of a while Loop condition evaluated true false statement

The while Statement int = 1; while ( <= 3) System.out.println ();

The while Statement 1 Initialize int = 1; while ( <= 3) System.out.println ();

The while Statement 1 int = 1; <= 3 is true while ( <= 3) System.out.println ();

The while Statement int = 1; while ( <= 3) Print System.out.println (); Output: 1 1

The while Statement int = 1; while ( <= 3) System.out.println (); Output: 1 2 Increment

The while Statement 2 int = 1; <= 3 is true while ( <= 3) System.out.println (); Output: 1

The while Statement int = 1; while ( <= 3) Print System.out.println (); Output: 1 2 2

The while Statement int = 1; while ( <= 3) System.out.println (); Output: 1 2 3 Increment

The while Statement 3 int = 1; <= 3 is true while ( <= 3) System.out.println (); Output: 1 2

The while Statement int = 1; while ( <= 3) Print System.out.println (); Output: 1 2 3 3

The while Statement int = 1; while ( <= 3) System.out.println (); Output: 1 2 3 4 Increment

The while Statement 4 int = 1; <= 3 is false while ( <= 3) System.out.println (); Output: 1 2 3

The while Statement unraveled int = 1; while ( <= 3) System.out.println(); Output: 1 2 3 int = 1; TEST:( <= 3)è true 1 System.out.println(); TEST:( <= 3)è true System.out.println(); TEST:( <= 3)è true System.out.println(); TEST:( <= 3)è false EXIT LOOP 2 3 4

more = true; while (more) input qp input credits qpa = qp/credits print qpa GPA problem: Algorithm for next solution print "Enter 1 to continue, 0 to quit input answer more = (1 == answer)

int qp; int credits; double gpa; Scanner scan = new Scanner(System.in); boolean more = true; GPA problem: next solution while (more) // get input System.out.print ( \n\nenter Quality Points > "); qp = scan.nextint(); System.out.print ("Enter Credits > "); credits = scan.nextint();...... // other logic goes here... link to GPA06.java System.out.print ("Enter 1 to continue, 0 to quit > "); more = (1 == scan.nextint());

If the condition of a while loop is false initially, the statement is never executed int = 8; while ( <= 3) System.out.println (); Therefore, the body of a while loop will execute zero or more times

Example: Input validation System.out.println( type in a number >5 ); int num = scan.nextint(); while (num <= 5) System.out.println ( Please try again ); System.out.println ( type a number >5 ); num = scan.nextint();

Example: Input validation System.out.println( type in a number >5 ); int num = scan.nextint(); while (num <= 5) System.out.println ( Please try again ); System.out.println ( type a number >5 ); num = scan.nextint(); In this example, the body of the while loop will typically execute zero times

Let s try this with the Wages.java program //******************************************************************** // Wages.java Author: Lewis/Loftus // // Demonstrates the use of an if-else statement. //******************************************************************** import java.text.numberformat; import java.util.scanner; public class Wages //----------------------------------------------------------------- // Reads the number of hours worked and calculates wages. //----------------------------------------------------------------- public static void main (String[] args) final double RATE = 8.25; // regular pay rate final int STANDARD = 40; // standard hours in a work week continue Scanner scan = new Scanner (System.in); double pay = 0.0;

continue System.out.print ("Enter the number of hours worked: "); int hours = scan.nextint(); What if input is negative or zero? System.out.println (); // Pay overtime at "time and a half" if (hours > STANDARD) pay = STANDARD * RATE + (hours-standard) * (RATE * 1.5); else pay = hours * RATE; NumberFormat fmt = NumberFormat.getCurrencyInstance(); System.out.println ("Gross earnings: " + fmt.format(pay));

What if we want to do a calculation over and over again? Example: Keep calculating wages until user quits program (infinite loop). Example: Keep calculating wages and ask each time whether to keep going. Example: Keep calculating wages until user inputs zero for the hours Example: Calcultate wages for 20 employees

Homework Read Section 5.4, up to Nested loops (pp 230-235) Always do all self-review exercises when you review material This time you only need to do SR 5.17-SR 5.20 and SR 5.22. Do end of chapter Exercises EX 5.7 5.11