340 likes | 466 Views
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. The While Loop.
E N D
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.
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.
The While Loop • Syntax of a while loop: while (boolean_expression) statement1; while(boolean_expression) { statement1; statement2; statement3; }
The While Loop { int num = 0; while (num < 5) num = num + 1; } True num = num + 1; num < 5 False
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.
The While Loop • What happens when we execute the following loops: Assume x = 4; while (x < 10) x += 4; while (x < 10); x += 4;
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.
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.
The Do/While Loop • The structure of a Do/While loop is: do { statement1; … statementN; } while (boolean_expression)
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); } True num = num + 1; num < 5 False True num = num + 1; num < 5 False
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.
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.
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)
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.
Counter Controlled Repetition int x = 0; while (x < 5) { statement1; x++; }
For Repetition Structure • A For repetition structure is essentially a counter-controlled repetition structure BUT it does the work for you!
For Repetition Structure • Structure for (int = initialValue; boolean_expression; incrementExpression ) statement1; for (int = initialValue; boolean_expression; incrementExpression ) { statement1; … statementN; }
For repetition Structure for (int counter = 1; counter <= 10; counter++){ output.printLine(counter); } counter = 1 outputBox.printLine( counter); True counter <= 10 counter++ False
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.
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.
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)
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?
For Repetition Structure • What happens when the following loop is executed? for (j = 1; j < 10; j++); outputBox.printLine(‘H’);
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’); }
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.
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; }
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.
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>)
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>)
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”.
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”);
ResponseBox • To test the selection compare the input to either ResponseBox.YES or ResponseBox.NO.
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”);
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.