CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.

Slides:



Advertisements
Similar presentations
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Advertisements

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
INF 523Q Chapter 3: Program Statements (Examples).
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering
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.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3: Program Statements
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Loops.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Repetition Statements while and do while loops
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
5-1 Chapter 5: Conditionals and Loops Topics: –Boolean expressions –Conditional statements –Increment and Decrement Operators (Chapter 2.4) –Repetition.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4 Repetition Statements (loops)
CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I
Chapter 6 More Conditionals and Loops
SELECTION STATEMENTS (1)
Chapter 6 More Conditionals and Loops
Outline Altering flow of control Boolean expressions
Outline Boolean Expressions The if Statement Comparing Data
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Outline Boolean Expressions The if Statement Comparing Data
Conditionals and Loops
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: 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 CSC 1051 M.A. Papalaskari, Villanova University

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 CSC 1051 M.A. Papalaskari, Villanova University

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 CSC 1051 M.A. Papalaskari, Villanova University

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 CSC 1051 M.A. Papalaskari, Villanova University

Logic of a while Loop statement true false condition evaluated CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Initialize count count 1 count 1 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true count 1 count 1 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 Output: 1 count 1 count 1 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 Output: 1 count 2 count 2 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true count 2 count 2 Output: 1 Output: 1 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 2 Output: 1 2 count 2 count 2 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 2 Output: 1 2 count 3 count 3 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true count 3 count 3 Output: 1 2 Output: 1 2 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: Output: count 3 count 3 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: Output: count 4 count 4 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is false count 4 count 4 Output: Output: CSC 1051 M.A. Papalaskari, Villanova University

The while Statement “unraveled” int count = 1; while (count <= 3) { System.out.println(count); count++; } int count = 1; while (count <= 3) { System.out.println(count); count++; } Output: Output: CSC 1051 M.A. Papalaskari, Villanova University int count = 1; TEST:(count <= 3)  true { System.out.println(count); count++; } TEST:(count <= 3)  true { System.out.println(count); count++; } TEST:(count <= 3)  true { System.out.println(count); count++; } TEST:(count <= 3)  false EXIT LOOP int count = 1; TEST:(count <= 3)  true { System.out.println(count); count++; } TEST:(count <= 3)  true { System.out.println(count); count++; } TEST:(count <= 3)  true { System.out.println(count); count++; } TEST:(count <= 3)  false EXIT LOOP count 4 count 4 count 3 count 3 count 2 count 2 count 1 count 1

GPA problem: Algorithm for next solution more = true; while (more) { input qp input credits qpa = qp/credits print qpa print "Enter 1 to continue, 0 to quit “ input answer more = (1 == answer) } CSC 1051 M.A. Papalaskari, Villanova University

GPA problem: next solution int qp; int credits; double gpa; Scanner scan = new Scanner(System.in); boolean more = true; 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... System.out.print ("Enter 1 to continue, 0 to quit > "); more = (1 == scan.nextInt()); } link to GPA06.javaGPA06.java CSC 1051 M.A. Papalaskari, Villanova University

If the condition of a while loop is false initially, the statement is never executed int count = 8; while (count <= 3) { System.out.println (count); count++; } Therefore, the body of a while loop will execute zero or more times CSC 1051 M.A. Papalaskari, Villanova University

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(); } CSC 1051 M.A. Papalaskari, Villanova University

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 CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // 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 Scanner scan = new Scanner (System.in); double pay = 0.0; continue Let’s try this with the Wages.java programWages.java

CSC 1051 M.A. Papalaskari, Villanova University continue System.out.print ("Enter the number of hours worked: "); int hours = scan.nextInt(); 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 input is negative or zero?

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 CSC 1051 M.A. Papalaskari, Villanova University

Homework Read Section 5.4, up to Nested loops (pp ) –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 Do end of chapter Exercises EX 5.7 – 5.11 CSC 1051 M.A. Papalaskari, Villanova University