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

Page 1 of 1

3 Replies - 9466 Views - Last Post: 25 October 2009 - 10:11 AM Rate Topic: -----

#1 akash_9105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-April 08

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

Post icon  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  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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.
Was This Post Helpful? 2
  • +
  • -

#3 akash_9105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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








View PostTshiknn, 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.

Was This Post Helpful? 0
  • +
  • -

#4 Tshiknn  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1