Jump to content


create if...elseif statement Visual basic

visual basic vb

  • Please log in to reply
58 replies to this topic

#16 Andre S.

Andre S.


  • Tech Issues Solved: 17
  • Joined: 26-October 05

Posted 24 August 2013 - 04:08

You didn't actually ask a question. You just posted your assignment. What are we supposed to help you with?


If you cannot be bothered to formulate a clear question, don't expect anyone to formulate a clear answer.

#17 OP soccerstar206



  • Joined: 19-August 13

Posted 24 August 2013 - 04:19

I need help creating that code

#18 adrynalyne


    .NET Developer

  • Tech Issues Solved: 1
  • Joined: 29-November 09

Posted 24 August 2013 - 04:31

If you can't figure out something this elementary, you need to start over. This time, at the beginning.

#19 Grinch



  • Tech Issues Solved: 5
  • Joined: 26-September 09
  • Location: Wisconsin
  • OS: Windows 10
  • Phone: Nokia Lumia 925

Posted 24 August 2013 - 04:32

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.

#20 Lord Method Man

Lord Method Man


  • Tech Issues Solved: 1
  • Joined: 18-September 12

Posted 24 August 2013 - 04:37

There isn't even a problem here. The question itself is literally the pseudocode for the solution.



Here, this is as much as I'm willing to provide:


#21 Andre S.

Andre S.


  • Tech Issues Solved: 17
  • Joined: 26-October 05

Posted 24 August 2013 - 05:31

I need help creating that code

That's not a question either. What is it you don't understand?

#22 Karl L.

Karl L.


  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 24 August 2013 - 05:45

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.


# 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;
        $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]);
    print "Syntax: $0 AGE_OF_CUSTOMER\n";

exit 0;

#23 seta-san


    Neowinian Senior

  • Joined: 17-February 05

Posted 24 August 2013 - 06:17

please do your own homework.

#24 jakem1


    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 17-November 06

Posted 24 August 2013 - 08:27

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


Why blame the teacher?

#25 GreenMartian


    Neowinian Senior

  • Joined: 28-August 04
  • Location: adelaide, au

Posted 24 August 2013 - 08:53

Why blame the teacher?

I guess sarcasm doesn't work too well in written form :p 

#26 68k


    Neowinian Senior

  • Tech Issues Solved: 5
  • Joined: 20-January 10
  • Location: Australia

Posted 24 August 2013 - 09:31

This will answer all your questions: http://msdn.microsof...y/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).

#27 68k


    Neowinian Senior

  • Tech Issues Solved: 5
  • Joined: 20-January 10
  • Location: Australia

Posted 24 August 2013 - 09:34

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

Just copy and paste, then modify, the examples at my link above.

#28 lunamonkey



  • Joined: 28-May 03
  • Location: Swindon, England

Posted 24 August 2013 - 10:11

He wanted it quick, but the time between posts was longer than it would have taken, like 2 minutes.

#29 FloatingFatMan


    Resident Fat Dude

  • Tech Issues Solved: 2
  • Joined: 23-August 04
  • Location: UK

Posted 24 August 2013 - 11:18

He wanted it quick, but the time between posts was longer than it would have taken, like 2 minutes.


Not even that... In fact, it would have taken longer to type up the OP than write the elseif clause he wanted...

#30 jake1eye



  • Joined: 02-April 03
  • Location: New Westminster

Posted 24 August 2013 - 16:55

Anyone who can not use If statements, case select, or for if loops should not

be in a programming class.