-3

I have almost done the following using if else expression.But I want to code it using if operator.Consider the following data of several ages and I have to print the appropriate age using vb.net 2008.

if age between 0-12 then "child", if age between 13-19 then "teen", if age between 19-35 then "youth", else adult.

Juan
  • 3,433
  • 29
  • 32
E Kishni
  • 1
  • 1
  • 2
    Note that there is a difference between an [If Statement](https://msdn.microsoft.com/en-us/library/752y8abs.aspx) used to control flow and the [If Operator](https://msdn.microsoft.com/en-us/library/bb513985.aspx) which evaluates a condition to make an assignment. – Ňɏssa Pøngjǣrdenlarp Mar 08 '15 at 01:58

2 Answers2

3

It sounds like a Select Case statement might be more appropriate; especially so if changes to the age brackets may be coming in the future.

Dim age as long = 11
Select Case age
    Case 0 To 12
        Debug.WriteLine("0-12 - Child, inclusive")
    Case 13 To 19
        Debug.WriteLine("13-19 Teen, inclusive")
    Case 20 To 35
        Debug.WriteLine("20-35 - Youth, inclusive")
    Case Else
        Debug.WriteLine("Older than 35 - Adult")
End Select

You need to specify your age ranges more succinctly unless you are using a hierarchy of criteria. Is 19 a Teen or a Youth? By your narrative it is both unless the Youth criteria is not evaluated once Teen matches positive.

  • 19 is teen but I need this to be coded using if operator other than using select case statement. – E Kishni Mar 08 '15 at 02:53
  • @EKishni - If you want to code it in a less-than-best method then you must be within the strictures of a homework assignment. Edit your question to show what you have tried so far and more help may be forthcoming. –  Mar 08 '15 at 03:07
  • @Jeeped: You should get the teacher's salary if you're going to teach this joker better than the actual teacher. – Sam Axe Mar 08 '15 at 06:09
  • In fact, how about we make a new tag, `homework`, and we can all get paid for doing other people's homework. :) – Sam Axe Mar 08 '15 at 06:12
  • @TheBlueDog: I know it's difficult to see the sarcasm on my comments.. pretend its there. (e.g. I agree with you). – Sam Axe Mar 08 '15 at 07:55
  • 1
    @Dan-o: Yeah, I got the sarcasm, I was just using is as an excuse for a rant. :D – The Blue Dog Mar 08 '15 at 07:58
-1

For If operator go this link

Dim s() As String = {0, 1, 2 and so on }
If s.Contains(Age) Then
  'Go      
End If

i hope this help for your project ......:-)

Community
  • 1
  • 1
Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37