© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Advertisements

1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Chapter Day 14. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 14 Problem set 3 posted  10 problems from chapters 5 & 6  Due in 11.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
1 Infinite Loops  The body of a while loop eventually must make the condition false  If not, it is an infinite loop, which will execute until the user.
Chapter Day 12. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 12 Problem set corrected  1 A, 2 B’s and 1 D Starting Late and not turning.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Chapter 3: Program Statements
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
© 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.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Chapter 5 Loops.
© 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.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science.
© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
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.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 4 Repetition Statements (loops)
Chapter 3: Program Statements
Chapter 6 More Conditionals and Loops
Loop Structures.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 3: Program Statements
MSIS 655 Advanced Business Applications Programming
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Chapter 3: Program Statements
Chapter 3: Program Statements
The ‘while’ Statement September 27, 2006
Chapter 3: Program Statements
3.5- The while Statement The while statement has the following syntax:
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)
Chapter 3: Program Statements
Outline Boolean Expressions The if Statement Comparing Data
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Chapter 3: Program Statements
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.
Presentation transcript:

© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1

2 Repetition Statements  Repetition statements allow us to execute a statement multiple times  Often they are referred to as loops  Like conditional statements, they are controlled by boolean expressions  The text covers two kinds of repetition statements: the while loop the for loop  The programmer should choose the right kind of loop for the situation

The while Statement  The while statement has the following syntax: while ( condition ) statement; while is a reserved word If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repeatedly until the condition becomes false.

Logic of a while Loop statement true condition evaluated false

The while Statement  Note that if the condition of a while statement is false initially, the statement is never executed  Therefore, the body of a while loop will execute zero or more times  See Counter.java (page 147)Counter.java  See Average.java (page 148)Average.java A sentinel value indicates the end of the input (value != 0) The variable sum maintains a running sum  See WinPercentage.java (page 151)WinPercentage.java A loop is used to validate the input, making the program more robust (won NUM_GAMES)

Infinite Loops  The body of a while loop eventually must make the condition false  If not, it is an infinite loop, which will execute until the user interrupts the program  This is a common logical error  You should always double check to ensure that your loops will terminate normally  See Forever.java (page 152)Forever.java

Nested Loops  Similar to nested if statements, loops can be nested as well  That is, the body of a loop can contain another loop  Each time through the outer loop, the inner loop goes through its full set of iterations  See PalindromeTester.java (page 155)PalindromeTester.java

The for Statement  A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }

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

The for Statement  Like a while loop, the condition of a for statement is tested prior to executing the loop body  Therefore, the body of a for loop will execute zero or more times  It is well suited for executing a loop a specific number of times that can be determined in advance  See Counter2.java (page 161)Counter2.java  See Multiples.java (page 163)Multiples.java  See Stars.java (page 165)Stars.java

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  Two semi-colons are always required in the for loop header

Choosing a Loop Structure  When you can’t determine how many times you want to execute the loop body, use a while statement  If you can determine how many times you want to execute the loop body, use a for statement

Program Development  We now have several additional statements and operators at our disposal  Following proper development steps is important  Suppose you were given some initial requirements: accept a series of test scores compute the average test score determine the highest and lowest test scores display the average, highest, and lowest test scores

14 ASSIGNMENT 2 1.Textbook Problems: MC: 4-8; TF: 7; SA: Pre-Lab Exercises # 3.Programming Projects (text) #3.4, 3.5, 3.12a, 3.12b (1, 8, 13) 4.Programming Exercises # 5.Ch3 Labs:

15 Assignment 3  COMPLETE ALL ASSIGNMENTS IN THE ORDER LISTED  Lab 2 -- Grades  Lab 3 Phase 1 Counting and Looping – Love CS Powers of 2 – Powers A guessing Game – Guess  Chapter 3 Exercises continued numbers  Lab 3 Phase 2 Factorials – Factorial (Ex. Credit) More Guessing – GuessDo  Chapter 3 Exercises continued numbers

16 Assignment 4  COMPLETE ALL ASSIGNEMENTS IN THE ORDER LISTED  Lab 4 Phase 1 Counting Characters – CharCount  Chapter 3 Programming Projects (pp. 187 – 188) 3.1, 3.4, 3.5, 3.7, 3.8, 3.12 a, b, 3.13  Lab 4 Phase 2 Finding Maximum & Minimum Values – MaxMin  Chapter 3 Exercises Continued numbers 23 – 27  Chapter 3 Exercises from textbook pp – 3.17 (Ex. Credit)  Lab 5 A Rainbow Applet – Rainbow (Ex. Credit)

17 Program Development  Requirements Analysis – clarify and flesh out specific requirements How much data will there be? How should data be accepted? Is there a specific output format required?  After conferring with the client, we determine: the program must process an arbitrary number of test scores the program should accept input interactively the average should be presented to two decimal places  The process of requirements analysis may take a long time

18 Program Development  Design – determine a possible general solution Input strategy? (Sentinel value?) Calculations needed?  An initial algorithm might be expressed in pseudocode  Multiple versions of the solution might be needed to refine it  Alternatives to the solution should be carefully considered

19 Program Development  Implementation – translate the design into source code  Make sure to follow coding and style guidelines  Implementation should be integrated with compiling and testing your solution  This process mirrors a more complex development model we'll eventually need to develop more complex software  The result is a final implementation  See ExamGrades.java (page 170)ExamGrades.java

20 Program Development  Testing – attempt to find errors that may exist in your programmed solution  Compare your code to the design and resolve any discrepancies  Determine test cases that will stress the limits and boundaries of your solution  Carefully retest after finding and fixing an error

21 More Drawing Techniques  Conditionals and loops can greatly enhance our ability to control graphics  See Bullseye.java (page 173)Bullseye.java  See Boxes.java (page 175)Boxes.java  See BarHeights.java (page 177)BarHeights.java

22 Summary  Chapter 3 has focused on: program development stages the flow of control through a method decision-making statements expressions for making complex decisions repetition statements drawing with conditionals and loops

Conditional Operator  Conditional operators are a shortcut way to express an if-else situation. Format: result = (condition)? value1 : value2; 23 “if” part “else” part

Conditional Operator 24 Example: (orig if-else) if (x > 0) count = count + 1; else count = count – 1; Example: (orig if-else) if (x > 0) count = count + 1; else count = count – 1; Example: (cond. operator) count =(x > 0)? (count + 1) : (count – 1); Example: (cond. operator) count =(x > 0)? (count + 1) : (count – 1);

The Do-While Loop  The do-while loop works like the while loop, BUT in a different order. The do portion is done first, before the while condition is checked. Format: 25 do { … statements for what to do } while (condition);

Example of Do-While loop int count = 0; int sum = 0; do { sum = sum + count; count ++; } while (count < 10); 26 Notice the while condition is not checked until the do loop is executed at least once.

27 The for Statement  The for statement has the following syntax: for ( initialization ; condition ; increment ) statement; Reserved word 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 The condition-statement-increment cycle is executed repeatedly