Software Engineering I SENG201
Lecture 8 – Iterations in Java March 8, 2022
Copyright By PowCoder代写 加微信 powcoder
Previous lecture
1. Content of objects
2. Decisions in Java
3. Testing for equality
4. Multi-way selection in Java with switch
1. Iterations with for
2. Iterations with while, do-while
1. Iterations with for
2. Iterations with while, do-while
Iterations with for (loops)
• Repeatedly execute one or more statements
– No need to hardcode repeated execution of statements
– No need to know the number of executions at design time
for – flow Initialization
Condition true
Statement(s) Increment
for(initialization; condition; increment) {
statement(s);
Explanation
• Initialization
– int counter = 0;
– Set counter before looping begins • Condition
– As long as counter < 6
– Iterates while true (may never stop)
– False when bound is reached
• Statement(s)
– System.out.println(“Hello World!”);
– Carry out action (often compound statement) • Increment
– counter++
– Updates counter (local variable in scope of body) – Progress towards bound (6)
for(int counter = 0; counter < 6; counter++) System.out.println(“Hello World!”);
Some examples (practice in your own time)
• Going backwards; counter can be used as parameter
• Or bigger jumps (prints 5, 7, 9, 11)
Fast slide
for(int i = 10; i > 0; i–) System.out.println(i);
int k = 6;
for(int i = (k – 1); i < (2 * k + 1); i += 2)
System.out.println(“The value of i is ” + i + “.”);
• Character sequences
for(char c = ‘a’; c <= ‘z’; c++) System.out.println(“Hello World!”);
for(char c = ‘Z’; c > ‘A’; c-=2) System.out.println(c);
Variations of for (practice in your own time)
Fast slide
int count = 0;
for( ; count < 3; count++) {
System.out.println(count);
System.out.println(count);
for(int count = 0; count < 3; ) {
System.out.println("Hello world");
for(int count = 0; count++ < 3; System.out.println("Hello world")) {
for( ; ; )
; // would not compile without ‘;’ in this line
• Statements within a loop are nested
– Indentation of body and comments improve readability
for(int outer = 1; outer <= 3; outer++) {
for(int inner = 1; inner <= 2; inner++) {
System.out.print("Hip, ");
} // end inner loop
System.out.print("Hooray!");
System.out.println();
} // end outer loop
Hip, Hip, Hooray!
Hip, Hip, Hooray!
Hip, Hip, Hooray!
Terminators
– Stopsloopexecutionimmediately – Controlspassedtonextstatement
• continue
– Skips remainder of loop body
– Evaluates increment and continuation expressions
– Possibly performs further iterations
• Check all numbers between 1 and 42 if divisible by 3
• Stop checking number if checked number is 27
public class Terminator {
public void checkNumber() {
for(int i = 1; i < 42; i++) {
if(i == 27) {
System.out.println("I don’t like 27");
if(! (i % 3 == 0)) continue;
System.out.println(i + " is divisible by 3");
System.out.println("Finished now!");
public static void main(String[] args)
{ Terminator me = new Terminator(); me.checkNumber(); }
3 is divisible by 3 6 is divisible by 3 9 is divisible by 3 12 is divisible by 3 15 is divisible by 3 18 is divisible by 3 21 is divisible by 3 24 is divisible by 3 I don’t like 27 Finished now!
1. Iterations with for
2. Iterations with while, do-while
while – flow
while(condition) {
statement(s);
Statement(s)
double currentBalance = 10; double target = 100000; double interestRate = 5; int years = 0;
while(currentBalance < target) {
// current account balance
// desired account balance
// 5% interest rate
double interest = currentBalance * interestRate / 100; currentBalance = currentBalance + interest;
System.out.println(“It will take you ” + years + “ years to reach ” +
target + “$”);
do-while – flow
statement(s); while(condition)
Statement(s)
Statement(s)
String userInput = “”; double value;
userInput = JOptionPane.showInputDialog(“Enter number > 0: ”); value = Double.parseDouble(userInput);
while(value <= 0);
for vs do-while / while
In some cases, both could be used to do the same thing
int i = start; while(i <= end) {
for(int i = start; i <= end; i++) {
for vs do-while / while • Generally easier to use for
– Emphasizes distinction between loop components (readability)
– Oftenusedwhennumberofloopsisknown
– Often“counter-controlled”
– Counter runs from start to end with constant increment
– Often used when a certain condition needs to be met, or an event occurs
– Often “sentinel-controlled” (i.e., a particular value is expected)
for vs while (practice in your own time)
Fast slide
public void useWhile() {
int i = 10, j = -5; while((i > 0) && (j < 0)) {
System.out.println(i + j);
public void useFor() {
for(int i = 10, j = -5; (i > 0) && (j < 0); i--, j++) {
System.out.println(i + j);
1. Iterations with for
2. Iterations with while, do-while
Cartoon of the day
Key lesson: Loops save time and effort.
Language in cartoon is C
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com