School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,152 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,687 people online right now. Registration is fast and FREE... Join Now!




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

 

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

akash_9105

24 Oct, 2009 - 10:42 AM
Post #1

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 5

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 :

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


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

User is offlineProfile CardPM
+Quote Post


Tshiknn

RE: Write A Program, Which Uses Post-test Loop, To Compute The Sum Of A

24 Oct, 2009 - 07:06 PM
Post #2

New D.I.C Head
*

Joined: 18 Oct, 2009
Posts: 18



Thanked: 2 times
My Contributions
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:

CODE
# 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.


User is offlineProfile CardPM
+Quote Post

akash_9105

RE: Write A Program, Which Uses Post-test Loop, To Compute The Sum Of A

24 Oct, 2009 - 08:32 PM
Post #3

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 5

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

Here is my code:
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







QUOTE(Tshiknn @ 24 Oct, 2009 - 07: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:

CODE
# 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.


User is offlineProfile CardPM
+Quote Post

Tshiknn

RE: Write A Program, Which Uses Post-test Loop, To Compute The Sum Of A

25 Oct, 2009 - 09:11 AM
Post #4

New D.I.C Head
*

Joined: 18 Oct, 2009
Posts: 18



Thanked: 2 times
My Contributions
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:

CODE
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

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:39PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month