# Do/While Pretest Loop

• (2 Pages)
• 1
• 2

## 29 Replies - 2549 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=c034ad27e3b1b22429eb2559b76915aa&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

# Do/While Pretest Loop

Posted 11 April 2013 - 05:23 PM

Strontium-90, a radioactive element that is part of the fallout from nuclear explosions, has
a half-life of 28 years. This means that a given quantity of strontium-90 will emit radioac-
tive particles and decay to one-half its size every 28 years. How many years are required for
100 grams of strontium-90 to decay to less than 1 gram?

I need to use a do/while pretest loop to solve this problem
Here's what I have so far:
```Dim grams as Decimal
Dim years as Integer
Do While grams >= 1
grams = 100
Loop

```

I just don't understand if the years or grams variables are supposed to be put in the loop. Sorry, I just started learning loops this week.

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

Is This A Good Question/Topic? 0

## Replies To: Do/While Pretest Loop

### #2 torind_2000

• D.I.C Regular

Reputation: 57
• Posts: 292
• Joined: 22-August 11

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 05:29 PM

do the math inside the loop.

### #3 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,295
• Joined: 12-September 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 05:34 PM

x RadicalAura x, on 11 April 2013 - 06:23 PM, said:

Strontium-90, a radioactive element that is part of the fallout from nuclear explosions, has
a half-life of 28 years. This means that a given quantity of strontium-90 will emit radioac-
tive particles and decay to one-half its size every 28 years. How many years are required for
100 grams of strontium-90 to decay to less than 1 gram?

I need to use a do/while pretest loop to solve this problem
Here's what I have so far:
```Dim grams as Decimal
Dim years as Integer
Do While grams >= 1
grams = 100
Loop

```

I just don't understand if the years or grams variables are supposed to be put in the loop. Sorry, I just started learning loops this week.

The best way to approach it is to think of what you want to do, and to break it into individual steps. In other words, how would yo do it manually?

First, you start with 100 grams, so would that go inside the loop? Or would it be a starting condition, to be set before the loop starts?
Then, when you enter the loop, do something with the number of grams. Then do something with the years. If you do the right things in the loop, you'll fall out (pun intended) the bottom with the answer.

This post has been edited by lar3ry: 11 April 2013 - 05:36 PM

### #4 Atli

Reputation: 3853
• Posts: 6,356
• Joined: 08-June 10

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 05:35 PM

Hey.

Loops are fairly simple. They just keep repeating the same block of code until the condition(s) are no longer true.

In your case, you want to add 28 years to the "years" number, and reduce the "grams" by half over and over again until the "grams" are less than 1. For that to happen, you need to set the loop up to meet that condition (which you already have done) and then do the calculations inside the loop.

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

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 05:41 PM

Thanks for the feedback. Does it look like I'm headed in the right direction?
```Dim grams as Decimal
Dim years as Integer = 28
Do While grams = >1
grams = 100
grams / 2
Loop

```

### #6 torind_2000

• D.I.C Regular

Reputation: 57
• Posts: 292
• Joined: 22-August 11

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 05:43 PM

does it give you the correct answer?

### #7 Atli

Reputation: 3853
• Posts: 6,356
• Joined: 08-June 10

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 05:53 PM

Consider what is happening in your loop. While the grams are larger than 1, you set the grams to 100 and then divide it by two. Does that sound right? What will "grams" be set to at the end of each iteration of the loop?

### #8 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,295
• Joined: 12-September 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 07:36 PM

x RadicalAura x, on 11 April 2013 - 06:41 PM, said:

Thanks for the feedback. Does it look like I'm headed in the right direction?
```Dim grams as Decimal
Dim years as Integer = 28
Do While grams = >1
grams = 100
grams / 2
Loop

```

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

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 07:55 PM

Does it make any difference if I define the "years" variable before the loop, like so (as opposed to the way I originally had it defined within the loop)?:
```Dim grams as Decimal = 100
Dim years as Integer = 28
Do While grams = >1
grams / 2
Loop

```

• MrCupOfT

Reputation: 2290
• Posts: 9,529
• Joined: 29-May 08

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 08:20 PM

Do you just jump straight in to coding?

