OR operator trouble and confusion.

trueone55

New member
Joined
Feb 2, 2014
Messages
2
Programming Experience
Beginner
Hello Everyone,


First I'd like to mention that I am currently a networking/IT student and am currently taking an intro to programming class that is required as part of the curriculum. I am by no means a programmer so please forgive my ignorance.


I am currently working on an assignment that states the following
1. Get mobileProvider
2. Get dataOption
3. Set dataPackage to true or false
4. If mobileProvider is equal to 1 AND dataPackage is equal to True, then display they have selected ATT with a dataPackage.
5. Else if mobileProvider is equal to 2 OR dataPackage is equal to True, then display they have selected Sprint or they have selected with a Data Package.
6. Else if mobileProvider is equal to 3 AND dataPackage is equal to False, then display they have selected Verizon and No Data Package.


This algorithm is the 3rd and final step of a console program assignment. I "bolded" the part that confuses me the most.
Here is the code I have written so far (please be gentle).

VB.NET:
If mobileProvider = 1 And dataPackage = True Then

     Console.WriteLine("You have selected ATT with a Data Package")

   ElseIf mobileProvider = 2 Or dataPackage = True Then

     Console.WriteLine("You have selected Sprint")

   ElseIf mobileProvider = 3 And dataPackage = False Then

     Console.WriteLine("You have selected Verizon and No Data Package")

End If


Notice I have left out the second part that states "or they have selected with a Data Package" because I just can't figure out how to make it decide between which statement to display.


Thanks in advance for your tips and suggestions.
 
Last edited:
OR means either one or both can be true. If you use AND (as you did) then it means that both of them must be true. But what happens if only one of them is true? You should use separate ElseIf statements for each of the 2 options.
 
Ha... Oops! I was messing with that line and must have copied it when I changed it from Or to And. I've corrected it up top but the question still stands. Perhaps if I post the entire code you might better understand what's happening but I will still try to explain why I believe "then display they have selected Sprint or they have selected with a Data Package" are meant to be two separate statements.
If you look at the last ElseIf the assumption is that the user will select the right combination to produce the line "You have selected Verizon and No Data Package" but if the user were to select that they indeed wanted a Data Package it would then need to refer to mobileProvider() and display "You selected Verizon" and THEN come back to displayChoices() and select the second part of what is confusing me, which is "they have selected with a Data Package" which in turn would output:

"You have selected Verizon"
"With a Data Package"

Right now if I select option 3, which is Verizon and select option 1, which is yes I want a Data Package the output is

"You have selected Verizon"
"You have selected Sprint"


Does that make sense? Sorry if i'm making it more complicated or confusing than it needs to be.


Here is the code
VB.NET:
Module Module1


    Sub Main()
        'local Variables
        Dim mobileProvider As Integer = 0
        Dim dataOption As Integer = 0
        Dim dataPackage As Boolean = True


        Call inputOptions(mobileProvider, dataOption, dataPackage)
        Call displayProvider(mobileProvider)
        Call displayChoices(mobileProvider, dataOption, dataPackage)


        'pause
        Console.WriteLine("Press any key to continue...")
        Console.ReadLine()


    End Sub


    Sub inputOptions(ByRef mobileProvider As Integer, ByRef dataOption As Integer, ByRef dataPackage As Boolean)


        Console.WriteLine("Enter 1 for ATT, 2 for Sprint, and 3 for Verizon")
        mobileProvider = Console.ReadLine()
        Console.WriteLine("Enter 1 if you want a data package, or 2 if you do not")
        dataOption = Console.ReadLine()
        If dataOption = 1 Then
            dataPackage = True
        Else
            dataPackage = False
        End If


    End Sub


    Sub displayProvider(ByRef mobileProvider As Integer)


        Select Case mobileProvider
            Case 1
                Console.WriteLine("You selected ATT")
            Case 2
                Console.WriteLine("You selected Sprint")
            Case 3
                Console.WriteLine("You selected Verizon")
            Case Else
                Console.WriteLine("You did not select a valid option")
        End Select


    End Sub


    Sub displayChoices(ByVal mobileProvider As Integer, ByVal dataOption As Integer, ByRef dataPackage As Boolean)


        If mobileProvider = 1 And dataPackage = True Then
            Console.WriteLine("You have selected ATT with a Data Package")
        ElseIf mobileProvider = 2 Or dataPackage = True Then
            Console.WriteLine("You have selected Sprint")
        ElseIf dataPackage = False Then
            Console.WriteLine("with a Data Package")
        ElseIf mobileProvider = 3 And dataPackage = False Then
            Console.WriteLine("You have selected Verizon and No Data Package")
        End If


    End Sub




End Module
 
You shouldn't be using the Select Case block since it doesn't provide all the required And and Or conditions. That's why you're getting two messages when you should only be getting one.

Keep working on the If Then block, and transfer the Case Else statement to the If block, as an Else statement. Also, keep in mind that only ONE of the ElseIf statements will be executed, the first one that is True. I see from one of your statements that you assumed the string would be continued from the string in the previous statement, but that will never happen.
 
Back
Top