CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Repetition Statements while and do while loops
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 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.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
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.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
Loop Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
The ‘while’ Statement September 27, 2006
3.5- The while Statement The while statement has the following syntax:
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
What output is produced by the following fragment?
CprE 185: Intro to Problem Solving (using C)
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Repetition Statements
Outline Boolean Expressions The if Statement Comparing Data
Chap 7. Advanced Control Statements in Java
CSCI 1100/1202 February 6, 2002.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost

2 Lecture Outline Loops NullPointerExceptions Lab 2 Overview

3 Repetition Statements Iteration Repetition statements allow us to execute a statement multiple times Repetitive statements are referred to as loops Like conditional statements, they are controlled by boolean expressions Java (like other C-based languages) has three kinds of repetition statements:  the while loop  the do loop  the for loop The programmer should choose the right kind of loop for the situation

4 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 Boolean-valued expression

5 Control flow of a while Loop statement true false condition evaluated

6 The while Statement Like the if statement, a while statement can have a block statement as the statement it conditionally controls Allows the repetition of multiple statements while ( condition ) { // Statements }

7 The while Statement An example of a while statement: int count = 1; while (count <= 5) { System.out.println(count); count++; } What is the output of the while loop above? If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times

8 Infinite Loops The body of a while loop eventually must* make the condition false If not, it is called an infinite loop, which will execute until the user terminates the program*  (* Or some control statement cause the loop to exit) This is most often a logical error You should always double check the logic of a program to ensure that your loops will terminate normally [Example of an infinite loop – Nested if ]

9 Nested Loops Similar to nested if statements, loops can be nested as well That is, the body of a loop can contain another loop For each iteration of the outer loop, the inner loop iterates completely  Print a list of numbers which are the sum of numbers from , , …, [Example on board] while ( condition ) { statements; }

10 Nested Loops How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) { System.out.println("Here"); count2++; } count1++; } 10 * 20 = 200

11 The do Statement A do statement has the following syntax: do { statement; } while ( condition ); The statement is executed once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes false

12 Control flow of a do Loop true condition evaluated statement false

13 The do Statement An example of a do loop: The body of a do loop executes at least once  A do loop executes one or more times int count = 4; do { count++; System.out.println(count); } while (count < 5);

14 Comparing while and do statement true false condition evaluated The while Loop true condition evaluated statement false The do Loop

15 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

16 Logic of a for loop statement true condition evaluated false increment initialization

17 The for Statement A for loop is functionally equivalent to the following while loop structure: Example on the board - Translating for loops to while loops initialization; while ( condition ) { statements; increment; }

18 The for Statement An example of a for loop: for (int i=1; i <= 5; i++) System.out.println(i); The initialization section can be used to declare and/or initialize a variable Like a while loop, the condition of a for loop is tested prior to executing the loop body Therefore, the body of a for loop will execute zero or more times

19 The for Statement Increment The increment section can perform any calculation What’s with the variable name i, anyway? for (int i=100; i > 0; i -= 5) System.out.println(num);

20 The for Statement Each expression in the header of a for loop is optional If the initialization is left out, no initialization occurs 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 occurs for (int count=1; count <= 5; count++) System.out.println (count); int count = 1; for (; count <= 5; count++) System.out.println(count); int count = 1; for (; ; count++) System.out.println (count); int count = 1; for (; ; ) System.out.println(count);

21 Loops When to Use Each? The Important thing is to make the repetitive statement comply to your logic, and not the other way around. while  When loop will execute an arbitrary number of times (0 or more) do / while  When loop will execute an arbitrary number of times, but at least once for  When loop will execute a specific number of times that can be calculated or determined in advance Number of times is a constant, e.g. 100 Number of times is contained in some variable

22 NullPointerExceptions Bad Programmer! No Cookie! NullPointerExceptions occur when a method is invoked or a field is referenced on an object variable that refers to null NullPointerExceptions are Runtime Errors, meaning they cannot be detected at compile-time before the program executes  Occur at runtime Most of the time, Runtime Errors (such as NullPointerExceptions) are due to programmer error BankAccount account; account.getBalance(); account.balance = 0.0; null

