• 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: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

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

  • 0
  On 24/08/2013 at 10:11, lunamonkey said:

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

  • 0
  On 24/08/2013 at 16:55, jake1eye said:

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

be in a programming class.

Programming classes are there to teach, are they not? 

 

Although I think if someone has trouble with something this simple, they might want to take up another skill. Like knitting. 

  • 0
  On 24/08/2013 at 23:11, adrynalyne said:

Programming classes are there to teach, are they not? 

 

Although I think if someone has trouble with something this simple, they might want to take up another skill. Like knitting. 

 

Yep, I learned LONG ago that programming was not for me.

  • 0

10 price = 7.50
20 if age > 62 then price = 5.50
30 if age < 20 then price = 4.50
40 if age < 12 then price = 3.50
50 if age < 6 then price = 0

 

Wait, was this supposed to be in Commodore 64 BASIC?

 

(Yes, this is a horrible example of coding, it's deliberate)

  • 0

Some more examples for your education:

 

REXX

/* REXX */
arg intAge .

select
when intAge <   6 then decTicketPrice = 0
when intAge <= 12 then decTicketPrice = 3.5
when intAge <= 19 then decTicketPrice = 4.5
when intAge >  62 then decTicketPrice = 5.5
otherwise              decTicketPrice = 7.5
end

return decTicketPrice

Cobol

01 DECTICKETPRICE			PIC S9(5)V99 COMP-3.
01 INTAGE				PIC 9(3) COMP-3.

EVALUATE TRUE
WHEN INTAGE <  6
	MOVE ZERO TO DECTICKETPRICE
WHEN INTAGE <= 12
	MOVE 3.5  TO DECTICKETPRICE
WHEN INTAGE <= 19
	MOVE 4.5 TO DECTICKETPRICE	
WHEN INTAGE >  62 
	MOVE 5.5 TO DECTICKETPRICE
WHEN OTHER
	MOVE 7.5 TO DECTICKETPRICE
END-EVALUATE.

Easytrieve Plus (v6.4 up)

DECTICKETPRICE				W	4 P 2
INTAGE					W	2 P

IF      INTAGE LT  6
	DECTICKETPRICE = 0
ELSE-IF INTAGE LE 12
	DECTICKETPRICE = 3.5
ELSE-IF INTAGE LE 19
	DECTICKETPRICE = 4.5
ELSE-IF INTAGE GT 62
	DECTICKETPRICE = 5.5
ELSE
	DECTICKETPRICE = 7.5
END-IF
  • 0
  On 24/08/2013 at 22:53, soccerstar206 said:

Thanks for all the help guys! Every one of your helpful comments contributed to me getting an A on the assignment!

 

Now there's a misrepresented grade if ever I saw one.

  • 0

Tbh it seems the OP didn't even try to do this.

if you pseudocode what is required for this you have pretty much written the code....especially considering its to be written in vb.

if x = x then

y = y

else

if x = z then

y = y1

else

'etcetceyc

if you are doing a programming course OP and your posting like this.......you wont get far im sorry.

  • 0
  On 25/08/2013 at 00:48, Joe USer said:

Needs more GOTO's.

 

Challenge accepted!

 

REXX

/* REXX */
arg intAge .

/* There is no 'GOTO' in REXX. The statement to use is 'SIGNAL'					 */
/* This is the unconditional form of 'SIGNAL ON' that is used to catch exceptions	*/

if intage < 6 then do
		decticketprice = 0
		signal my_ass
	end
  
if intage <= 12 then do
		decticketprice = 3.5
		signal my_ass
	end
	
if intage <= 19 then do
		decticketprice = 4.5
		signal my_ass
	end
	
if intage > 62 then do
		decticketprice = 5.5
		signal my_ass
	end
	
decticketprice = 7.5

my_ass.

return decticketprice

Cobol

01 DECTICKETPRICE			PIC S9(5)V99 COMP-3.
01 INTAGE					PIC 9(3) COMP-3.

SOME-BOLLOCKS-CODE SECTION.

	IF INTAGE < 6
		MOVE ZERO TO DECTICKETPRICE
		GO TO MY-ASS
	END-IF.
	
	IF INTAGE <= 12
		MOVE 3.5  TO DECTICKETPRICE
		GO TO MY-ASS
	END-IF.
	
	IF INTAGE <= 19
		MOVE 4.5 TO DECTICKETPRICE	
		GO TO MY-ASS
	END-IF.
	
	
	IF INTAGE > 62 
		MOVE 5.5 TO DECTICKETPRICE
		GO TO MY-ASS
	END-IF.

	MOVE 7.5 TO DECTICKETPRICE.

MY-ASS.
	EXIT.

Easytrieve Plus

DECTICKETPRICE				W	4 P 2
INTAGE						W	2 P

SOME-BOLLOCKS-CODE. PROC

	IF INTAGE LT 6
		DECTICKETPRICE = 0
		GO TO MY-ASS
	END-IF

	IF INTAGE LE 12
		DECTICKETPRICE = 3.5
		GO TO MY-ASS
	END-IF
	
	IF INTAGE LE 19
		DECTICKETPRICE = 4.5
		GO TO MY-ASS
	END-IF
	
	IF INTAGE GT 62
		DECTICKETPRICE = 5.5
		GO TO MY-ASS
	END-IF

	DECTICKETPRICE = 7.5

MY-ASS.

END-PROC
  • Like 2
  • 0

Challenge accepted (again)!

Echo off
set intAge=%1%
if %intAge% LSS  6 (
	SET decTicketPrice=0
	goto done
)
if %intAge% LEQ 12 (
	SET decTicketPrice=3.5
	goto done
)
if %intAge% LEQ 19 (
	SET decTicketPrice=4.5
	goto done
)
if %intAge% GTR 62 (
	SET decTicketPrice=5.5
	goto done
)
set decTicketPrice=7.5

:done
Echo Ticket price is %decTicketPrice%
  • Like 2
  • 0
  On 24/08/2013 at 03:17, soccerstar206 said:

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

All values of age were covered in the criteria; so no need for any 'else' statements.

 

But if you insist:

 

float decTicketPrice = 7.50;   // default ticket price

if (intAge < 6) decTicketPrice = 0.00 // under 6 are free

     else decTicketPrice = 3.50; // 6 and over base price is 3.50

 

/* The above statement just screwed up the teacher's requirement */

/* of an 'otherwise' ticket price since the customer is either under 6 */

/* or 6 or above. Dumb teacher giving dumb assignment fails.         */

 

if (intAge > 12) decTicketPrice += 1.00; // over 12 adds a dollar

if (intAge > 62) decTicketPrice += 1.00; // over 62 adds another dollar

  • 0
  On 29/08/2013 at 21:56, abecedarian paradoxious said:

All values of age are covered; no need for any 'else' statements.

 

float decTicketPrice = 0.00;

if (intAge >= 6) decTicketPrice += 3.50;

if (intAge >= 12) decTicketPrice += 1.00;

if (intAge >= 62) decTicketPrice += 1.00;

VB doesn't use semicolons to end a line ;)

  • 0
  On 29/08/2013 at 21:56, abecedarian paradoxious said:

All values of age are covered; no need for any 'else' statements.

 

float decTicketPrice = 0.00;

if (intAge >= 6) decTicketPrice += 3.50;

if (intAge >= 12) decTicketPrice += 1.00;

if (intAge >= 62) decTicketPrice += 1.00;

 

Doesn't cover the $7.50 cost if they are between 19 and 62.

  • 0
  On 29/08/2013 at 21:59, Lord Method Man said:

Doesn't cover the $7.50 cost if they are between 19 and 62.

Okay.

unsigned int intAge

// should be unsigned because negative ages aren't possible

// char is also permissible since people aren't likely to be > 255 years old

 

float decTicketPrice = 7.50   // default ticket price

 

if (intAge < 6) decTicketPrice = 0.00 // under 6 and you're in for free!

if (intAge > 5) and (intAge < 13) decTicketPrice += -4.00 // nice discount.

if (intAge > 12) and (intAge < 20) decTicketPrice += -3.00 // decent discount

if (intAge > 62) decTicketPrice += -2.00 // why the hate for senior citizens?

 

 

Still, no real reason for "else" conditions.

Start with the default and go.

  • 0
  On 29/08/2013 at 22:25, abecedarian paradoxious said:

Okay.

 

float decTicketPrice = 7.50

if (intAge <6 ) decTicketPrice = 0.00

if (intAge >5 ) and (intAge < 13) decTicketPrice += -4.00

if (intAge > 12) and (intAge <20) decTicketPrice += -3.00

if (intAge > 62) decTicketPrice += -2.00

 

 

Still, no real reason for "else" conditionals.

The reason is the assignment called for it.

 

Assignments are rarely designed to be the most efficient, but rather to teach a concept.

 

Not that it matters, the OP is clueless and wanted a handout.  And got it.

  • 0
  On 29/08/2013 at 22:31, adrynalyne said:

The reason is the assignment called for it.

 

Assignments are rarely designed to be the most efficient, but rather to teach a concept.

 

Not that it matters, the OP is clueless and wanted a handout.  And got it.

True, I suppose.

I always received 'extra credit' for thinking beyond the assignment though.

  • 0
  On 29/08/2013 at 22:40, abecedarian paradoxious said:

True, I suppose.

I always received 'extra credit' for thinking beyond the assignment though.

Heh, where I went, the instructors weren't allowed to give you extra credit.

This topic is now closed to further replies.
  • Posts

    • "Let's antagonize them more so they'll be less likely to invade us" Good logic there.
    • Samsung One UI 8 Watch beta program goes live in Korea and the U.S, for eligible devices by Sagar Naresh Bhavsar After launching the Android 16-based One UI 8 beta program for the Galaxy S25 series, Samsung has also kicked off the One UI 8 Watch beta program for eligible Galaxy smartwatches. Notably, the beta program is live for Galaxy Watch users in the U.S. and Korea through the Samsung Members app. The new One UI 8 Watch introduces a bunch of new health features, which Samsung says are to "help users build healthier habits." New features include Bedtime Guidance, Vascular Load, Running Coach, and Antioxidant Index. Here's what each feature does: Bedtime Guidance It recommends Galaxy Watch users the best time they can get a good sleep based on their recent sleep patterns. This feature could be helpful for those who have a hard time having a good asleep. To recommend the best sleep patterns, the Bedtime Guidance uses sleep data from the past three days and analyzes metrics such as sleep pressure and circadian rhythm. Sleeping on the recommended time may help users recover from irregular schedules and sleep patterns. Vascular Load Using this feature, the One UI 8 Watch-powered Galaxy smartwatches will measure the amount of stress your heart and blood vessels experience during sleep. It is one of the key indicators of heart health, because if the vascular load shows excessive fluctuations, then it could be an indicator of an underlying cardiovascular issue. Running Coach Samsung has also added a Running Coach feature with the One UI 8 Watch. It gives users a personalized running program based on their fitness levels. The user needs to run for 12 minutes for the Galaxy Watch to register and analyze certain metrics and present a performance score. Based on the score, the Running Coach will present a tailored plan to help them safely reach and work up to marathon levels. Antioxidant Index The Antioxidant Index measures the carotenoid levels, which are antioxidants found inside green and orange fruits and vegetables that are inside your skin, meant to fight aging and cell damage. With One UI 8 Watch, the Galaxy Watch will make use of a light-based sensor to scan the skin and, in five seconds, will show a report on how their eating habits are paying off. Eligible devices Beta program is available for owners of Galaxy Watch5 or later in the U.S. and South Korea. However, not all features will be available on all supported Galaxy Watch models. Here are the details: Bedtime Guidance: Available on Galaxy Watch5 series or later, requires Android phone running Android 11 or later and Samsung Health app v6.30.2 or later. Vascular Load: Supported on Galaxy Watch Ultra or later, requires Android 10 or later, and Samsung Health app v6.30.2 or later. Running Coach: Requires Galaxy Watch7 or later, Android 10 or later, and Samsung Health app v6.30.2 or later. Antioxidant Index: Supported on Galaxy Watch Ultra or later, Android 10 or later, and Samsung Health app v6.30.2 or later. How to join beta program If you own a Galaxy Watch5 series or later model, then you can head over to the Samsung Members app > navigate to the bottom of the page > tap on the Watch Beta poster > and enroll for the beta prorgam. Do note that their are limited seats available.
    • I disabled the optical camera in device manager.. leaving only the IR. This has fixed the issue for me...but only because I never use the optical camera. After each monthly update re-enable the optical just to see if it's fixed...but nope! It's annoying though how this issue hasn't been acknowledged by Microsoft.
    • Linux is a different kettle of fish to macOS and Windows, if it ran the software I required, I may have looked at it, instead of the Mac, also the Mac is a pretty powerful machine that uses less energy than x86 machines. I never in my widest dreams thought I would ever buy a Mac, the price and restrictions of the hardware, I always liked machines that I could update internally, one reason why I never liked laptops. But here I am, a nice little Mac mini M2 pro. I doubt i will replace it for a long time, if I ever do, it does what I need.
    • 106 years ago! A comic strip from 1919 predicted — eerily and accurately — what would happen if our phones fit into our pockets.  W. K. Haselden’s ‘The Pocket Telephone: When Will it Ring?’ was published in “The Mirror” when barely 1/3rd of American homes even had telephones. (A double irony: most of us are viewing this on our “pocket phones”.)
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      640
    2. 2
      ATLien_0
      277
    3. 3
      +FloatingFatMan
      172
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      132
  • Tell a friend

    Love Neowin? Tell a friend!