Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition."— Presentation transcript:

1 Repetition Control Structure

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

3 Topics for discussion Repetition structure (loop) design while loop : syntax, semantics, example Loop control Summary

4 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 { }.

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

6 The while statement while (condition) { statement block } Condition? Statements yes no

7 Example 1 Problem: Do something 12 times. int count = 0; // to keep count of how many are done while (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 }

8 Example in Greenfoot int count = 0; // initialize the count GreenfootImage 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); }

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

10 10 concepts 1.While loop for repeating operations 2.While initialization 3.While condition 4.While body 5.While condition updated within the loop 6.While termination 7.Writing out to Greenfoot background 8.Setting the color for background (for future output) 9.Location of output (x and y) 10.Algebraic expression (count*100)


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

Similar presentations


Ads by Google