






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







































Control Statements – Iteration
Spring 2009
I (^) Reading: Textbook, Chapter 4.1 – 4.2, 4.5 – 4.
I (^) Lab preparation & lab
I (^) Attendance
There are five forms of the compound assignment statement:
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
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;
I (^) The operator + is used to combine two int objects.
I (^) The operator && is used to combine two boolean objects:
true true true true false false false true false false false false
In other words, both operands must be true to obtain true.
The operator || is used to combine two boolean objects:
true true true true false true false true true false false false
In other words, if either operand is true, the result is true.
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.
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:
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:
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.
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.