Know your ISP.

breath-hyenas
User #158056   1186 posts
Whirlpool Enthusiast

Begin mainprogram
set cash to 100
repeat
Get payment
cash = payment + cash
until cash > 50
Print cash
End Program

this is a school task , and they said if the payment = '20' , what would the outout be. I am quit confused coz if you set cash to 100, the cash is > 50 already and doesnt need to enter the loop. and just prints 100

reference: whrl.pl/RbVNQc
posted 2009-Jul-3, 8am AEST
User #112740   306 posts
Forum Regular

Metoo writes...

I am quit confused coz if you set cash to 100, the cash is > 50 already and doesnt need to enter the loop. and just prints 100

Begin mainprogram
set cash to 20

$ repeat Get payment cash = payment + cash until cash > 50
$ Print cash End Program

what would happen here?
Thats what they're asking

reference: whrl.pl/RbVNVz
posted 2009-Jul-3, 8am AEST
User #30842   3758 posts
Whirlpool Forums Addict

Some loops ALWAYS evaluate once. That is why there are several sorts of loops (Some loops may never loop). You might not want the loop to evaluate once if some conditions are met.

So you have to figure out (when you are writing a program), if you want the loop to always run at least once.

Your job is to workout what sort of loop this is. Maybe it is the wrong sort (or maybe it is not).

reference: whrl.pl/RbVOhW
posted 2009-Jul-3, 10am AEST
User #31410   8661 posts
Whirlpool Forums Addict

Metoo writes...

just prints 100

cash = 100
payment = 20

Since the exit condition (cash > 50) is evaluated after cash = payment + cash, then it will print "120"

reference: whrl.pl/RbVOnm
posted 2009-Jul-3, 11am AEST
User #29925   1298 posts
Whirlpool Enthusiast

The loop used in your pseudocode is a post-test loop meaning the loop is executed once and then the condition is evaluated. Therefore cash = 120.

reference: whrl.pl/RbVOTa
posted 2009-Jul-3, 1pm AEST
User #20620   4409 posts
Whirlpool Forums Addict

just to elaborate on what others are saying...

Metoo writes...

repeat

Get payment

cash = payment + cash

until cash > 50

it is a bottom driven loop. which means that it will *always* execute at least once before checking a condition.

http://www.gailer-net.de/tutorials/java/Notes/chap44/ch44_3.html

notice how the "until" line is executed last.

if it was a top driven loop it would start

until cash > 50
yada
yada
yada
repeat

reference: whrl.pl/RbVO5e
posted 2009-Jul-3, 1pm AEST
User #255811   1445 posts
Whirlpool Enthusiast

Repeat
Task
Until Condition

Will always run the task once before evaluating the condition.

While condition
Task
End While

Will only run the task if the condition is true. If the condition is false then nothing happens.

reference: whrl.pl/RbVO6R
posted 2009-Jul-3, 1pm AEST
User #176087   1199 posts
Whirlpool Enthusiast

Don't get intimidated, just read it line by line like you are a dumb computer, doing exactly what it says.

set cash to 100 --- woo 100!
repeat — ok!
Get payment — woo, got 20!
cash = payment + cash --- yay now i have 120 in my cash!
until cash > 50 --- hrmmm cash is more than 50, so i dont repeat
Print cash --- "120"!
End Program --- yay!

AMD CPUs aren't as happy about things though, they tend to grumble a lot.

reference: whrl.pl/RbVPr9
posted 2009-Jul-3, 3pm AEST
User #162600   563 posts
Whirlpool Enthusiast

phx writes...

AMD CPUs aren't as happy about things though, they tend to grumble a lot.

That's not my CPU, that's my stomach.
Feed me! Feed me!

reference: whrl.pl/RbVPxv
posted 2009-Jul-3, 3pm AEST
User #112740   306 posts
Forum Regular

phx writes...

just read it line by line like you are acting dumb

who said i was acting? :P

reference: whrl.pl/RbVPM6
posted 2009-Jul-3, 4pm AEST
Hosted by
Bulletproof Managed Hosting
Big numbers
1,668,885 threads
32,921,337 posts
3,365,326 whims sent
3,986 wiki topics
195 ISPs listed
10,178 broadband plans
1,268 modems & routers
59,837 features filled