• 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
  On 24/08/2013 at 03:20, GreenMartian said:

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
  On 24/08/2013 at 03:24, Lord Method Man said:

https://www.neowin.net/forum/topic/1172793-help-with-writing-code-with-arrays/#entry595899343

 

Are you going to post all of your homework assignments here?

 

nah just the ones I need help on

  • 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
  On 24/08/2013 at 03:30, soccerstar206 said:

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
  On 24/08/2013 at 03:33, Grinch said:

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
  On 24/08/2013 at 03:34, soccerstar206 said:

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.

  • Like 4
  • 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
  On 24/08/2013 at 04:19, soccerstar206 said:

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.
  • Posts

    • Actually, when Windows came to "play" decades ago, they never left. Play took over the Operating System to the detriment of business of serious consumer users. Made worse, in the Microsoft removed the option to have a "non-play oriented version of the OS" (as in a separate SKU).
    • ".....hellbent on shoving Copilot down the throats ..." Not great journalism IMHO. Put it in an opinion piece. Besides it's cliché.
    • Well that should last us until Sunday, December 4, 292277026596 at 15:30:08.
    • Windows 11 build 27909 is out with fixes for battery indicator and more by Taras Buria This week's Friday Windows 11 preview build comes from the Canary Channel with a few fixes here and there. No new features or noticeable changes in build 27909, so Windows 11 Insiders get to test several fixes and general improvements. Here is the changelog: [General] This update includes a small set of general improvements and fixes that improve the overall experience for Insiders running this build on their PCs. [Administrator Protection] Fixed an underlying issue where the Xbox app wouldn’t launch when administrator protection was enabled. This may have also impacted other apps too, showing error 0xC0000142 or 0xC0000045. [Settings] Fixed an issue where the battery percentage was unexpectedly missing from the top of System > Power & Battery in the last few builds. [Remote desktop] Fixed an issue causing extreme graphical distortion and rendering issues using remote desktop on Arm64 PCs in the last couple builds. [Other] Fixed a high hitting pcasvc.dll crash in the previous build. Fixed an underlying issue where if you disconnected the device you were casting to from outside of Media Player, Media Player would still show an option to disconnect from the device. Fixed an issue which was causing Remote Credential Guard scenarios between the latest Windows 11 builds and Server 2022 (and below) to fail. The list of known issues includes the following: [General] [IMPORTANT NOTE FOR COPILOT+ PCs] If you are joining the Canary Channel on a new Copilot+ PC from the Dev Channel, Release Preview Channel or retail, you will lose Windows Hello pin and biometrics to sign into your PC with error 0xd0000225 and error message “Something went wrong, and your PIN isn’t available”. You should be able to re-create your PIN by clicking “Set up my PIN”. There’s an issue starting with the latest builds causing a small number of Insiders to experience repeated bugchecks with KERNEL_SECURITY_CHECK_FAILURE after upgrading.This may occur when connecting to VPN. This Canary Channel flight comes with a delightful blast from the past and will play the Windows Vista boot sound instead of the Windows 11 boot sound. The fix should be coming in a future Canary Channel flight soon. [Settings] We’re investigating an issue in this build which could cause Settings to crash when interacting with the options under Settings > System > Power & Battery. We’re investigating an issue where some of the apostrophes across text in Settings and settings-related dialogs are not displaying correctly and are showing random characters. You can find the announcement post here.
    • And they didn't use Clippy for that? What a waste
  • Recent Achievements

    • Week One Done
      CyberCeps666 earned a badge
      Week One Done
    • Very Popular
      d4l3d earned a badge
      Very Popular
    • Dedicated
      Stephen Leibowitz earned a badge
      Dedicated
    • Dedicated
      Snake Doc earned a badge
      Dedicated
    • One Month Later
      Philsl earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      629
    2. 2
      ATLien_0
      240
    3. 3
      Xenon
      163
    4. 4
      neufuse
      124
    5. 5
      +FloatingFatMan
      124
  • Tell a friend

    Love Neowin? Tell a friend!