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/

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
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.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Copyright © 2014 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design 8 th Edition John.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
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 – 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:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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:
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
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:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 More about Flow of Control. Topics Debugging Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement.
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.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
COS 312 DAY 5 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 2 Posted – Due Feb 22 prior to class – Any issues? Assignment 3 will be posted soon Capstones.
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:
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I
Chapter 6 More Conditionals and Loops
More Conditionals and Loops
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Chapter 6 More Conditionals and Loops
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Chapter 6 More Conditionals and Loops
Chapter 6 More Conditionals and Loops
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Outline Boolean Expressions The if Statement Comparing Data
CSCI 1100/1202 February 6, 2002.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

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 CSC 1051 M.A. Papalaskari, Villanova University Loops revisited: do & for loops

Repetition structures in Java while loop: int count = 0; while (count < 5) { System.out.println (count); count++; } Other repetition structures (Chapter 6 in text) –the do loop –the for loop CSC 1051 M.A. Papalaskari, Villanova University

The do Statement in Java A do statement has the following syntax: do { statement-list; } while (condition); //end do The statement-list is executed once initially, and then the condition is evaluated The statement-list is executed repeatedly until the condition becomes false CSC 1051 M.A. Papalaskari, Villanova University

The while and do loops are similar. int count = 0; while (count < 5) { System.out.println (count); count++; } int count = 0; do { System.out.println (count); count++; } while (count < 5);

Similar – but not the same: statement true false condition evaluated while Loop true condition evaluated statement false do Loop int count = 0; while (count < 5) { System.out.println (count); count++; } int count = 0; do { System.out.println (count); count++; } while (count < 5); CSC 1051 M.A. Papalaskari, Villanova University The body of a do loop executes at least once

Try this: Write a do loop to print the even numbers from 2 to 100. CSC 1051 M.A. Papalaskari, Villanova University

For some things the do loop is more appropriate: System.out.println(“input a number >5”); int num = scan.nextInt(); while (num <= 5) { System.out.println (“type a number >5”); num = scan.nextInt(); } do { System.out.println (“type a number >5”); num = scan.nextInt(); } while (num <= 5) input validation

CSC 1051 M.A. Papalaskari, Villanova University boolean more = true; while (more) { System.out.print(“Enter Quality Points "); qp = scan.nextInt(); System.out.print ("Enter Credits "); credits = scan.nextInt(); gpa = (double) qp /credits; System.out.println("GPA = " + gpa); System.out.print ("Again? 1=yes, 0=no "); more = (1 == scan.nextInt()); } System.out.println("Thank you. Goodbye. "); do { System.out.print(“Enter Quality Points "); qp = scan.nextInt(); System.out.print ("Enter Credits "); credits = scan.nextInt(); gpa = (double) qp /credits; System.out.println("GPA = " + gpa); System.out.print ("Again? 1=yes, 0=no "); } while (1 == scan.nextInt()) System.out.println("Thank you. Goodbye. "); repeating a computation For some things the do loop is more appropriate:

Copyright © 2012 Pearson Education, Inc. //******************************************************************** // ReverseNumber.java Author: Lewis/LoftusReverseNumber.java // Demonstrates the use of a do loop. //******************************************************************** import java.util.Scanner; public class ReverseNumber { // // Reverses the digits of an integer mathematically. // public static void main (String[] args) { int number, lastDigit, reverse = 0; Scanner scan = new Scanner (System.in); System.out.print ("Enter a positive integer: "); number = scan.nextInt(); do { lastDigit = number % 10; reverse = (reverse * 10) + lastDigit; number = number / 10; } while (number > 0); System.out.println ("That number reversed is " + reverse); } Sample Run Enter a positive integer: 2896 That number reversed is 6982 Another example: ReverseNumber.java ReverseNumber.java