23 NullPointerExceptions The Aftermath NullPointerExceptions print a stack trace to the console and usually terminate your program // BankAccount.java void transfer (BankAccount other, double amount) { other.withDraw(amount) this.deposit(amount); } Exception in thread "main" java.lang.NullPointerException at MyBankApp.main(MyBankApp.java:12) // MyBankApp.java BankAccount one; BankAccount two; one.transfer(two, 1000); // MyBankApp.java BankAccount one; BankAccount two; one = new BankAccount(5000); one.transfer(two, 1000); Exception in thread "main" java.lang.NullPointerException at BankAccount.transfer(BankAccount.java:37) at MyBankApp.main(MyBankApp.java:13)

24 NullPointerExceptions Anatomy of a Stack Trace A stack trace tells you where an exception occurred, and context in which it occurred Exception in thread "main" java.lang.NullPointerException at BankAccount.transfer(BankAccount.java:37) at MyBankApp.main(MyBankApp.java:13) The type of exception that occurred The class and method in which the exception occurred The file in which the exception occurred The line number at which the exception occurred The method that called the method where the exception occurred (the call stack)

25 NullPointerExceptions Anatomy of a Stack Trace A stack trace may consist of numerous calling methods: The stack trace above tells us that the main method invoked…  executeATMCommand on line 13 which invoked…  doService on line 83 which invoked…  transfer on a BankAccount object at line 27  Wherein the exception occurred at line 37 of BankAccount.java Exception in thread "main" java.lang.NullPointerException at BankAccount.transfer(BankAccount.java:37) at MyBankApp.doService(MyBankApp.java:27) at MyBankApp.executeATMCommand(MyBankApp.java:83) at MyBankApp.main(MyBankApp.java:13)

26 NullPointerExceptions Some Common Causes Sometimes the problem caused by the direct use of an object variable that has not been initialized public static void main (String[] args) { BankAccount accountOne; BankAccount accountTwo = new BankAccount(10000); accountOne.transfer(accountTwo, 5000); } Exception in thread "main" java.lang.NullPointerException at MyBankApp.main(MyBankApp.java:13)

27 NullPointerExceptions Some Common Causes Sometimes the problem is the fault of the method that called the method where the exception occurs: public void transfer (BankAccount other, double amount) { other.withdraw(amount); this.balance += interest; } Exception in thread "main" java.lang.NullPointerException at BankAccount.transfer(BankAccount.java:37) at MyBankApp.main(MyBankApp.java:13)

28 NullPointerExceptions Some Common Causes Forgetting to initialize an object instance variable in the constructor: public class BankAccount { protected Bank bank protected double interest; public BankAccount (Bank bank, double balance) { } public void awardInterest () { double interest = bank.calculateInterest(this); this.balance += interest; } bank = bank; this.bank = bank; This method invocation results in a NullPointerException because bank is null

29 NullPointerExceptions … And the this Reference public void transferAll (BankAccount other) { this.transfer(other, other.getBalance()); } public void transfer (BankAccount other, double balance) { // … transfer logic … } Exception in thread "main" java.lang.NullPointerException at BankAccount.transferAll(BankAccount.java:37) at MyBankApp.main(MyBankApp.java:13) How do we know this is not the null reference?  The NullPointerException would have been preempted by another NullPointerException…  this can never refer to null.

30 NullPointerExceptions With Multiple Suspects public void transferAll (BankAccount from, BankAccount to) { to.transfer(from, from.getBalance()); } Exception in thread "main" java.lang.NullPointerException at MyBankApp.transferAll(MyBankApp.java:98) at MyBankApp.main(MyBankApp.java:13)

31 Lab 2 SimBee Grid-based Turn-Based Coordinates / Vectors

32 Conclusion Questions? Lab 2 Assigned Now  More difficult than 1 & 1.5 Get Started Early  Due Tuesday, Sept. 29 I will be in Lab now