Results 1 to 2 of 2

Thread: Need help converting For If Loops to Do While Loops.

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    1

    Angry Need help converting For If Loops to Do While Loops.

    I'm a beginner with visual basic, and not that great at it...basically I am making a simple vowel counter that counts the vowels within a set of string a user inputs. For example, If I click button1, which is deemed as button "A", it should be able to find all letter A's, lower and uppercase of course. I tried several times, but the application just froze whenever I clicked the button in question. So clearly, I'm doing something wrong.

    This is the original code for button "E" with the For If Loops:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'When user clicks button "E"
    Dim str1, s, c As String
    Dim i, l As Integer
    str1 = TextBox1.Text
    l = Len(str1)
    c = 0
    i = 0
    Dim counter As Integer
    For counter = 1 To l
    s = Mid(str1, counter, 1)
    If (s = "E" Or s = "e") Then
    c = c + 1
    End If
    Next
    MsgBox("No of Vowels: " + c.ToString)
    End Sub

    And this is the code I attempted with button "A" to make it into a Do While Loop:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'When user clicks button "A"
    Dim str1, s, c As String
    Dim i, l As Integer
    str1 = TextBox1.Text.Length
    l = Len(str1)
    c = 0
    i = 0
    Dim counter As Integer = 1
    Do While counter <= 1
    s = Mid(str1, counter, 1)
    If (s = "A" Or s = "a") Then
    c = c + 1

    End If
    l += 1
    Loop
    MsgBox("No of Vowels: " + c.ToString)
    End Sub

    Can anyone tell me why it's not working? Literally, I click button "A" and the application just freezes. Thanks for any help you can give me.

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Need help converting For If Loops to Do While Loops.

    Hi,

    The first thing to do is to turn Option Strict On and never turn it off. It will help you to identify and correct Type Conversion errors in your code as you make them.

    The reason why the Do While Loop does not work is because you never change the value of the Counter variable in the Loop which is used as the condition as to whether to continue the Loop or not. Due to this the conditional statement never becomes False so the Loop never ends.

    This question is however best answered with a For Each Loop. The reason for this is that a String is actually an Array of Chars which can easily be looped through to get what you need. Have a look at this:-

    vb.net Code:
    1. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    2.   Dim eCounter As Integer
    3.  
    4.   For Each currentCharacter As Char In TextBox1.Text
    5.     If Char.ToLower(currentCharacter) = "e" Then
    6.       eCounter += 1
    7.     End If
    8.   Next
    9.  
    10.   MsgBox(String.Format("Number of E's = {0}", eCounter))
    11. End Sub

    Hope that helps.

    Cheers,

    Ian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width