1 / 34

Repetition Structures

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.

Download Presentation

Repetition Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.

  3. The While Loop • Syntax of a while loop: while (boolean_expression) statement1; while(boolean_expression) { statement1; statement2; statement3; }

  4. The While Loop { int num = 0; while (num < 5) num = num + 1; } True num = num + 1; num < 5 False

  5. 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.

  6. The While Loop • What happens when we execute the following loops: Assume x = 4; while (x < 10) x += 4; while (x < 10); x += 4;

  7. 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.

  8. 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.

  9. The Do/While Loop • The structure of a Do/While loop is: do { statement1; … statementN; } while (boolean_expression)

  10. 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

  11. 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.

  12. 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.

  13. 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)

  14. 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.

  15. Counter Controlled Repetition int x = 0; while (x < 5) { statement1; x++; }

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

  17. For Repetition Structure • Structure for (int = initialValue; boolean_expression; incrementExpression ) statement1; for (int = initialValue; boolean_expression; incrementExpression ) { statement1; … statementN; }

  18. For repetition Structure for (int counter = 1; counter <= 10; counter++){ output.printLine(counter); } counter = 1 outputBox.printLine( counter); True counter <= 10 counter++ False

  19. 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.

  20. 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.

  21. 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)

  22. 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?

  23. For Repetition Structure • What happens when the following loop is executed? for (j = 1; j < 10; j++); outputBox.printLine(‘H’);

  24. 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’); }

  25. 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.

  26. 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; }

  27. 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.

  28. 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>)

  29. 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>)

  30. 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”.

  31. 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”);

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

  33. 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”);

  34. 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.

More Related