Hi I have hard time connecting the if else statement because my teacher is telling us to use this kind of if else

if (statement)
if (statement)
if (statement)
condition
else
condition("error")
end if
else
condition
end if
else
condition
end if
this is a simple betting game can you please tell me how to arrange this code i really have hard time doing this in this way and there slight problems in this code

If (Val(txtInput.Text) >= 0 And Val(txtInput.Text) <= 9 And IsNumeric(txtInput.Text)) Then
            lblRandom.Text = Int(Rnd() * 10)
        Else
            MessageBox.Show("Wrong Input!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            SendKeys.Send("{Home}+{End}")
            txtInput.Focus()
        End If

        If (Val(txtInput.Text) > Val(lblRandom.Text)) Then
            lblOutput.Text = "You Win!"
        End If
        If (Val(txtInput.Text) < Val(lblRandom.Text)) Then
            lblOutput.Text = "You Lose!"
        End If
        If (txtInput.Text = lblRandom.Text) Then
            lblOutput.Text = "Draw!"
        End If
Comments
You had plenty of help on this. Nice job marking it as solved.

Use the If statements inside one another.

If 1 > 0 Then
            If 2 < 3 Then
                If 4 = 4 Then
                    MsgBox("You Win! :)", MsgBoxStyle.Information)
                Else
                    MsgBox("error", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox("You Tie! :/", MsgBoxStyle.Information)
            End If
        Else
            MsgBox("You Loose! :(", MsgBoxStyle.Information)
        End If

Since you are new to this, a small hint:
If you get lost with the nested IFs and can't tell where each else (or end if) goes to which if, it might help you to add a comment at the end of the line starting the if with a number. Use the same number for the matching end if and you know where each if starts and ends.

Like this:

if 1=1 ' 1
if 2=2 ' 2 
if 3=3 '3 
...

end if '3
else ' 2
 if a=a ' 4

else '4

end if '4
end if '2

else '1 
end if '  1

Edited 5 Years Ago by adam_k: n/a

Comments
+=1 for comments. :)

I think correct indenting is easier to understand than the numbers beside them and is a habit you should get into. Other coders will expect to see it.

'I think your code just about has it.

        'First thing you should do is validate that the input
        'is numeric.  Your example has the validation in 1 line
        'with the numeric check as the last check. You can put it
        '1 line but the IsNumeric needs to be first and followed
        'by AndAlso so that if its not numeric the next constraint
        'will not be checked. 
        If Not IsNumeric(TextBoxInput.Text) Then
            'msg error
            'Get out of the sub since its bad.
            Exit Sub
        End If

        'If you wanted your check in 1 line like you displayed then
        'it should be:
        'If Not IsNumeric(TextBoxInput.Text) OrElse CInt(TextBoxInput.Text) < 0 OrElse CInt(TextBoxInput.Text) > 10 Then
        '    'bad input which would already take care of the next constraint check.
        'End If


        'Now at this point its numeric.  Go ahead and create
        'a variable to store the user's value as an integer.
        Dim UserInput As Integer = CInt(TextBoxInput.Text)

        'Now validate the user input is between 0 and 10.
        'If its not then do not continue.
        If UserInput < 0 OrElse UserInput > 10 Then
            'msg error 0-10
            Exit Sub
        End If

        'Now create the random number as an integer if thats
        'what you want.
        Dim ComputerInput As Integer = CInt(Rnd() * 10)

        'Update the label
        LabelRandom.Text = CStr(ComputerInput)

        'Now use an if elseif instead of 3 Ifs
        'The single ifs do the same thing but
        'if the first condition is met then
        'your wasting 2 other if checks.
        If UserInput > ComputerInput Then
            'you win
        ElseIf UserInput < ComputerInput Then
            'you lose
        ElseIf UserInput = ComputerInput Then
            'you tie
        End If

        'In your code you use Val.  If your dealing with a whole number
        'then you should use cint instead of val.  And if your dealing
        'with a double then use cdbl or val.  There is also csng for single.
        'You should convert to what the variable should be instead of
        'Val.  You should forget about Val.

        'In this case it doesn't really matter but Integer is much faster than
        'double.  You don't want to get into the habbit of converting things
        'to what they shouldn't be.
Comments
Congrats for excellent commenting. I am too bored to comment this heavily, but I admire anybody that takes the time to. Hope this is how all your apps look like.

Hi i thank you for giving me those example but my teacher won't allow us to use those codes here i am having problem with the YOU WIN YOU LOSE AND DRAW i am really tired please tell me where i am wrong and i will fix it

If IsNumeric(txtInput.Text) Then
            If (Val(txtInput.Text) >= 0 And Val(txtInput.Text) <= 9) Then
                If (Val(txtInput.Text) < Val(lblRandom.Text) Or Val(txtInput.Text) > Val(lblRandom.Text)) Then
                    If (Val(txtInput.Text) = Val(lblRandom.Text)) Then

                        lblOutput.Text = "Draw!"
                    End If

                    lblOutput.Text = "You Lose!"
                Else
                    lblOutput.Text = "You Win!"
                End If

                lblRandom.Text = Int(Rnd() * 10)
            Else
                MessageBox.Show("Wrong Input!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                SendKeys.Send("{Home}+{End}")
                txtInput.Focus()
            End If

        Else
            MessageBox.Show("Wrong Input!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            SendKeys.Send("{Home}+{End}")
            txtInput.Focus()
        End If

I think that you are having a logical error here.
You are checking if the input is smaller or greater than the lblrandom and if it is, you check if it is equal to. If it is smaller or greater it can't be equal to. The if checking for equal to should be at the else clause of

If (Val(txtInput.Text) < Val(lblRandom.Text) Or Val(txtInput.Text) > Val(lblRandom.Text)) Then

PS: Can you give us a clear rule as to when somebody is winning or losing or draw? this is not clear from your code.
PS2: Does the lblRandom have a value from a previous run? And why are you setting it's value at that point?

The rule is like this when i input a number let number be X if i will click Draw button the random will generate a number if X>random then you win if X<random then you lose or Draw if they are equal

See if this will work for you (might contain errors, I just wrote it in the reply box)

If (Val(txtInput.Text) >= 0 And Val(txtInput.Text) <= 9) Then
                If (Val(txtInput.Text) < Val(lblRandom.Text) and Val(txtInput.Text) > Val(lblRandom.Text)) Then
                    If (Val(txtInput.Text) < Val(lblRandom.Text)) Then
                    lblOutput.Text = "You Lose!"

               Else
                    lblOutput.Text = "You Win!"

                    End If
                Else
                        lblOutput.Text = "Draw!"
                End If

This could be a valuable lesson, so make sure you understand how each if affects the other, and why my code has a chance to work.

Edited 5 Years Ago by adam_k: n/a

one other thing. I think you will need to use

Randomize

at the top of your program to get a truely random number, other wise you get the same sequence of numbers each time you run the program

See if this helps.

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        '// CInt="Convert To Integer".
        If CInt(txtInput.Text) >= 0 AndAlso CInt(txtInput.Text) <= 9 Then '// if correct # entered, proceed.
            Dim rnd As New Random '// declare new random.
            lblRandom.Text = rnd.Next(0, 10).ToString '// Randomize (0 is minimum #, 10 is max and will only randomize to 9)
            If CInt(txtInput.Text) > CInt(lblRandom.Text) Then lblOutput.Text = "You Win!"
            If CInt(txtInput.Text) < CInt(lblRandom.Text) Then lblOutput.Text = "You Lose!"
            If CInt(txtInput.Text) = CInt(lblRandom.Text) Then lblOutput.Text = "Draw!"
        Else
            MsgBox("Number entered can only be between 0 and 9.", MsgBoxStyle.Critical)
            txtInput.Select()
            txtInput.SelectAll()
        End If
    End Sub

    '// Numeric TextBox.
    Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
        Select Case e.KeyChar
            Case "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c, "0"c, CChar(vbBack) '// allow numeric and backspace.
                Exit Sub
            Case Else
                e.Handled = True '// block.
        End Select
    End Sub
End Class
This question has already been answered. Start a new discussion instead.