# Do/While Pretest Loop

• (2 Pages)
• 1
• 2

## 29 Replies - 3660 Views - Last Post: 13 April 2013 - 06:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=318277&amp;s=470d0c307ca788f57dad0b13ad238ed9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 29
• Joined: 01-December 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 09:46 PM

Like so?:
```Dim grams as Decimal = 100
Dim years as Integer = 28
Do While grams >= 1 and years <=56
For i as Integer = 0 To years Step 28
grams = 50
Loop
Next

```

This post has been edited by x RadicalAura x: 11 April 2013 - 09:47 PM

### #17 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 09:50 PM

Would it not be:

```grams *= 0.5
```

Which says grams = grams * 50%

Since it decay 50%?

Reputation: 0
• Posts: 29
• Joined: 01-December 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 09:56 PM

_HAWK_, on 11 April 2013 - 09:50 PM, said:

Would it not be:

```grams *= 0.5
```

Which says grams = grams * 50%

Since it decay 50%?

```Dim grams as Decimal = 100
Dim years as Integer = 28
Do While grams >= 1 and years <=56
For i as Integer = 0 To years Step 28
grams *= 0.5
Loop
Next

```

Better? That makes sense. Sorry, I forgot that I shouldn't define the variable within the loop.

### #19 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:04 PM

Don't think you need to Do While part. You may just want to check the year variable with an If statement surrounding the loop - to check size.

```If years >= {some number} Then
'for loop
End If
```

Why not decay each year by 0.0178571428571429 which = 0.5 in 28 years. Better declination of size and slightly more accurate.

Reputation: 0
• Posts: 29
• Joined: 01-December 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:05 PM

_HAWK_, on 11 April 2013 - 10:00 PM, said:

Don't think you need to Do While part. You may just want to check the year variable with an If statement surrounding the loop - to check size.

```If years >= {some number} Then
'for loop
End If
```

Doesn't a pretest loop have to have the "Do While?" I might have misunderstood.

### #21 lar3ry

• Coding Geezer

Reputation: 314
• Posts: 1,296
• Joined: 12-September 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:06 PM

x RadicalAura x, on 11 April 2013 - 10:46 PM, said:

Like so?:

OK, let's step through your code, one line at a time.

```Dim grams as Decimal = 100

Dim years as Integer = 28
'and another value, which is actually a unit of half-life

Do While grams >= 1 and years <=56
'OK, that's the start of a loop that will run until grams is equal to or less than 1,
'AND years is less than 56

For i as Integer = 0 To years Step 28
'OK, first time through the For loop, i = 0
' second time through, i = 28
grams = 50
' first time though the For loop, grams = 50
'second time through, grams = 50
Loop
'oops, the first part of the Do loop ends here, before the end of the For loop.
'definitelt not going to work.
Next

```

Even if you had the For loop entirely withn the Do loop, you are going to loop forever, because grams will always be set to 50, twice per Do loop, and years will always be 28.

Again, THINK, man, THINK!

### #22 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:06 PM

If it's part of your assignment then it is fine.

Reputation: 0
• Posts: 29
• Joined: 01-December 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:09 PM

lar3ry, on 11 April 2013 - 10:06 PM, said:

x RadicalAura x, on 11 April 2013 - 10:46 PM, said:

Like so?:

OK, let's step through your code, one line at a time.

```Dim grams as Decimal = 100

Dim years as Integer = 28
'and another value, which is actually a unit of half-life

Do While grams >= 1 and years <=56
'OK, that's the start of a loop that will run until grams is equal to or less than 1,
'AND years is less than 56

For i as Integer = 0 To years Step 28
'OK, first time through the For loop, i = 0
' second time through, i = 28
grams = 50
' first time though the For loop, grams = 50
'second time through, grams = 50
Loop
'oops, the first part of the Do loop ends here, before the end of the For loop.
'definitelt not going to work.
Next

```

Even if you had the For loop entirely withn the Do loop, you are going to loop forever, because grams will always be set to 50, twice per Do loop, and years will always be 28.

Again, THINK, man, THINK!

Lar3ry, I adjusted it to grams *= 50, I think that's right.

_HAWK_, on 11 April 2013 - 10:06 PM, said:

If it's part of your assignment then it is fine.

Thanks so much for your help!

This post has been edited by x RadicalAura x: 11 April 2013 - 10:09 PM

### #24 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:09 PM

Good point @lar3ry, but the compiler won't let them do it that way anyhow - typo?

Reputation: 0
• Posts: 29
• Joined: 01-December 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:10 PM

Think I got it finalized, guys. Thanks to all of you who have responded in attempt to help out. Like I said, I just started learning loops/arrays this week so it's kind of uncharted territory for me up until now. This was definitely a good introduction to them though. Thanks again.

This post has been edited by x RadicalAura x: 11 April 2013 - 10:11 PM

### #26 lar3ry

• Coding Geezer

Reputation: 314
• Posts: 1,296
• Joined: 12-September 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:18 PM

Quote

Lar3ry, I adjusted it to grams *= 50, I think that's right.

You have been all over the map with this one. Let's try it this way.

```   Dim grams As Integer = <amount of material>
Dim years As Integer

Do Until grams is <some value>
add <some number of years> to a variable
calculate amount of material left after <some number of years>
Loop

```

And once you have entered your code, compile it. See if it compiles without errors. If not, fix the errors. Once you can run it, check to see if it gives you the right answer. If not, debug it. If you don't know how to debug it, see the Debuggingtutorial.

### #27 dbasnett

Reputation: 118
• Posts: 651
• Joined: 01-October 08

## Re: Do/While Pretest Loop

Posted 12 April 2013 - 06:56 AM

Given:

half-life (/2) = 28 years

Find number of years until grams < 1

Figure out the ??'s and you will have the answer (196)

```        Dim grams As Decimal = 100D
Dim years As Integer = 0
Do While grams >= ??
years += ??
grams /= ??
Loop

```

edit:didn't see larrys post. same idea.

This post has been edited by dbasnett: 12 April 2013 - 07:01 AM

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Do/While Pretest Loop

Posted 12 April 2013 - 02:20 PM

If you use math you don't need the loop. Have a think.

### #29 Atli

Reputation: 4240
• Posts: 7,216
• Joined: 08-June 10

## Re: Do/While Pretest Loop

Posted 12 April 2013 - 03:39 PM

AdamSpeight2008, on 12 April 2013 - 09:20 PM, said:

If you use math you don't need the loop.

Where's the fun in that?

He does mention in the first post that he needs to do this using a loop though.

### #30 dbasnett

Reputation: 118
• Posts: 651
• Joined: 01-October 08

## Re: Do/While Pretest Loop

Posted 13 April 2013 - 06:27 AM

AdamSpeight2008, on 12 April 2013 - 04:20 PM, said:

If you use math you don't need the loop. Have a think.

The loop hints at the math.