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.
INF 523Q Chapter 3: Program Statements (Examples).
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
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.
© 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:
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
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:
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:
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.
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.
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
Repetition-Sentinel,Flag Loop/Do_While
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
Control Statements Loops.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
What output is produced by the following fragment?
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Control Statements Loops.
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
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 Repetition Statements

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

The while and do loops are similar. statement true false condition evaluated while Loop true condition evaluated statement false do Loop 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

An example of a do loop: The body of a do loop executes at least once int count = 0; do { count++; System.out.println (count); } while (count < 5); CSC 1051 M.A. Papalaskari, Villanova University

The do Statement 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 is executed repeatedly until the condition becomes false CSC 1051 M.A. Papalaskari, Villanova University

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

Try this: Write a do loop to keep repeating a computation while a user has more data. CSC 1051 M.A. Papalaskari, Villanova University

Another example: GradeReportLoopy.java //******************************************************************** // GradeReportLoopy.java Author: Papalaskari //******************************************************************** import java.util.Scanner; public class GradeReportLoopy { // // Reads grades from the user and prints comments accordingly. // public static void main (String[] args) { int grade; String ans; Scanner scan = new Scanner (System.in); do { System.out.print ("Enter a numeric grade (0 to 100): "); grade = scan.nextInt(); printReport(grade); System.out.print ("Would you like to do another grade report? "); ans = scan.next(); } while (ans.charAt(0) == 'y' || ans.charAt(0) == 'Y') ; } continueGradeReportLoopy.java CSC 1051 M.A. Papalaskari, Villanova University

public static void printReport (int grade) { int category = grade / 10; System.out.print ("That grade is "); switch (category) { case 10: System.out.println ("a perfect score. Well done."); break; case 9: System.out.println ("great!"); case 8: System.out.println ("well above average. Excellent."); break; case 7: System.out.println ("above average. Nice job."); break; case 6 System.out.println ("below average."); break; default: System.out.println ("not passing."); } } } CSC 1051 M.A. Papalaskari, Villanova University

Another example of a do loop: //******************************************************************** // GradeReportLoopy.java Author: Papalaskari //******************************************************************** import java.util.Scanner; public class GradeReportLoopy { // // Reads a grades from the user and prints comments accordingly. // public static void main (String[] args) { int grade; String ans; Scanner scan = new Scanner (System.in); do { System.out.print ("Enter a numeric grade (0 to 100): "); grade = scan.nextInt(); printReport(grade); System.out.print ("Would you like to do another grade report? "); ans = scan.next(); } while (ans.charAt(0) == 'y' || ans.charAt(0) == 'Y') ; } GradeReportLoopy.java CSC 1051 M.A. Papalaskari, Villanova University Output Enter a numeric grade (0 to 100): 84 That grade is well above average. Excellent. Would you like to do another grade report? yes Enter a numeric grade (0 to 100): 99 That grade is great! well above average. Excellent. Would you like to do another grade report? Y Enter a numeric grade (0 to 100): 100 That grade is a perfect score. Well done. Would you like to do another grade report? no

Try this: This code inputs a single grade: System.out.print ("Enter a numeric grade (0 to 100):"); grade = scan.nextInt(); Use a do loop to do input verification – i.e., ensuring that the grade typed in is in the range (ask them to keep trying again and again until they input a number in the range) CSC 1051 M.A. Papalaskari, Villanova University

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

The for Statement - Example A while loop: int count=1; while (count <= 5) { System.out.println (count); count++; } Equivalent for loop: for (int count=1; count <= 5; count++) System.out.println (count); CSC 1051 M.A. Papalaskari, Villanova University

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=1; 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

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

Character data reminder Character data is stored as numeric codes. In Java, a char is stored as a 16-bit number – same as a short integer. We can convert back and forth between integers and characters using casts. Note that going between char and short is a narrowing conversion in both directions. (Figure 2.6 page 84 in textbook – Java narrowing conversions) CSC 1051 M.A. Papalaskari, Villanova University

Try this: Write a loop to print out the characters corresponding to the ASCII codes CSC 1051 M.A. Papalaskari, Villanova University

Try this: Write a loop to print out the numbers (ASCII codes) corresponding to the characters ‘A’ through ‘Z’ CSC 1051 M.A. Papalaskari, Villanova University

Try this: Write a for loop to print n stars, where n is an integer parameter 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 See Stars.javaStars.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(); }

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 * ** *** **** ***** ****** ******* ******** ********* **********

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

Which loop to use???? We need to write a program that finds the largest of some numbers input through the keyboard (or some other such calculation involving repeatedly getting input). Which type of loop should we use if… a) We ask the user whether to continue, each time through the loop b) We know in advance how many numbers there will be? c) We don’t know in advance how many, but don’t want to ask each time through the loop either. CSC 1051 M.A. Papalaskari, Villanova University

Homework Review Sections 6.3, 6.4, and 6.5 Always do all self-review exercises when you review Exercises EX (important to get lots of practice with writing loops!) Look at some additional examples of programs from the text: ReverseNumber.java Multiples.java CSC 1051 M.A. Papalaskari, Villanova University Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus