Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition - while, for and do “Loops - iterations Chapter Eight.

Similar presentations


Presentation on theme: "Repetition - while, for and do “Loops - iterations Chapter Eight."— Presentation transcript:

1 Repetition - while, for and do “Loops - iterations Chapter Eight

2 While (segment of code) int counter = 0, x =10; while (counter < 8 ){ g.drawString( “*”, x, 20); // x is location on X axis to draw “*” // 20 on Y axis x = x + 10; counter++; }

3 While Flow Chart Rest of pgm F T Body Up above while Test

4 Parallel Lines Program // See web page for entire program import // The usual stuff public class Lines extends Applet { public void paint (Graphics g) { int n =0, x = 20, y = 20; while (n < 5) {

5 // while (n < 5 ) ‘true’ g.drawLine ( x,y, x + 100, y); // How that works - first two parameters = // X Y position on grid of screen, second // pair X and Y where line ends y = y + 10; n++ ; }

6 Another great example of while //” STEPS “ PROGRAM See it on the web site. REASON for while, saves programmer from writing lots (some 20) separate lines of very repetitive code “drawing in each step” See page 121 for code

7 FlowChart of for statement F T Inital Test Body Final

8 FlowChart do - while “Test is below the body F Rest of Pgm Body Test T Always goes through at least once

9 Asterisks program Draws histogram based on location on scrollbar (from 1 to 100) Program RICE “an old story one grain of rice on first square, 2 on second 4 on third etc. on a chess board with 64 squares how many grains (2 to the 64th - 1)

10 Rice (contd) The legend goes that as payment for some good deed our hero asks for the grains of rice. Later (sad story) the ruler beheads hero when ruler figures he will go bankrupt We will not go into the moral of the story just calculate how much rice is involved Over 10,000 grains when 64 squares filled

11 And Or NOT - && || ! // while (totalRice < 10000 && CountOfSquares < 64 ) … // program goes on


Download ppt "Repetition - while, for and do “Loops - iterations Chapter Eight."

Similar presentations


Ads by Google