do-while, for loops, nested z loops aftercs115mb/slides/cs115week12.pdf · cs115 week 12 do-while,...

2
1 CS115 Week 12 Do-While, For Loops, Nested Loops 2 Do Statement z Is a looping control structure in which the loop condition is tested after executing the body of the loop DoStatement do Statement while(Expression); Loop body can be a single statement or a block 3 Do vs. While z POSTTEST loop(exit- condition) z The loop condition is tested after executing the loop body z Loop body is always executed at least once z PRETEST loop(entry- condition) z The loop condition is tested before executing the loop body z Loop body may not be executed at all 4 “for” statement - Designed for Count- Controlled Loops SYNTAX for(initialization; test expression; update) { 0 or more statements to repeat } 5 // Calculating compound interest double amount; double principal = 1000.0; double rate = 0.07; System.out.println(“Year Amount”); for(int year = 1; year <= 10; year++) { amount = principal * Math.pow(1.0 + rate, year); System.out.println(year + “ ” + amount); } A Count-Controlled For Loop 6 Nested loops z Some problems require iteration structures to be nested z Examples: Process data in rows and columns of a table, outer loop is for one row at a time, inner loop is for each column in the current row Outer loop is to allow the user to “play again” or “re-enter data”, inner loop is to do some iterative processing for each outer

Upload: buinguyet

Post on 10-Mar-2018

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Do-While, For Loops, Nested z Loops aftercs115mb/slides/CS115Week12.pdf · CS115 Week 12 Do-While, For Loops, Nested Loops 2 Do Statement zIs a looping control structure in which

1

CS115 Week 12

Do-While, For Loops, Nested Loops

2

Do Statement

Is a looping control structure in which the loop condition is tested after executing the body of the loop

DoStatement

do

Statement

while(Expression);

Loop body can be a single statement or a block

3

Do vs. While

POSTTEST loop(exit-condition)The loop condition is tested after executing the loop bodyLoop body is always executed at least once

PRETEST loop(entry-condition)The loop condition is tested before executing the loop bodyLoop body may not be executed at all

4

“for” statement - Designed for Count-Controlled Loops

SYNTAX

for(initialization; test expression; update){

0 or more statements to repeat}

5

// Calculating compound interest

double amount;

double principal = 1000.0;

double rate = 0.07;

System.out.println(“Year Amount”);

for(int year = 1; year <= 10; year++) {

amount = principal * Math.pow(1.0 + rate, year);

System.out.println(year + “ ” + amount);}

A Count-Controlled For Loop

6

Nested loopsSome problems require iteration structures to be nestedExamples:

Process data in rows and columns of a table, outer loop is for one row at a time, inner loop is for each column in the current rowOuter loop is to allow the user to “play again” or “re-enter data”, inner loop is to do some iterative processing for each outer

Page 2: Do-While, For Loops, Nested z Loops aftercs115mb/slides/CS115Week12.pdf · CS115 Week 12 Do-While, For Loops, Nested Loops 2 Do Statement zIs a looping control structure in which

initialize outer loopwhile ( outer loop condition ){ . . .

initialize inner loopwhile ( inner loop condition ){

inner loop processing and update}. . .

}

Pattern of a nested loop