C OMP 110 L OOPS Instructor: Prasun Dewan 2 P REREQUISITE Conditionals.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
C OMP 110 R ECURSION Instructor: Jason Carter. 2 R ECURSION English Return (Oxford/Webster) procedure repeating itself indefinitely or until condition.
C OMP 401 I NTRODUCTION Instructor: Prasun Dewan.
C OMP 110 A RRAYS Instructor: Jason Carter. 2 O UTLINE for loops Arrays.
C OMP 110 M AIN & C ONSOLE I NPUT Instructor: Jason Carter.
1 Repetition structures Overview while statement for statement do while statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops More loops Off-by-one errors Infinite loops Nested Loops.
Main and Control Flow Programming without ObjectEditor Main Class Control Flow “Real” Programming Linear Branching Looping Programmer-Defined Overloading.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Repetition structures Overview while statement for statement do while statement.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
C OMP 110 M AIN & C ONSOLE I NPUT Instructor: Sasa Junuzovic.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
C OMP 110 L OOPS A DVANCED Instructor: Prasun Dewan.
C OMP 401 C LASS S TATE Instructor: Prasun Dewan.
C OMP 401 B ASICS O F S CANNING Instructor: Prasun Dewan (FB 150,
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
C OMP 110/401 D OCUMENTATION : C OMMENTS Instructor: Prasun Dewan.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
C OMP 401 U SER -I NTERFACE VS. M AIN T HREADS Instructor: Prasun Dewan.
C OMP 401 R EFLECTION AND A CTION O BJECTS Instructor: Prasun Dewan.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Repetition loops Is a condition true? START END OF LOOP EXIT.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Chapter 9 Repetition.
Chapter 5: Control Structures II
Repetition-Counter control Loop
Repetition.
Chapter 5: Control Structures II
TK1114 Computer Programming
Chapter 5 Repetition.
Decision statements. - They can use logic to arrive at desired results
Chapter 8 The Loops By: Mr. Baha Hanene.
Unit 3 - The while Loop - Extending the Vic class - Examples
class PrintOnetoTen { public static void main(String args[]) {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
CIS 110: Introduction to Computer Programming
Nate Brunelle Today: Conditional Decision Statements
Repetition Statements
Statements in C Programming
Building Java Programs
Building Java Programs
Presentation transcript:

C OMP 110 L OOPS Instructor: Prasun Dewan

2 P REREQUISITE Conditionals.

3 L OOPING printHello(2); printHello(3); hello

4 public static void printHellos( int n) { } L OOPS int counter = 0; if (counter < n) { counter = counter + 1; System.out.println (“hello”); }

5 public static void printHellos( int n) { } L OOPS int counter = 0; while (counter < n) { counter = counter + 1; System.out.println (“hello”); }

6 I F VS. W HILE S TATEMENT while ( ) ; if ( ) ;

7 I F S TATEMENT true false

8 W HILE S TATEMENT true false

9 W HILE L OOP true false

10 S ENTINEL -B ASED F OLDING

11 A DDING F IXED N UMBER OF L OANS Loan loan1 = readLoan(); Loan loan2 = readLoan(); Loan loan3 = readLoan(); Loan loan4 = readLoan(); Loan sumLoan = ALoan.add( loan1, ALoan.add(loan2, ALoan.add(loan3, loan4)) ); print(sumLoan);

12 G ENERALIZING TO V ARIABLE N UMBER OF L OANS Loan loan1 = readLoan(); Loan loan2 = readLoan(); Loan loan3 = readLoan(); Loan loan4 = readLoan(); … Loan loanN = readLoan(); Loan sumLoan = ALoan.add(loan1, ALoan.add(loan2, ALoan.add(loan3, ALoan.add(loan4, ……( ALoan.add(loanN-1, loanN)*; print (sumLoan); Variable Number of Statements Variable Number of Subexpressions (function calls) Recursion Loops and Arrays

13 S PACE -E FFICIENT A DDING OF F IXED N UMBER OF L OANS Loan loan1 = readLoan(); Loan loan2 = readLoan(); Loan sumLoan = ALoan.add(loan1, loan2); loan1 = readLoan(); // 3rd loan sumLoan = ALoan.add(sumLoan, loan1); loan1 = readLoan(); // 4th loan sumLoan = ALoan.add(sumLoan, loan1); print (sumLoan);

14 M ORE S PACE -E FFICIENT A DDING OF F IXED N UMBER OF L OANS Loan sumLoan = readLoan(); //first loan Loan nextLoan = readLoan(); //second loan sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // 3rd loan sumLoan = ALoan.add(sumLoan, nextLoan); nextLoan = readLoan(); // 4th loan sumLoan = ALoan.add(sumLoan, nextLoan); print (sumLoan);

15 M ORE S PACE -E FFICIENT A DDING OF V ARIABLE N UMBER OF L OANS Loan sumLoan = readLoan(); //first loan Loan nextLoan = readLoan(); //second loan sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // 3rd loan sumLoan = ALoan.add(sumLoan, nextLoan); nextLoan = readLoan(); // 4th loan sumLoan = ALoan.add(sumLoan, nextLoan); nextLoan = readLoan(); //Nth loan sumLoan = ALoan.add(sumLoan, nextLoan); nextLoan = readLoan(); //sentinel print (sumLoan); N-1 Repetitions

16 W HILE L OOP Loan sumLoan = readLoan(); //first loan Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan); Input Result Program waits forever for second loan Boundary Condition

17 C ORRECT S OLUTION Loan sumLoan = new ALoan(0); //initial value Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan); ALoan.add(new ALoan(0), add(loan1, add (…., loanN) Identity

18 A S INGLE S ENTINEL V ALUE Loan sumLoan = new ALoan(0); //initial value Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan);

19 A S INGLE L OAN Loan sumLoan = new ALoan(0); //initial value Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan);

20 T WO L OANS Loan sumLoan = new ALoan(0); //initial value Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan);

21 M ULTIPLYING N UMBERS (E DIT ) public class ANumberMultiplier { public static void main(String[] args) { int product = 1; int nextInt = Console.readInt(); while (nextInt >= 0) { product = product * nextInt; nextInt = Console.readInt(); } System.out.println(product); }

22 M ULTIPLYING N UMBERS int product = 1; int num = Console.readInt(); while (num >= 0) { product = product*num; num = Console.readInt(); } print (product); 1 * 20 * 2 * 3 Identify

23 C OMPARING T WO S OLUTIONS int product = 1; int num = Console.readInt(); while (num >= 0) { product = product*num; num = Console.readInt(); } print (product); Loan sumLoan = new ALoan(0); //initial value Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan); result nextVal Read first value Read other value Identity Binary folding function !isSentinel(nextVal)

24 G ENERALIZED F OLDING OF A S ENTINEL - T ERMINATED L IST a1a1 a2a2 a3a3 anan a1a1 a1a1 a1a1 f: T, T  T F(x, I)  x

25 G ENERALIZED F OLDING F UNCTION T result = I; T nextValue = getNextValue() while (!isSentinel(nextValue)) { result = f(result, nextValue); nextValue = getNextValue(..); } ≥ 0ALoan.add(), * Loan, intnew ALoan(0), 1

26 C OMPARING T WO S OLUTIONS (C OMMENTS ) int product = 1; //identity int num = Console.readInt(); // read next list value while (num >= 0) { // sentinel checking product = product*num; // binary folding function num = Console.readInt(); // read next value } print (product);// print value Loan sumLoan = new ALoan(0); //identity Loan nextLoan = readLoan(); // read next list value while (nextLoan().getPrincipal() >= 0) {// sentinel checking sumLoan = Aloan.add(nextLoan, sumLoan); // binary folding function nextLoan = readLoan(); // read next list value } print (sumLoan); // print value