Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 

Similar presentations


Presentation on theme: "Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop "— Presentation transcript:

1 Loops Tonga Institute of Higher Education

2 Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop  While Loop  Do-While Loop

3 Loops Parts of a loop  Action  End Condition Example: Scanning items at checkout counter in grocery store.  Action: Get a price for the item and add it to the bill.  End Condition: All items have a price and have been added to the bill.

4 For Loop Executes a definite number of times. Condition is evaluated before the body of the loop is executed. Incrementation of counter takes place after the Body of Loop is executed. For (initialization; continuation condition; increment) Body of Loop For (start at this value; keep looping as long as this expression is true; increment the value) Body of Loop Semicolons Can initialize variable here Body of Loop is 1 line Body of Loop can be multiple lines

5 For Loop Variations for (;;)  Infinite Loop for (int counter = 1; counter < 11;)  Increment counter inside of the loop for (; counter < 11; counter = counter + 1)  Initialize the counter outside of the loop

6 Incrementing and Decrementing Increment  number1 = number1 + 1 Or  number1++ Shortcut to make it easier to read and avoid errors. Decrement  number1 = number1 - 1 Or  number1-- Shortcut to make it easier to read and avoid errors.

7 Comprehension Check For Loops

8 While Loop Executes an indefinite number of times.  The body of the loop is executed as long as the condition is true. Developer controls when condition changes.  Done in the body of the loop.  If not done, the loop will never end. Condition is evaluated before the body of the loop is executed.  The body of the loop is not guaranteed to be executed. while (continuation condition) { Body of Loop } Initialization of counter occurs outside of loop Incrementation occurs inside of loop Keep looping until this condition is false

9 Example of While Loop Prints 0, 5, 10, 15

10 Example of While Loop Prints 0, 5, 10, 15 Modified to make sure 15 is printed

11 Example of While Loop The condition is evaluated first, So nothing is printed.

12 Comprehension Check While Loops

13 Do-While Loop Executes an indefinite number of times.  The body of the loop is executed as long as the condition is true. Developer controls when counter is incremented.  Done in the body of the loop.  If not done, the loop will never end. Condition is evaluated after the body of the loop is executed.  The body of the loop is executed at least once. do { Body of Loop } while (continuation condition) Initialization of counter occurs outside of loop Incrementation occurs inside of loop

14 Example of Do-While Loop Prints 0, 5, 10, 15

15 Example of Do-While Loop Prints 0, 5, 10, 15Modified to make sure 15 is printed

16 Example of Do-While Loop The body of the loop is executed first. Then, the condition is evaluated to see if we want to repeat the loop. Therefore, “Hello World” is printed.

17 Comprehension Check Do-While Loops

18 Quitting a Loop The break command is used to exit a loop

19 Demonstration Quitting a Loop


Download ppt "Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop "

Similar presentations


Ads by Google