Click to See Complete Forum and Search --> : If...Then...Else Statement


vanidosa27
February 22nd, 2006, 06:49 PM
I want to know if I am doing this correctly. Here is my assignment. Complete the If...Then...Else button's Click event procedure by writing an If..Then...Else statement that displays the name of the month corresponding to the number entered by the user
Example, if the user enters 1, the month is January
If the user enters an invalid number display an error message
Here is my pseudocode in If/Else and Case format:
What I am not understanding is the teacher told us to use a nested if, which is just stupid when we could use a If/ElseIf/Else?
A little help please? Thank you so much!
How can I write this pseudocode using a nested if? I don't see it as being possible.

Pseudocode for If/EsleIf/Else

1. Declare variables
2. Assign variables to the string input box
3. If the month code is 1, display January
Else If the month code is 2, display February
Else If the month code is 3, display March
Else If the month code is 4, display April
Else If the month code is 5, display May
Else If the month code is 6, display June
Else If the month code is 7, display July
Else If the month code is 8, display August
Else If the month code is 9, display September
Else If the month code is 10, display October
Else If the month code is 11, display November
Else If the month code is 12, display December
Else
Display “Error”
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

Pseudocode for Case Selection Structure
1. declare variable
2. get month
3. month value:
1 display ”January”
2 display February
3 display “March”
4 display “April”
5 display “May”
6 display “June”
7 display “July”
8 display “August”
9 display “September”
10 display “October”
11 display “November”
12 display “December”
Other display “Error”

dglienna
February 22nd, 2006, 07:09 PM
Take a look at this page. It has samples of each, so you can see that the SELECT CASE statement is recommended rather than IF statements.

http://www.startvbdotnet.com/language/ifthen.aspx

I'm pretty sure that you could just use FORMAT the MONTH when the user enters a number, also.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp

vanidosa27
February 22nd, 2006, 07:12 PM
Yes, that is perfect! I just don't understand why she wants us to do both. Thanks for the link! Perfect! :)

dglienna
February 22nd, 2006, 07:13 PM
Maybe she doesn't know VB or Net? :)

vanidosa27
February 22nd, 2006, 07:26 PM
That is funny! Considering she is the teacher! LOL :) She is also my teacher for Web Programming. I don't know what other classes she teaches. You could look her up on ratemyprofessor.com her name is Dawn Craig at Rock Valley College in Illinois. LOL :)

Got it! Now time to program it!

Using a Nested if...

1. Declare variables
2. Assign variables to the string input box
3. If the month code is 1, Then
display ”January”
Else If the month code is 2, Then
display February
Else If the month code is 3, Then
display “March”
Else If the month code is 4, Then
display “April”
Else If the month code is 5, Then
display “May”
Else If the month code is 6, Then
display “June”
Else If the month code is 7, Then
display “July”
Else If the month code is 8, Then
display “August”
Else If the month code is 9, Then
display “September”
Else If the month code is 10, Then
display “October”
Else If the month code is 11, Then
display “November”
Else If the month code is 12, Then
display “December”
Else
display “Error”
End If

vanidosa27
February 22nd, 2006, 07:43 PM
A little help can take you a long way! What's that saying give a man a fish and he will keep asking you for more... Teach a man to fish and he will eat for life? Not with the mercury in our river! LOL

Private Sub uiIfButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiIfButton.Click
Dim month As Integer
month = Integer.Parse(Me.uiMonthCodeTextBox.Text)
If month = 1 Then
Me.uiMsgLabel.Text = "January"
ElseIf month = 2 Then
Me.uiMsgLabel.Text = "February"
ElseIf month = 3 Then
Me.uiMsgLabel.Text = "March"
ElseIf month = 4 Then
Me.uiMsgLabel.Text = "April"
ElseIf month = 5 Then
Me.uiMsgLabel.Text = "May"
ElseIf month = 6 Then
Me.uiMsgLabel.Text = "June"
ElseIf month = 7 Then
Me.uiMsgLabel.Text = "July"
ElseIf month = 8 Then
Me.uiMsgLabel.Text = "August"
ElseIf month = 9 Then
Me.uiMsgLabel.Text = "September"
ElseIf month = 10 Then
Me.uiMsgLabel.Text = "October"
ElseIf month = 11 Then
Me.uiMsgLabel.Text = "November"
ElseIf month = 12 Then
Me.uiMsgLabel.Text = "December"
Else
MessageBox.Show("Please enter a number from 1 - 12", _
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

End If

End Sub

vanidosa27
February 22nd, 2006, 07:48 PM
Using the Case Selection structure

Private Sub uiIfButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiIfButton.Click
Dim month As Integer
month = Integer.Parse(Me.uiMonthCodeTextBox.Text)
Select Case month
Case 1
Me.uiMsgLabel.Text = "January"
Case 2
Me.uiMsgLabel.Text = "February"
Case 3
Me.uiMsgLabel.Text = "March"
Case 4
Me.uiMsgLabel.Text = "April"
Case 5
Me.uiMsgLabel.Text = "May"
Case 6
Me.uiMsgLabel.Text = "June"
Case 7
Me.uiMsgLabel.Text = "July"
Case 8
Me.uiMsgLabel.Text = "August"
Case 9
Me.uiMsgLabel.Text = "September"
Case 10
Me.uiMsgLabel.Text = "October"
Case 11
Me.uiMsgLabel.Text = "November"
Case 12
Me.uiMsgLabel.Text = "December"
Case Else
MessageBox.Show("Please enter a number from 1 - 12", _
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Select

End Sub

Thanks again Genius! Your the BOMB! LOL :)