Copyright © 2012 Pearson Education, Inc. /******************************************************************** // EvenOdd.java Author: Lewis/Loftus // Demonstrates the use of the JOptionPane class. //******************************************************************** import javax.swing.JOptionPane; public class EvenOdd { // // Determines if the value input by the user is even or odd. // Uses multiple dialog boxes for user interaction. // public static void main (String[] args) { String numStr, result; int num, again; do { numStr = JOptionPane.showInputDialog ("Enter an integer: "); num = Integer.parseInt(numStr); result = "That number is " + ((num%2 == 0) ? "even" : "odd"); JOptionPane.showMessageDialog (null, result); again = JOptionPane.showConfirmDialog (null, "Do Another?"); } while (again == JOptionPane.YES_OPTION); } Another example: EvenOdd.javaEvenOdd.java The JOptionPane class provides methods that for creating dialog boxes

Copyright © 2012 Pearson Education, Inc. /******************************************************************** // EvenOdd.java Author: Lewis/Loftus // Demonstrates the use of the JOptionPane class. //******************************************************************** import javax.swing.JOptionPane; public class EvenOdd { // // Determines if the value input by the user is even or odd. // Uses multiple dialog boxes for user interaction. // public static void main (String[] args) { String numStr, result; int num, again; do { numStr = JOptionPane.showInputDialog ("Enter an integer: "); num = Integer.parseInt(numStr); result = "That number is " + ((num%2 == 0) ? "even" : "odd"); JOptionPane.showMessageDialog (null, result); again = JOptionPane.showConfirmDialog (null, "Do Another?"); } while (again == JOptionPane.YES_OPTION); }

for : a loop with built in “counter” statement true condition evaluated false increment initialization CSC 1051 M.A. Papalaskari, Villanova University

for : a loop with built in “counter” statement true condition evaluated false increment initialization CSC 1051 M.A. Papalaskari, Villanova University int count = 0; while (count < 5) { System.out.println (count); count++; } Example

for : a loop with built in “counter” statement true condition evaluated false increment initialization CSC 1051 M.A. Papalaskari, Villanova University int count = 0; while (count < 5) { System.out.println (count); count++; } for (int count = 0; count < 5; count++) System.out.println (count); Example

The for Statement A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; } CSC 1051 M.A. Papalaskari, Villanova University for ( initialization ; condition ; increment ) statement;

The for Statement A for statement has the following syntax: for ( initialization ; condition ; increment ) statement; The initialization is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration CSC 1051 M.A. Papalaskari, Villanova University

The for Statement A for statement has the following syntax: for (int count = 0; count < 5; count++) System.out.println (count); The initialization is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration CSC 1051 M.A. Papalaskari, Villanova University

The for Statement The increment section can perform any calculation: for (int num=100; num > 0; num -= 5) System.out.println (num); A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance CSC 1051 M.A. Papalaskari, Villanova University

Try this: Write a for loop to print the even numbers from 2 to 100. CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // ReverseNumberAsString.java Author: MAP // Demonstrates the use of a for loop. //******************************************************************** import java.util.Scanner; public class ReverseNumberAsString { // // Reverses the digits of an integer viewed as a String. // public static void main (String[] args) { int number; String reverse = ""; Scanner scan = new Scanner (System.in); System.out.print ("Enter a positive integer: "); number = scan.nextInt(); String original = Integer.toString(number); for (int i=0; i<original.length(); i++) reverse = original.charAt(i) + reverse; System.out.println ("That number reversed is " + reverse); } } Sample Run Enter a positive integer: 2896 That number reversed is 6982 Example: ReverseNumberAsString.java ReverseNumberAsString.java

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // Stars.java Author: Lewis/Loftus // // Demonstrates the use of nested for loops. //******************************************************************** public class Stars { // // Prints a triangle shape using asterisk (star) characters. // public static void main (String[] args) { final int MAX_ROWS = 10; for (int row = 1; row <= MAX_ROWS; row++) { for (int star = 1; star <= row; star++) System.out.print ("*"); System.out.println(); } Output * ** *** **** ***** ****** ******* ******** ********* ********** Example: Stars.java Stars.java Exercise: can you make it print the row number in (1, 2, 3… ) at the beginning of each line?

The for Statement Each expression in the header of a for loop is optional If the initialization is left out, no initialization is performed If the condition is left out, it is always considered to be true, and therefore creates an infinite loop If the increment is left out, no increment operation is performed CSC 1051 M.A. Papalaskari, Villanova University