Repetition structures
Download
1 / 34

Repetition Structures - PowerPoint PPT Presentation


  • 106 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Repetition Structures' - aubrey-chapman


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
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 loop1
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 loop2
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 loop3
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 loop4
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 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 loop1
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 loop2
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 loop3
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
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 repetition1
Counter Controlled Repetition

int x = 0;

while (x < 5) {

statement1;

x++;

}


For repetition structure
For Repetition Structure

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


For repetition structure1
For Repetition Structure

  • Structure

    for (int = initialValue; boolean_expression; incrementExpression )

    statement1;

    for (int = initialValue; boolean_expression; incrementExpression ) {

    statement1;

    statementN;

    }


For repetition structure2
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 structure3
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 structure4
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 structure5
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 structure6
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 structure7
For Repetition Structure

  • What happens when the following loop is executed?

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

    outputBox.printLine(‘H’);


For repetition structure8
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 structure9
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
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
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 class1
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 class2
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
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”.


Responsebox1
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”);


Responsebox2
ResponseBox

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


Responsebox3
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”);


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


  • ad