CS 18000 Lecture Notes - Lecture 8: Foreach Loop, Array Data Structure, Sequential Access

34 views5 pages
Repetition
Repetition Concept:
Repetition broken into 2 parts
Body of code that gets repeatedly executed
Condition (boolean) to determine when to stop
Boolean codes: Determines whether statement is true or false
State of the computation must change with each iteration
Forms of iteration
Indefinite: Loop until “done”; no advance knowledge of how many iterations required
Definite: Loop of a given number of times; used when iterations are controlled by
counter/size/limit
Java Repetition Constructs
While loop
Check a boolean condition
If true, execute block of statements
Repeat
Do-while loop
Execute a block of statements
If a boolean condition is true, repeat
while (x <= y) {
if (x == y);
______;
Else
x = y * z;
______ = Continue
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
2/2/2017
Compound Assignment
x = x + y;
A = a-b;
S = s+”\n”; // s is a string
Increment/Decrement Operators
Increment/Decrement Operator: Adds or subtracts from their operands (mathematical
expression)
A refinement for an even more common case:
x = x + 1;
a = a-1;
Java provides even more keystroke savings:
x++;
A--;
Also:
++x;
--a;
Post and Pre-Increment/Decrement
X++ increment x by one, but the expression value is the original x
int x = 0;
Print 0, then 1
++x increment x by one, and its value is the new x
Int x = 0;
System.out.println(++x); // Print 1
System.out.println(x); // Prints 1
Definite Iteration: for loop
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Boolean codes: determines whether statement is true or false. State of the computation must change with each iteration. Indefinite: loop until done ; no advance knowledge of how many iterations required. Definite: loop of a given number of times; used when iterations are controlled by counter/size/limit. If a boolean condition is true, repeat while (x <= y) { if (x == y); S = s+ \n ; // s is a string. Increment/decrement operator: adds or subtracts from their operands (mathematical expression) A refinement for an even more common case: x = x + 1; a = a-1; X++ increment x by one, but the expression value is the original x int x = 0; ++x increment x by one, and its value is the new x. To loop n times, go from 0 to n-1. } while (boolean-expression): execute statements in body, test boolean-expression. Inner loop is run completely for each iteration of outer loop.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents