34
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. – Also called Loops Counter controlled loops are executed a specific number of times. Conditional loops are executed an indefinite number of times.

Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

Embed Size (px)

Citation preview

Page 1: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

Repetition Structures

• Repetition Structures allow you to write programs that will repeat program steps multiple times.– Also called Loops– Counter controlled loops are executed a

specific number of times.– Conditional loops are executed an

indefinite number of times.

Page 2: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The While Loop

• The while loop is a conditional loop. It is executed an indefinite number of times.– A while loop terminates based upon a

boolean expression becoming false.• As long as the expression is true, the loop will

be executed.

Page 3: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The While Loop

• Syntax of a while loop:while (boolean_expression)

statement1;

while(boolean_expression) {

statement1;

statement2;

statement3;

}

Page 4: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The While Loop

{

int num = 0;

while (num < 5)

num = num + 1;

}

num = num + 1;

num < 5

False

True

Page 5: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The While Loop

• If the boolean_expression is false when the while loop is encountered, the statements inside the loop are never executed.

• If the boolean_expression never becomes false, then the statements inside the loop are executed “forever” in an infinite loop.

Page 6: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The While Loop

• What happens when we execute the following loops: Assume x = 4;

while (x < 10)

x += 4;

while (x < 10);

x += 4;

Page 7: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The While Loop

• Write a program to print the squares of the even numbers between 0 and 10. Print the results out in a table.

Page 8: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The Do/While Loop• The do/while repetition structure is very

similar to the while repetition structure. The difference between the two is where the conditional test is performed.– While statement: the conditional test is performed

before the body of the loop is executed.– Do/while statement: the conditional test is performed

after the body of the loop is executed. This means the body of the loop is executed at least once.

Page 9: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The Do/While Loop

• The structure of a Do/While loop is:

do {

statement1;

statementN;

} while (boolean_expression)

Page 10: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The Do/While Loop

{ // While loop

int num = 0;

while (num < 5)

num = num + 1;

}

------------------------------------------------

{ // Do/While loop

int num = 0;

do

num = num + 1;

while (num < 5);

}

num = num + 1;

num < 5

False

True

num = num + 1; num < 5

False

True

Page 11: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The Do/While Loop

• If the boolean_expression is false when the while section of the loop is encountered, the statements inside the loop are only executed once.

• If the boolean_expression never becomes false, then the statements inside the loop are executed “forever” in an infinite loop.

Page 12: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The Do/While Loop

• Let’s rewrite the previous program to repeatedly ask the user to enter a lower and upper bound for which to calculate the squares of the even numbers. This time we will use a Do/While Loop.

Page 13: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

The Do/While Loop

• What is printed when the following code is executed:

int x = 1, y = 1;

do {

while ( y < x ) {

outputBox.print(“*”);

y++;

} // end of while loop

outputBox.printLine(‘\n’);

} while (x <= 5)

Page 14: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

Counter-Controlled Repetition

• Requires– A named variable that acts as the storage

location for the number of times the loop is executed.

– An initial value for the named variable.– An increment or decrement statement that is

applied to the named variable.– A conditional test that looks for the final

value of the named variable.

Page 15: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

Counter Controlled Repetition

int x = 0;

while (x < 5) {

statement1;

x++;

}

Page 16: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For Repetition Structure

• A For repetition structure is essentially a counter-controlled repetition structure BUT it does the work for you!

Page 17: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For Repetition Structure• Structure

for (int = initialValue; boolean_expression; incrementExpression )

statement1;

for (int = initialValue; boolean_expression; incrementExpression ) {

statement1;

statementN;

}

Page 18: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For repetition Structure

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

output.printLine(counter);

}

outputBox.printLine(counter);counter <= 10

False

True

counter = 1

counter++

Page 19: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For Repetition Structure

• Write a program to print the squares of the even numbers between 0 and 10. Print the results out in a table.

Page 20: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For Repetition Structure

• Rewrite the previous program to repeatedly ask the user to enter a lower and upper bound for which to calculate the squares of the even numbers.

Page 21: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For Repetition Structure

• Examples of for structures:– for ( ; I <= 100; I++) (assume int I = 0;

precedes this statement)

– for (int I = j; I > m*n; I += 6) (where j = 0, m = 3, and n = 5)

– for (int I = 100; I >= 1; I--)– for (int I = 7; I <= 77; I += 7)

Page 22: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For Repetition Structure

• For loop:for (expression1; expression2; expression3)

statement;

– What happens if expression2 is omitted?– What happens if expression1 is completed

before the for structure?– What happens if expression3 is completed

in the body of the for loop?

Page 23: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For Repetition Structure

• What happens when the following loop is executed?

for (j = 1; j < 10; j++);

outputBox.printLine(‘H’);

Page 24: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For repetition Structure• What does the following Loop print out?

for (i = 1; i <= 5; i++){

for (j = 1; j <=3; j++){

for(k = 1; k <= 4; k++)

outputBox.printLine(“*“);

outputBox.printLine(‘\n’);

}

outputBox.printLine(‘\n’);

}

Page 25: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For Repetition Structure

• Rules for using For Loops– You should indent the body of the for loop,

the statements.– You should not modify the index/counter

variable inside the for loop.– Never use floats or doubles for the index

counter in a for loop.– Make sure you use a ‘;’ and not a ‘,’ to

separate the control statements.

Page 26: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

For loop vs. While loop• In most cases a For loop can be represented

as a While loop.• For Loop:

for (expression1; expression2; expression3)

statement

• While Loop:expression1;

while (expression2) {

statement;

expression3;

}

Page 27: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

Format Class

• The Format class can be used to format data for output. – The Format class needs to know how

many characters are to be printed and what value is to be printed.

Page 28: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

Format Class

• The basic statements for formatting integer output are:– Format.leftAlign(<field width>, <int expression>)– Format.rightAlign(<field width>, <int expression>)– Format.centerAlign(<field width>, <int

expression>)

Page 29: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

Format Class

• The basic statements for real numbers are:– Format.leftAlign(<field width>, <decimal

places>, <real expression>)– Format.rightAlign(<field width>, <decimal

places>, <real expression>)– Format.centerAlign(<field width>, <decimal

places>, <real expression>)

Page 30: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

ResponseBox

• The default ResponseBox class creates a dialog box that contains text and two buttons.– The text usually includes a question.– The buttons are labeled “Yes” and “No”.

Page 31: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

ResponseBox

– The prompt method is used to display the dialog box and get the user’s response.

MainWindow mainWindow = new MainWindow();

ResponseBox yesNoBox = new ResponseBox(mainWindow);

int selection = yesNoBox.prompt(“Today is Monday”);

Page 32: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

ResponseBox

– To test the selection compare the input to either ResponseBox.YES or ResponseBox.NO.

Page 33: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

ResponseBox

• A ResponseBox can have up to three buttons.MainWindow mainWindow = new MainWindow();

ResponseBox newBox = new ResponseBox(mainWindow);

newBox.setLabel(ResponseBox.BUTTON1, “One”);

newBox.setLabel(ResponseBox.BUTTON2, “Two”);

newBox.setLabel(ResponseBox.BUTTON3, “Three”);

Page 34: Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled

ResponseBox

– The test comparison for the button selected with a three button response box is:

• selection == ResponseBox.BUTTON1• selection == ResponseBox.BUTTON2• selection == ResponseBox.BUTTON3

• See page 307 for more details.