• 0

create if...elseif statement Visual basic


Question

Write an If..ElseIf  statement (or Select Case if you prefer) given the following:

 

decTicketPrice                    ?ticket price

intAge                                  ?age of customer   

 

If customer's age is less than 6, ticket price is Free

 

If customer's age is 6 to 12, ticket price is $3.50

 

If customer's age is 13 to 19, ticket price is $4.50

 

If customer's age is over 62, ticket price is $5.50

 

Otherwise, ticket price is $7.50

Recommended Posts

  • 0

Do the teachers these days throw assignments at students without teaching them anything, nor equipping them with textbooks?

I'm sure they do, but the student would need to pay attention...But, why pay attention when all you have to do is ask on a forum?

  • 0

Google would show you quite clearly how to do If and ElseIf statements in VB.NET just so you know. MSDN is a great resource as well, I came to learn this rather quickly. As for your question;

http://msdn.microsoft.com/en-us/library/752y8abs.aspx

  • 0

what we need to start doing is writing the most convoluded and syntatically poor way of answering the homework questions as possible.  And whenever someone asks for help we need to write something that the teacher would look at and say WTF.

  • 0

I do know how to do them I just need to do them quickly and I take awhile when I write my code

Wouldn't it be better to learn how to do it more quickly yourself rather than having others write it up for you? How do you intend to function in job that revolves around this language? You can't just turn to your co-worker and say "Hey could you write up this elseif statement for me?".

  • Like 3
  • 0

Wouldn't it be better to learn how to do it more quickly yourself rather than having others write it up for you? How do you intend to function in job that revolves around this language? You can't just turn to your co-worker and say "Hey could you write up this elseif statement for me?".

 

Im taking this class for fun and credits

  • 0

Im taking this class for fun and credits

Either way you should still learn to do it yourself. No one is going to write up your code for you. In the time you've sat here and tried to get someone to do so you should have been able to write it up yourself which even if it is slow, you improve with each time you do it. It becomes more second nature to you.

Ask to learn, not to be lazy.

  • 0

It's also important to remember that when you post a question you are asking volunteers to spend their free time trying to help you. Most of the time people don't mind doing that - it is why the majority of us joined Neowin after all - but posting mundane questions that you could easily lookup in your text book or elsewhere online feels like abusing those same people's good will. Not to mention the fact that posting many of this type of question in a short time period is likely to burn people out. As others have already offered, I would be happy to offer my advice to help you solve, and more importantly understand, a nontrivial programming problem, but just seeing the sheer volume of these threads you have posted in the last week has already made me leery of investing my time.

  • 0

I need help creating that code

You clearly are not trying to help yourself in that regard. You should be attempting it yourself instead of getting others to write it up. If you really hit a roadblock and not a physical (lazyness) one then feel free to post a question.

  • 0

Your problem seems to be primarily with programming logic - which is not specific to any language - and very little with VB syntax. Nothing will help you more than reading the documentation and working through the logic yourself. Once you do that a few times you will start to develop an aptitude and be able to write programs much more quickly. Although you will likely need to learn more useful languages at some point in your programming career, probably sooner rather than later, this course is trying to teach you how to program much more than Visual BASIC syntax. If you can get into the programming mindset - the how to program I just mentioned - learning the syntax for other languages becomes much easier. Ask us specific questions when you get stuck, but not just for whole answers to your homework problems. Like I am trying to explain, that won't help you in the long run anyway.

 

The question of syntax for this specific problem is more-or-less explicitly solved by the MSDN article Lord Method Man linked to a couple posts above mine. As for the logic, I have already stated that I don't believe directly giving you the answer will help you learn. Not only that, but I don't know Visual BASIC, nor do I have any intention of learning it. As a compromise I implemented the program you need in Perl. It is heavily commented to explain my logic, and you will need to think it through, at least to some degree, to combine it with the MSDN instructions above to create a valid Visual BASIC program. I hope that helps.

#!/usr/bin/perl

# Given age of a customer print the ticket price.
#
# Arguments:
#   age [in]        Age of customer
#
# Return Value:
#   The price of the customer's ticket will be returned.
sub ticket_price_by_age
{
    # Declare variables used in this function along with a succinct description of each.
    # This is not strictly necessary, but it is generally considered good practice.
    my $age; # Age of the customer
    my $price; # Price of the customer's ticket
    
    # Check to make sure that the customer's age was given as an argument,
    # and that the argument makes sense. If no age was given or the age is not
    # a natural number we need to exit the script with a brief error message.
    $age = $_[0] or die "Customer's age must be given!\n";
    $age =~ /^[0-9]+$/ or die "Customer's age must be a positive integer!\n";
    
    # Use a simple if..else statement to implement the core logic of the script.
    # Since each statement is evaluated in order, it makes sense to evaluate the
    # customer's age from least to greatest so we only need to implement one
    # check per branch. For example, when evaluating the second if statement
    # ("elsif ($age <= 12)") we know that the customer is at least 7 years old
    # because we would not have reached this statement if that were not the case.
    # Similarly we know that the customer is at least 13 years old when we reach
    # the third if statement ("elsif ($age <= 19)") for the same reason.
    if ($age <= 6)
    {
        $price = 0.0;
    }
    elsif ($age <= 12)
    {
        $price = 3.50;
    }
    elsif ($age <= 19)
    {
        $price = 4.50;
    }
    elsif ($age <= 62)
    {
        $price = 7.50;
    }
    else
    {
        $price = 5.50;
    }
    
    # Display the price of the customer's ticket on the screen.
    #
    # Digression: There are a few things in this statement that are specific to
    # Perl syntax, and therefore probably make it somewhat less clear from a VB
    # programmer's perspective. The "\$" is just escaping the "$" so only "$"
    # will print. The sprintf() function is used to print two decimal places in
    # the price, as is customary for fiat currency. The "." on either side of the
    # sprintf() is merely concatenating it as part of the string being passed
    # to the print() function.
    #
    # The important thing to take away from this statement is that something
    # like the following will be displayed on screen:
    #   Customer's ticket price: $4.50
    print "Customer's ticket price: \$" . sprintf("%.2f", $price) . "\n";
    
    # The return value is not actually used in this script.
    # You can safely ignore it (or remove it) if you so choose.
    return $price;
}

# Take the customer's age as a command line argument.
# If an age is specified call the ticket_price_by_age() function,
# otherwise print basic invocation syntax for the script.
if ($ARGV[0])
{
    ticket_price_by_age ($ARGV[0]);
}
else
{
    print "Syntax: $0 AGE_OF_CUSTOMER\n";
}

exit 0;
  • Like 1
  • 0

This will answer all your questions: http://msdn.microsoft.com/en-us/library/752y8abs.aspx I suggest you use this site as your primary reference/resource. In an exam, you may not even have access to documentation, so it's worth learning code off by heart! The best way to learn is by troubleshooting.

 

You're fortunate with how easy Visual Basic has become - if you were taking a course 15-20 years ago, you wouldn't have all the in-app support there is today (or the internet).

This topic is now closed to further replies.