Docsity

Control Statements: Iteration - Computer Science I | MAT 2170, Study notes of Computer Science

Partial preview of the text

Download Control Statements: Iteration - Computer Science I | MAT 2170 and more Study notes Computer Science in PDF only on Docsity!

Mat 2170

Chapter Four – Part A

Control Statements – Iteration

Spring 2009

Student Responsibilities

I (^) Reading: Textbook, Chapter 4.1 – 4.2, 4.5 – 4.

I (^) Lab preparation & lab

I (^) Attendance

Compound Assignment Statements

There are five forms of the compound assignment statement:

+=, -=, *=, /=, and %=

Before Assignment After int i = 2; i is 2 i is 2 i += 3; i is 5 i is 5 i -= 1; i is 4 i is 4 i *= 3; i is 12 i is 12 i /= 3; i is 4 i is 4 i %= 4; i is 0

Increment and Decrement

It is often the case that we wish to add or subtract one from a numeric object.

To add one to object k: k = k + 1; k += 1; k++; ++k;

To subtract one from object k:

k = k - 1; k -= 1; k--; --k;

Boolean operators — AND

I (^) The operator + is used to combine two int objects.

I (^) The operator && is used to combine two boolean objects:

P Q P && Q

true true true true false false false true false false false false

In other words, both operands must be true to obtain true.

Boolean operators — OR

The operator || is used to combine two boolean objects:

P Q P || Q

true true true true false true false true true false false false

In other words, if either operand is true, the result is true.

Relational Operators

Mathematics C++ < < ≤ <=

≥ >= 6 = != = ==

Notes: I (^) The result of a relational operator is a boolean value

I (^) Testing for equality requires the == operator.

I (^) The = operator is used for assignment.

Examples of relational operators

Assume these declarations are in effect:

int i = 1; int j = 2; int k = 2;

Boolean Exp.

true or false?

Boolean Exp.

true or false? i < j i < (j + k) j == k j <= k j < k i == k ii > kk j != 2

I (^) Simple statements are formed by adding a semicolon (;) to the end of a Java expression

I (^) Compound statements (aka blocks) consist of a sequence of statements enclosed in curly braces: { }

I (^) Control statements fall into two categories:

  1. Conditional (selection) statements that make choices
  2. Iterative (looping) statements that specify repetition

Control Statements and Problem Solving

I (^) Before looking at the details of control statements, it may help to look at common control patterns — when and how they are used.

I (^) We will extend the Add2Integers program from lab 1 to create programs that add longer lists of integers.

I (^) We will illustrate three different strategies:

  1. Add new code to process each (additional) input value
  2. Repeat the input cycle a predetermined number of times (for loop)
  3. Repeat the input cycle until a special sentinel value is entered by the user (while loop)

The Repeat–N–times Pattern

I (^) The Repeat–N–times Pattern: execute a set of statements a specified number of times.

I (^) The general form of the pattern: for (int i = 0; i < repsDesired; i++) { statements to be repeated; }

I (^) The number of repetitions is specified in the first line of the pattern, which is called the header line.

I (^) The statements to be repeated are called the body of the for loop.

I (^) A control statement that repeats a section of code is called a loop.

I (^) Each execution of the body of a loop is called a cycle, iteration, or pass through the loop.

The Repeat–Until–Sentinel Pattern

I (^) The Repeat–Until–Sentinel Pattern executes a set of statements until the user enters a specific value, called a sentinel, to signal the end of the list:

prompt user and read in a value while (value != sentinel) { process value; prompt user and read in a value; }

I (^) This approach works for any number of values.

I (^) The sentinel value chosen should not be a possible legitimate data value.

I (^) Define the sentinel as a named constant to make it easy to change.