mat 2170
play

Mat 2170 Week 4 Chapter Four Part A Review Boolean Control - PowerPoint PPT Presentation

Mat 2170 Chapter Four Part A Control Statements Iteration Mat 2170 Week 4 Chapter Four Part A Review Boolean Control Control Statements Iteration Repeat Patterns while Loops Infinite Loops Spring 2014 for Loops


  1. Mat 2170 Chapter Four – Part A Control Statements – Iteration Mat 2170 Week 4 Chapter Four – Part A Review Boolean Control Control Statements – Iteration Repeat Patterns while Loops Infinite Loops Spring 2014 for Loops Exercises Nested Loops Animation

  2. Week 4 Mat 2170 Student Responsibilities Chapter Four – Part A Reading: Textbook, Chapter 4.1 – 4.2, 4.5 – 4.6 Control Statements – Iteration Lab preparation & lab Week 4 Attendance Review Boolean Chapter Four Overview: 4.1 – 4.4 Control Repeat A little review Patterns while Loops Java statement types Infinite Loops Control statements and problem solving for Loops Exercises The while statement Nested Loops Animation The for statement

  3. Compound Assignment Statements Mat 2170 There are five forms of the compound assignment statement: Chapter Four – Part A +=, -=, *=, /= , and %= Control Statements – Iteration Week 4 Before Assignment After Review Boolean i is 2 int i = 2; Control i is 2 i is 5 i += 3; Repeat Patterns i is 5 i is 4 i -= 1; while Loops i is 4 i is 12 Infinite Loops i *= 3; for Loops i is 12 i is 4 i /= 3; Exercises i is 4 i is 0 i %= 4; Nested Loops Animation

  4. Increment and Decrement Mat 2170 Chapter Four – Part A It is often the case that we wish to add or subtract one from a Control Statements – numeric object. There are many equivalent statements to Iteration accomplish this. Week 4 Review To add one to int object k : Boolean Control k = k + 1; k += 1; k++; ++k; Repeat Patterns while Loops To subtract one from object k : Infinite Loops for Loops k = k - 1; k -= 1; k--; --k; Exercises Nested Loops Animation

  5. The boolean type Mat 2170 Chapter Four One of the built-in primitive data types – Part A Control Has two values only: true and false Statements – Iteration Is useful for loops —the while statement Week 4 Is useful for conditionals —the if statement Review Boolean Examples of Boolean objects: Control boolean doAgain = true; Repeat Patterns boolean bigger = false; while Loops Infinite Loops The type of the parameter passed in the GRect and GOval for Loops message setFilled() : Exercises Nested Loops GRect MyRect = new GRect(x, y, w, h); Animation MyRect.setFilled(true);

  6. Boolean operators — AND Mat 2170 Chapter Four The operator + is used to combine two numeric objects – Part A Control The operator && is used to combine two boolean objects: Statements – Iteration Week 4 P Q P && Q Review true true true Boolean Control true false false Repeat Patterns false true false while Loops false false false Infinite Loops for Loops Exercises Both operands must be true to obtain true. Nested Loops This is similar to good parenting Animation when both parents must say “Yes.”

  7. Boolean operators — OR Mat 2170 Chapter Four The operator || is used to combine two boolean objects: – Part A Control Statements – Iteration P Q P || Q Week 4 true true true Review true false true Boolean Control false true true Repeat Patterns false false false while Loops Infinite Loops for Loops If either operand is true, the result is true. Exercises This is similar to not-so-good parenting , Nested Loops where only one must say “Yes.” Animation

  8. Boolean operators — NOT Mat 2170 Chapter Four – Part A The operator ! is used to negate one boolean object: Control Statements – Iteration Week 4 Review P !P Boolean Control true false Repeat Patterns false true while Loops Infinite Loops for Loops Exercises The ! operator simply “flips” the truth value. Nested Loops Animation

  9. Relational Operators Mat 2170 Chapter Four Mathematics Java – Part A Control < < Statements – Iteration ≤ <= Week 4 > > Review ≥ >= Boolean Control � = != Repeat = == Patterns while Loops Infinite Loops Notes: for Loops The result of a relational operator is a boolean value Exercises Nested Loops Testing for equality requires the == operator. Animation The = operator is used for assignment .

  10. Examples of relational operators Mat 2170 Assume these declarations are in effect: Chapter Four – Part A Control int i = 1; Statements – Iteration int j = 2; int k = 2; Week 4 Review Boolean Boolean Boolean true or true or Control Expression Expression Repeat false? false? Patterns while Loops i < j i < (j + k) Infinite Loops j == k j <= k for Loops j < k i == k Exercises Nested Loops i*i > k*k j != 2 Animation

  11. Java Statement Types Mat 2170 Chapter Four – Part A Java programs consist of a set of classes. Control Statements – Iteration Classes contain methods, and each method consists of a Week 4 sequence of statements. Review Boolean Control There are three basic types of Java statements: Repeat Patterns while Loops 1. Simple Infinite Loops 2. Compound for Loops Exercises 3. Control Nested Loops Animation

  12. Mat 2170 Chapter Four – Part A Simple statements are formed by adding a semicolon (;) to the Control Statements – end of a Java expression Iteration Week 4 Review Compound statements (aka blocks ) consist of a sequence of Boolean statements enclosed in curly braces: { } Control Repeat Patterns Control statements fall into two categories: while Loops Infinite Loops 1. Conditional (selection) statements that make choices for Loops Exercises 2. Iterative (looping) statements that specify repetition Nested Loops Animation

  13. Control Statements and Problem Solving Mat 2170 Chapter Four Before looking at the details of control statements, it may help – Part A to look at common control patterns — when and how they are Control Statements – used. Iteration Week 4 We will extend the Add2Integers program from lab 1 to Review create programs that add longer lists of integers. Boolean Control We will illustrate three different strategies : Repeat Patterns while Loops 1. Add new code to process each (additional) input value Infinite Loops 2. Repeat the input cycle a predetermined number of times ( for for Loops loop) Exercises Nested Loops 3. Repeat the input cycle until a special sentinel value is entered by the user ( while loop) Animation

  14. The Add4Integers Problem At this point, the only way to increase the number of inputs is Mat 2170 Chapter Four to add new statements for each one: – Part A Control Statements – public class Add4Integers extends ConsoleProgram Iteration { Week 4 public void run() { Review println("This program adds four numbers."); Boolean int n1 = readInt("Enter first number: "); Control int n2 = readInt("Enter second number: "); Repeat int n3 = readInt("Enter third number: "); Patterns int n4 = readInt("Enter fourth number: "); while Loops int total = n1 + n2 + n3 + n4; Infinite Loops println("The total is " + total + "."); for Loops } Exercises } Nested Loops Animation This strategy is difficult to generalize and would be cumbersome if we needed to add 100 values!

  15. The Repeat–N–times Pattern Mat 2170 Chapter Four – Part A Control The Repeat–N–times Pattern : Statements – Iteration execute a set of statements a specified number of times. Week 4 Review Boolean The general form of the pattern: Control Repeat for (int i = 0; i < repsDesired; i++) Patterns { while Loops statements to be repeated; Infinite Loops } for Loops Exercises Nested Loops Animation

  16. Terminology Mat 2170 Chapter Four – Part A A control statement that repeats a section of code is called a Control Statements – loop . Iteration Week 4 The statements to be repeated are called the body of the Review loop. Boolean Control Repeat Each execution of the body of a loop is called a cycle , an Patterns iteration , or a pass through the loop. while Loops Infinite Loops for Loops In a for –loop, the number of repetitions is specified in the first Exercises line of the pattern, which is called the loop header . Nested Loops Animation

  17. The AddNIntegers Program This program uses the Repeat–N–Times pattern to Mat 2170 Chapter Four compute the sum of a predetermined number of integer – Part A Control values, specified by the named constant N . Statements – Iteration public class AddNIntegers extends ConsoleProgram { Week 4 public void run() Review { Boolean println("This program adds " + N + " numbers."); Control int total = 0; Repeat for (int i = 0; i < N; i++) Patterns { while Loops int value = readInt("Enter number ["+i+"]: "); Infinite Loops total += value; for Loops } Exercises println("The total is " + total + "."); } Nested Loops private static final int N = 100; Animation }

  18. The Repeat–Until–Sentinel Pattern Mat 2170 The programs on the previous slides haven’t been flexible: you Chapter Four – Part A must add either four integers, or 100 integers. Sometimes we Control Statements – may not know how many integers are on the list to sum. Iteration Week 4 The Repeat–Until–Sentinel Pattern executes a set of Review statements until the user enters a specific value, called a Boolean sentinel , to signal the end of the list: Control Repeat Patterns prompt user and read in a value while Loops while (value != sentinel) Infinite Loops { for Loops process value; Exercises prompt user and read in a value; Nested Loops } Animation

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend