# Write a program, which uses post-test loop, to compute the sum of a

Page 1 of 1

## 3 Replies - 26606 Views - Last Post: 25 October 2009 - 10:11 AMRate Topic:     //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=134117&amp;s=64ab48bbdf2222689e61ae9f04b193a5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 akash_9105 Reputation: 0
• Posts: 31
• Joined: 04-April 08

# Write a program, which uses post-test loop, to compute the sum of a Posted 24 October 2009 - 11:42 AM

Write a program, which uses post-test loop, to compute the sum of a series of numbers entered by the user. Note that the sum is displayed when the user enters the value 0, i.e. zero

Here is what i have done :

```i=0
sum=0.0
x=input("Enter a number : ")

while x!=0:
sum=x+sum
x=input("Enter a number : ")
i=i+1

if x==0:
print "sum is : ",sum

```

The code works fine but i am not being able to understand what is meant by a POST TEST LOOP . Can someone explain to me what is meant by a post test loop ?

thanx

Is This A Good Question/Topic? 0

## Replies To: Write a program, which uses post-test loop, to compute the sum of a

### #2 Tshiknn Reputation: 4
• Posts: 20
• Joined: 18-October 09

## Re: Write a program, which uses post-test loop, to compute the sum of a

Posted 24 October 2009 - 08:06 PM

A normal loop checks the conditional, then if found true, runs the code. Then it goes back to the conditional and repeats. A post-test loop is a loop that runs the code, then checks the condition, then if found true, runs the code again. The difference is that a post-test loop is always runs the code once, even if the conditional isn't true, whereas a pre-test loop doesn't run the code if the conditional isn't true. Here's the basic form for a post-test loop:

```# post-test loop
while True:
<statements>
if not <conditional>:
break

# pre-test loop
while <conditional>:
<statements>
```

You don't often use post-test loops, but sometimes they're useful.

### #3 akash_9105 Reputation: 0
• Posts: 31
• Joined: 04-April 08

## Re: Write a program, which uses post-test loop, to compute the sum of a

Posted 24 October 2009 - 09:32 PM

Thanx for your quick response, so even though my code is working, is it a post test loop ?

Here is my code:
```i=0
sum=0.0
x=input("Enter a number : ")

while x!=0:
sum=x+sum
x=input("Enter a number : ")
i=i+1

if x==0:
print "sum is : ",sum

``` Tshiknn, on 24 Oct, 2009 - 07:06 PM, said:

A normal loop checks the conditional, then if found true, runs the code. Then it goes back to the conditional and repeats. A post-test loop is a loop that runs the code, then checks the condition, then if found true, runs the code again. The difference is that a post-test loop is always runs the code once, even if the conditional isn't true, whereas a pre-test loop doesn't run the code if the conditional isn't true. Here's the basic form for a post-test loop:

```# post-test loop
while True:
<statements>
if not <conditional>:
break

# pre-test loop
while <conditional>:
<statements>
```

You don't often use post-test loops, but sometimes they're useful.

### #4 Tshiknn Reputation: 4
• Posts: 20
• Joined: 18-October 09

## Re: Write a program, which uses post-test loop, to compute the sum of a

Posted 25 October 2009 - 10:11 AM

No, not really. A post test loop would be an infinite loop that runs the code, then breaks if the conditional isn't true. A post-test loop would look like this in your program:

```i=0
sum=0.0
x=input("Enter a number : ")

while True:
sum=x+sum
x=input("Enter a number : ")
i=i+1

if x==0:
print "sum is : ", sum
break
```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }