/
Looping Looping

Looping - PowerPoint Presentation

lindy-dunigan
lindy-dunigan . @lindy-dunigan
Follow
382 views
Uploaded On 2017-12-20

Looping - PPT Presentation

By Muhammad Zidny Nafan 30 January 2012 Looping Structure Initialization the first action before looping started Condition boolean expression for looping Loopings body part of program that will be looped ID: 616978

sum loop exp slide loop sum slide exp education pearson 2003 copyright loops product looping count statement body pseudocode

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Looping" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Looping

By: Muhammad Zidny Nafan

30 January 2012Slide2

Looping Structure

Initialization

, the first action before looping started.

Condition

, boolean expression for looping.

Looping’s body

, part of program that will be looped

Terminator

, the action to terminated loopingSlide3

For

for(exp1; exp2; exp3) statement;

atau:

for(exp1; exp2; exp3)

{

statement1; statement2; …….}

exp1

is

initialization

exp2

is

condition

exp3

is

increment

or

decrement expression

Looping will run if the increment or decrement equal with conditionSlide4

Example 1

Write “Hello World” ten timesSlide5

Struktur While

while (exp) statements;

atau :

while(exp)

{

statement1; statement2; …..;}

exp

adalah boolean expression

Checking for

exp

before statement executed exp will be checked first. If TRUE looping will be processed, if FALSE program will do nothingSlide6

Example 2

Write number 1 to 10Slide7

Struktur do-while

do

{

< statements >;

} while(exp);

Selama nilai exp true maka statement dieksekusi berulang-ulang.Pengetesan exp dilakukan SETELAH meng-eksekusi statement.

Pada konstruksi do-while statement atau blok statement pasti dikerjakan paling sedikit satu kali, karena ekspresi boolean baru diuji pada akhir blok pengulangan

.Slide8

Example 3

penggunaan do-while pada pemilihan menu:

<see in modul>Slide9

Copyright © 2003 Pearson Education, Inc.

Slide

9

Sums and Products

A common task is reading a list of numbers

and computing the sumPseudocode for this task might be: sum = 0; repeat the following this_many times cin >> next; sum = sum + next;

end of loop

This pseudocode can be implemented with a for-loop

as shown on the next slideSlide10

Copyright © 2003 Pearson Education, Inc.

Slide

10

for-loop for a sum

The pseudocode from the previous slide is

implemented as int sum = 0;for(int count=1; count <= this_many; count++) { cin >> next; sum = sum + next; }sum must be initialized prior to the loop body!Slide11

Copyright © 2003 Pearson Education, Inc.

Slide

11

for-loop For a Product

Forming a product is very similar to the sum

example seen earlier int product = 1; for(int count=1; count <= this_many; count++) {

cin >> next;

product = product * next

;

}

product must be initialized prior to the loop body

Notice that product is initialized to 1, not 0!Slide12

Copyright © 2003 Pearson Education, Inc.

Slide

12

Which Loop To Use?

Choose the type of loop late in the design process

First design the loop using pseudocodeTranslate the pseudocode into C++The translation generally makes the choice of anappropriate loop clear

While-loops are used for all other loops when there

might be

occassions

when the loop should not run

Do-while loops are used for all other loops when

the loop must always run at least onceSlide13

Copyright © 2003 Pearson Education, Inc.

Slide

13

Choosing a for-loop

for-loops

are typically selected when doing numeric calculations, especially when using a variable changed by equal amounts each time the loop iteratesSlide14

Copyright © 2003 Pearson Education, Inc.

Slide

14

Choosing a while-loop

A while-loop is typically used

When a for-loop is not appropriateWhen there are circumstances for which the loop body should not be executed at allSlide15

Copyright © 2003 Pearson Education, Inc.

Slide

15

Choosing a do-while Loop

A do-while-loop is typically used

When a for-loop is not appropriateWhen the loop body must be executed at least onceSlide16

Copyright © 2003 Pearson Education, Inc.

Slide

16

Debugging Loops

Common errors involving loops include

Off-by-one errors in which the loop executes one too many or one too few timesInfinite loops usually result from a mistake

in the Boolean expression that controls

the loop