Do/While Pretest Loop

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1550 Views - Last Post: 13 April 2013 - 06:27 AM Rate Topic: -----

#16 x RadicalAura x  Icon User is offline

  • New D.I.C Head

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

Was This Post Helpful? 0
  • +
  • -

#17 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,086
  • 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%?
Was This Post Helpful? 1
  • +
  • -

#18 x RadicalAura x  Icon User is offline

  • New D.I.C Head

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

Re: Do/While Pretest Loop

Posted 11 April 2013 - 09:56 PM

View Post_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.
Was This Post Helpful? 0
  • +
  • -

#19 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,086
  • 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.
Was This Post Helpful? 0
  • +
  • -

#20 x RadicalAura x  Icon User is offline

  • New D.I.C Head

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

Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:05 PM

View Post_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.
Was This Post Helpful? 0
  • +
  • -

#21 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:06 PM

View Postx 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
  'OK, there's a nice initial value to start with

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!
Was This Post Helpful? 1
  • +
  • -

#22 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,086
  • 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.
Was This Post Helpful? 1
  • +
  • -

#23 x RadicalAura x  Icon User is offline

  • New D.I.C Head

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

Re: Do/While Pretest Loop

Posted 11 April 2013 - 10:09 PM

View Postlar3ry, on 11 April 2013 - 10:06 PM, said:

View Postx 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
  'OK, there's a nice initial value to start with

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.

View Post_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

Was This Post Helpful? 0
  • +
  • -

#24 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,086
  • 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?
Was This Post Helpful? 0
  • +
  • -

#25 x RadicalAura x  Icon User is offline

  • New D.I.C Head

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

Was This Post Helpful? 0
  • +
  • -

#26 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • 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.

Let's see your code now.

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.
Was This Post Helpful? 0
  • +
  • -

#27 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Do/While Pretest Loop

Posted 12 April 2013 - 06:56 AM

Given:

half-life (/2) = 28 years
100 grams to start with

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

Was This Post Helpful? 0
  • +
  • -

#28 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • 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.
Was This Post Helpful? 0
  • +
  • -

#29 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Do/While Pretest Loop

Posted 12 April 2013 - 03:39 PM

View PostAdamSpeight2008, 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.
Was This Post Helpful? 0
  • +
  • -

#30 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Do/While Pretest Loop

Posted 13 April 2013 - 06:27 AM

View PostAdamSpeight2008, 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2