10
Repetition Control Structure

Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

Embed Size (px)

Citation preview

Page 1: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

Repetition Control Structure

Page 2: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

Introduction

• Many applications require certain operations to be carried out more than once. Such situations require repetition in control flow.

• We will examine “while” statement in Java and learn how to implement repetition using this statement.

• More specifically, we will learn the “syntax” or structure and “semantics” or meaning, and the usage of this statement.

Page 3: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

Topics for discussion

• Repetition structure (loop) design• while loop : syntax, semantics, example• Loop control • Summary

Page 4: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

while loop : syntax

while ( condition ) { statement }• “condition” is a logical expression that

evaluates to true or false. It could be a relational or boolean expression.

• “statement” could a single statement or more than one statement bounded by { }.

Page 5: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

while loop : semantics

1) The condition is evaluated. 2) If it is true, then the body of the loop is executed.

Then the control is transferred back to the condition for re-evaluation.

3) If the logical expression is false, the while loop is exited and control is transferred to the statement after the while statement.

Page 6: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

The while statement

while (condition) {

statement block }

Condition?

Statements

yes

no

Page 7: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

Example 1

Problem: Do something 12 times.int count = 0; // to keep count of how many are donewhile (count <12) // 0,1,2,3,4,5,6,7,8,9,10,11{ // do something // update the counter count = count + 1; // increment the counter by 1}

Page 8: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

Example in Greenfoot

int count = 0; // initialize the countGreenfootImage bg = getWorld().getBackground(); bg.setColor(Color.WHITE); // set the color

while (count < 6) // 0, 1, 2, 3, ,4 ,5{ bg.drawString(“ “ + count, 100*count, 320);}

Page 9: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

Loop Design• While Loop design should consider:

– Initialization of conditions (count = 0)– Termination of the loop (when count >= 6)– Testing (at the top or bottom of the loop)

• (at the top , count >= 6)– Updating conditions

• ( count = count + 1)– Of course, the body of the loop.

• ( output the count on the scenario)• Body of the loop: Statements representing the process to be

repeated. These are statements within the scope of a loop.

Page 10: Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition

10 concepts1. While loop for repeating operations2. While initialization3. While condition4. While body5. While condition updated within the loop6. While termination7. Writing out to Greenfoot background8. Setting the color for background (for future output)9. Location of output (x and y)10. Algebraic expression (count*100)