hey guys,

i've got an 'if' statement that works but i have to change it into a 'loop' statement. i originally chose 'if' because it's easier for me to understand but... it's gotta be a loop.

so if anyone could, what would be the best loop style and how would i set it up?

If chancecount <= 8 Then

            If isConverted = True Then

                If chancecount <= 8 AndAlso usernum < randomnum Then
                    chancecount = chancecount + 1
                    Me.xAnswerLabel.Text = "Oops, too low. Go a little higher"

                ElseIf chancecount <= 8 AndAlso usernum > randomnum Then
                    chancecount = chancecount + 1
                    Me.xAnswerLabel.Text = "Whoa! Whoa! You're guessing a little TOO high now."

                ElseIf usernum = randomnum Then
                    Me.xAnswerLabel.Text = "Congratulations, you have selected the correct number which is, " + Convert.ToString(usernum) + "!"

                End If

            End If

        ElseIf chancecount > 8 Then
            Me.xAnswerLabel.Text = "We are so sorry but your chances have expired. Don't fret, try with us again anytime. Just exit this application and restart the program. But remember... The number will never be the same! Happy Hunting!"

        End If

Recommended Answers

All 6 Replies

i don't think this if statement can be changed to a loop as there are too many conditions to check.

that's what i'm thinking but my professor's like... "I want you to use a loop"

and i don't see how to make this work from a loop at all.

Hi,

Where are you writing this code ..? (Which event)
and is there any other code before this line ..:
If chancecount <= 8 Then ..?
I guess, there must be a Loop there..
So which IF, you want to change to the loop? (there are 3 IF's)


Regards
Veena

well he's saying the whole code (as far as the if statements go) should be a loop but i'm not seeing how to change it.

Option Explicit On
Option Strict On

Public Class MainForm
    'show the random number
    Dim randomGenerator As New Random
    Dim randomnum As Integer = randomGenerator.Next(1, 51) 'give the number
    Private chancecount As Integer


    Private Sub xExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xExitButton.Click
        Me.Close()

    End Sub

    Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click

        Dim usernum As Integer
        Dim isConverted As Boolean

        isConverted = Integer.TryParse(Me.xEnterTextBox.Text, usernum)

        If chancecount <= 8 Then

            If isConverted = True Then

                If chancecount <= 8 AndAlso usernum < randomnum Then
                    chancecount = chancecount + 1
                    Me.xAnswerLabel.Text = "Oops, too low. Go a little higher"

                ElseIf chancecount <= 8 AndAlso usernum > randomnum Then
                    chancecount = chancecount + 1
                    Me.xAnswerLabel.Text = "Whoa! Whoa! You're guessing a little TOO high now."

                ElseIf usernum = randomnum Then
                    Me.xAnswerLabel.Text = "Congratulations, you have selected the correct number which is, " + Convert.ToString(usernum) + "!"

                End If

            End If

        ElseIf chancecount > 8 Then
            Me.xAnswerLabel.Text = "We are so sorry but your chances have expired. Don't fret, try with us again anytime. Just exit this application and restart the program. But remember... The number will never be the same! Happy Hunting!"

        End If




    End Sub
End Class
Do
        Do While isConverted = True
            counter += 1
            If chancecount <= 8 AndAlso usernum < randomnum Then
                    chancecount = chancecount + 1
                    Me.xAnswerLabel.Text = "Oops, too low. Go a little higher"

Exit Do
                ElseIf chancecount <= 8 AndAlso usernum > randomnum Then
                    chancecount = chancecount + 1
                    Me.xAnswerLabel.Text = "Whoa! Whoa! You're guessing a little TOO high now."

Exit Do
                ElseIf usernum = randomnum Then
                    Me.xAnswerLabel.Text = "Congratulations, you have selected the correct number which is, " + Convert.ToString(usernum) + "!"
Exit Do

ElseIf chancecount > 8 Then
            Me.xAnswerLabel.Text = "We are so sorry but your chances have expired. Don't fret, try with us again anytime. Just exit this application and restart the program. But remember... The number will never be the same! Happy Hunting!"
Exit Do

                End If

        Loop

Loop Until chancecount <= 8

debug that according to your need...hope it helps...

thanx man. will try tonight and tell you in the morning

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.