4 Replies - 846 Views - Last Post: 10 April 2011 - 11:48 AM Rate Topic: -----

#1 fed2056794  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 22
  • Joined: 07-March 11

Help how do I reset a loop so the when it loops it goes back to 0

Posted 08 April 2011 - 05:27 PM

In the first loop once it cycles through once it does not reset the data so it continues to hold the previous data so therefore the outcome is incorrect. Can I reset it to start back at 0 ???

Module Module1

    Sub Main()

      

            'declare variables

        Dim s1 As String = Console.ReadLine()

            Dim a As Integer = 0

            Dim b As Integer = 0

            Dim c As Integer = 0

            Dim d As Integer = 0

            Dim e As Integer = 0
 
            'loop         
        Do

            Console.WriteLine("Please enter a sentence")

            s1 = Console.ReadLine()

            If s1 = "all done" Then Exit Do ' exit loop if s1 = "all done"

            For count As Integer = 0 To s1.Length - 1

                'count occurences of letters

                Select Case s1.Chars(count)

                    Case ("a"c)

                        a += 1

                    Case ("b"c)

                        b += 1

                    Case ("c"c)

                        c += 1

                    Case ("d"c)

                        d += 1

                    Case ("e"c)

                        e += 1

                End Select
            Next

            'output results

            Console.WriteLine("A = " & a & vbNewLine & "B = " & b & vbNewLine & "C = " & c & vbNewLine & "D = " & d & vbNewLine & "E = " & e)

        Loop
 
            'this should be in a loop like the letter counter loop

            'prompt for a sentence

            Console.WriteLine("Please enter something")
            Dim s3 As String = Console.ReadLine()

            'replace words in sentence

            Dim s2 As String = s3.Replace("hi", "hello").Replace("today", "tomorrow").Replace("hate", "love")

            'output results

            Console.WriteLine(s3)

            Console.WriteLine(s2)

            Console.ReadLine()
 
        End Sub
 
    End Module


Is This A Good Question/Topic? 0
  • +

Replies To: Help how do I reset a loop so the when it loops it goes back to 0

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: Help how do I reset a loop so the when it loops it goes back to 0

Posted 08 April 2011 - 05:35 PM

Have you considered setting back to zero?
Where in the loop would be the best place to do that?
Was This Post Helpful? 0
  • +
  • -

#3 fed2056794  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 22
  • Joined: 07-March 11

Re: Help how do I reset a loop so the when it loops it goes back to 0

Posted 10 April 2011 - 08:40 AM

I am assuming at the end of the loop, this is where I struggle most with these loops I just don't understand how to get it back to the original once it has gone through the data once. I struggle with how I write it.

View PostAdamSpeight2008, on 08 April 2011 - 05:35 PM, said:

Have you considered setting back to zero?
Where in the loop would be the best place to do that?

Was This Post Helpful? 0
  • +
  • -

#4 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: Help how do I reset a loop so the when it loops it goes back to 0

Posted 10 April 2011 - 09:56 AM

Wherever you want to reset it to zero just pop this in:
count = 0

Count is the variable the for loop is using to count so you just have to set that variable to 0.
Was This Post Helpful? 0
  • +
  • -

#5 fed2056794  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 22
  • Joined: 07-March 11

Re: Help how do I reset a loop so the when it loops it goes back to 0

Posted 10 April 2011 - 11:48 AM

Would that go just before the "loop"?

View PostMATTtheSEAHAWK, on 10 April 2011 - 09:56 AM, said:

Wherever you want to reset it to zero just pop this in:
count = 0

Count is the variable the for loop is using to count so you just have to set that variable to 0.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1