Imagine you are telling some one over the phone the steps involved to get the answer. (An Algorithm).
Write those step out on paper. (no code at this time)

Do a pencil and paper run through the algorithm. Does it work?
No go back and think about the problem.

Now translate that algorithm into code.
Use the debugger to step through the code.
Check to see running of the code matches your expected results.

Finally don't be afraid of trying stuff out. It doesn't take that long to run the code.
You'll learn a lot from your unsuccessful experimentations.

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

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 08:44 PM

I think I might have it!
```Dim grams as Decimal = 100
Dim years as Integer = 28
Do While grams =>1
Loop
Do While Years <= 56
Loop

```

Am I at least headed in the right direction? It makes sense to me since it would exit the loop or "fall out" like lar3ry's pun once both the grams =>1 and Years <= 56 conditions are met. By the way, sorry for the delay in between my posts. It's not like I've been banging my head against the wall for hours on this problem, just been juggling some other schoolwork as well!

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

### #12 _HAWK_

• Master(Of Foo)

Reputation: 1151
• Posts: 4,418
• Joined: 02-July 08

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 08:57 PM

Well => is not valid in VB(try >=), C# it would be a function though. How is that working - there is nothing in the loops?

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

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 09:09 PM

_HAWK_, on 11 April 2013 - 08:57 PM, said:

Well => is not valid in VB(try >=), C# it would be a function though. How is that working - there is nothing in the loops?

Fixed the => problem! As far as what should be in the loop, that's what I'm stuck on. All my instructor gave me was the hypothetical question, that's it. No variables, no specifics about how he would like the answer to be output. I assume that's what would need to go into the loop to output the answer, but there's no clarification as to whether it should be a text box, label, list box, etc.

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

### #14 _HAWK_

• Master(Of Foo)

Reputation: 1151
• Posts: 4,418
• Joined: 02-July 08

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 09:41 PM

Don't worry about the controls right now, how do you make the equation?

Pseudo code:

```particleSize = {some amount}
For Each 28years
particle is 50% of previous
Next
```

Hint:

```For i As Integer = 0 To years Step 28
'do calc
Next
```

This post has been edited by _HAWK_: 11 April 2013 - 09:38 PM

### #15 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,295
• Joined: 12-September 12

## Re: Do/While Pretest Loop

Posted 11 April 2013 - 09:44 PM

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

_HAWK_, on 11 April 2013 - 08:57 PM, said:

Well => is not valid in VB(try >=), C# it would be a function though. How is that working - there is nothing in the loops?

Fixed the => problem! As far as what should be in the loop, that's what I'm stuck on. All my instructor gave me was the hypothetical question, that's it. No variables, no specifics about how he would like the answer to be output. I assume that's what would need to go into the loop to output the answer, but there's no clarification as to whether it should be a text box, label, list box, etc.

Have you actually thought about the problem? A few of us have advised you to think about how you would figure it out, and it appears to us that you have not thought about it at all. You have tried writing some random code that not only doesn't give you an answer, but actually has no way it could possibly give you an answer, because no calculations are done.

Never mind the output for now. Never mind whether you are going to show the answer in a TextBox or a Label, or have an Aardvark tap it out in Morse Code with his claws. How are you going to figure out how long it will take 100 grams of Strontium 90 to decay to 1 gram or less?

Before you jot down any more random code fragments, figure out HOW you would do it if you only had a piece of paper and a pencil.

What do you know about the proble?

1. you have 100 grams of Strontium 90.
2. after 28 years, you have 50 grams left.
3. how many years will pass before you have 25 grams left.
4. etc....

You ask us where the 100 grams should be, or where the 28 years should be. If we tell you, will you understand WHY it should be where we say it should be?

Trust me, you will NEVER write a non-trivial program by placing randomly generated lines of code together in arbitrarily positioned sequences. Programming requires:

1. a problem that can be stated
2. an understanding of how that problem can be solved
3. a plan based on how you would find the answer
4. a translation of that plan into code
5. And finally, how you will accept input and show output.

While you can sometimes change the order of a few of these, this is fundamental programming, and you really need to stop tossing random lines of code into the program if you are to be successful at solving the problem.

This post has been edited by lar3ry: 11 April 2013 - 09:46 PM