if statement

Discussion in 'Java' started by geletine, Nov 16, 2005.

  1. geletine

    geletine Guest

    I am a bigginner java programmer, thats obvious from the post, anyway i
    am trying out this experiment, the program asks the users name, there
    sex and there age.

    the name is nothing more than a name, there sex determines weather they
    can get a senior travel discount at 60 for females and 65 for males,
    the standard discount can also be given to anyone 16 or under, anyone
    older than 16 and younger than 60 cannot get a discount, anyone older
    than 150 is considered dead.

    I think i should be using two diffrent if statements, instead i have
    combinded them together.

    I start of by asking what there name is then there sex, if they are
    male it asks there age and should decided weather there are 65 and
    older to give them a senior bus card or under 65 but older than 16 to
    give them no discount, if there 16 and younger a travelcard discount
    and if they are over 150 there should be dead.

    If they are not male then it asks if they are 60 or over, if they are
    they get a senior travelcard if there 16 and younger a travelcard , if
    they older than 16 but younger than 60 no travelcard and lastly if
    there over 150 they should be dead.

    here is my first attempt, :

    import java.io.*;

    public class bus

    {



    public static void main (String args [])throws IOException

    {

    String name, input, male;

    int age;



    BufferedReader info = new BufferedReader ( new InputStreamReader (
    System.in ) );



    System.out.println ("Enter your name");

    input = info.readLine();

    name = input;



    System.out.println ("Are you male? ");

    input = info.readLine();

    male = input;



    System.out.println ("Enter your age");

    input = info.readLine();

    age = Integer.parseInt ( input );



    if ((male) || (age >= 65))

    {

    System.out.println ( name + " is entitled to a senior travelcard
    ");

    }



    else if ((male) || (age < 65))

    {

    System.out.println ( name + " is not entitled to a senior
    travelcard ");

    }



    else if ((male) || ( age >= 150))

    {

    System.out.println ( name + " , you are kidding, what are you on?
    ");

    }



    else if (age >= 150)

    {

    System.out.println ( name + " , you are kidding, what are
    you on ? ");

    }



    else if (age <= 16)

    {

    System.out.println ( name + " is entitled to a bus pass");

    }



    else if (age >= 60)
    {
    System.out.println (name + " is entitled to a senior bus
    pass ");
    }



    else

    {

    System.out.println ( name + " is not allowed a bus pass");

    }



    }

    }
    geletine, Nov 16, 2005
    #1
    1. Advertising

  2. geletine

    Roedy Green Guest

    On 16 Nov 2005 07:39:23 -0800, "geletine" <>
    wrote, quoted or indirectly quoted someone who said :

    >if ((male) |


    you declared male as a string, presumably to contain "Y" "y" "male"
    "M" or the like.

    And you tried to use it as a boolean. You need to write some code to
    convert from the string to the boolean.

    You might do it with a switch

    switch ( input.charAt(0) )
    {
    case 'y':
    case 'm':
    .....
    male = true;
    break;
    .....

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 16, 2005
    #2
    1. Advertising

  3. geletine

    J. Verdrengh Guest

    Have a look at this:


    import java.io.*;

    public class Bus{
    public static void main (String args []) throws IOException{
    BufferedReader info = new BufferedReader ( new InputStreamReader
    (System.in ) );

    System.out.println ("Enter your name");
    String name = info.readLine();

    System.out.println ("Are you male? ");
    String male= info.readLine();

    System.out.println ("Enter your age");
    int age = Integer.parseInt (info.readLine());

    bool isMale = male.equals("yes");
    int borderAge = -1;
    if (isMale){
    borderAge = 65;
    } else {
    borderAge = 60;
    }


    if (age <= 16) {System.out.println ( name + " is entitled to a bus
    pass");}
    else if(age>=borderAge && age<150) System.out.println ( name + " is
    entitled to a senior travelcard");
    else if(age<borderAge ) System.out.println ( name + " is not entitled
    to a senior travelcard ");
    else System.out.println ( name + " , you are kidding, what are you
    on?");
    }
    }
    J. Verdrengh, Nov 16, 2005
    #3
  4. geletine

    J. Verdrengh Guest

    > bool isMale = male.equals("yes");

    bool should be boolean
    J. Verdrengh, Nov 16, 2005
    #4
  5. geletine

    Roedy Green Guest

    On Wed, 16 Nov 2005 17:02:53 +0100, "J. Verdrengh" <>
    wrote, quoted or indirectly quoted someone who said :

    >int borderAge = -1;
    > if (isMale){
    > borderAge = 65;
    > } else {
    > borderAge = 60;
    > }


    Most java programmers would write that with the ternary operator as:

    int borderAge = isMale ? 65 : 60;
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 16, 2005
    #5
  6. On 2005-11-16, Roedy Green penned:
    > On Wed, 16 Nov 2005 17:02:53 +0100, "J. Verdrengh" <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>int borderAge = -1;
    >> if (isMale){
    >> borderAge = 65;
    >> } else {
    >> borderAge = 60;
    >> }

    >
    > Most java programmers would write that with the ternary operator as:
    >
    > int borderAge = isMale ? 65 : 60;


    I guess I'm not most java programmers. I find the if/else syntax much
    clearer. In fact, I just found a bug in someone else's code in which
    the second and third arguments to the ternary operator were reversed;
    I'd imagine the bug would have been more obvious if written using
    if/else blocks instead.

    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    Monique Y. Mudama, Nov 16, 2005
    #6
  7. geletine

    Oliver Wong Guest

    "Monique Y. Mudama" <> wrote in message
    news:...
    > On 2005-11-16, Roedy Green penned:
    >> On Wed, 16 Nov 2005 17:02:53 +0100, "J. Verdrengh" <>
    >> wrote, quoted or indirectly quoted someone who said :
    >>
    >>>int borderAge = -1;
    >>> if (isMale){
    >>> borderAge = 65;
    >>> } else {
    >>> borderAge = 60;
    >>> }

    >>
    >> Most java programmers would write that with the ternary operator as:
    >>
    >> int borderAge = isMale ? 65 : 60;

    >
    > I guess I'm not most java programmers. I find the if/else syntax much
    > clearer. In fact, I just found a bug in someone else's code in which
    > the second and third arguments to the ternary operator were reversed;
    > I'd imagine the bug would have been more obvious if written using
    > if/else blocks instead.


    Also, Java does some short circuiting with the ternary operator, so
    that:

    int borderAge = isMale ? getMaleBorderAge() : getFemaleBorderAge();

    will only result in one method call, which may or may not be the
    intention of the programmer (this is important if the methods have some sort
    of side effect). Rewriting this as an if-else-statement usually makes the
    intent explicit.

    - Oliver
    Oliver Wong, Nov 16, 2005
    #7
  8. geletine

    Roedy Green Guest

    On Wed, 16 Nov 2005 14:24:44 -0700, "Monique Y. Mudama"
    <> wrote, quoted or indirectly quoted someone who
    said :

    >> int borderAge = isMale ? 65 : 60;

    >
    >I guess I'm not most java programmers. I find the if/else syntax much
    >clearer. In fact, I just found a bug in someone else's code in which
    >the second and third arguments to the ternary operator were reversed;
    >I'd imagine the bug would have been more obvious if written using
    >if/else blocks instead.


    Objectively, the ternary form is clearer. There is less distracting
    bubblegum and redundancy. What you really mean is the ternary form is
    less familiar.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 16, 2005
    #8
  9. On 2005-11-16, Roedy Green penned:
    > On Wed, 16 Nov 2005 14:24:44 -0700, "Monique Y. Mudama"
    ><> wrote, quoted or indirectly quoted someone
    >who said :
    >
    >>> int borderAge = isMale ? 65 : 60;

    >>
    >>I guess I'm not most java programmers. I find the if/else syntax
    >>much clearer. In fact, I just found a bug in someone else's code in
    >>which the second and third arguments to the ternary operator were
    >>reversed; I'd imagine the bug would have been more obvious if
    >>written using if/else blocks instead.

    >
    > Objectively, the ternary form is clearer. There is less distracting
    > bubblegum and redundancy. What you really mean is the ternary form
    > is less familiar.


    Objectively, I think you're wrong. I really mean that I think the
    if/else syntax is clearer, just as I think that using curly braces and
    parentheses even when they are not required by the compiler can make the
    code clearer.

    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    Monique Y. Mudama, Nov 16, 2005
    #9
  10. Oliver Wong coughed up:
    > "Monique Y. Mudama" <> wrote in message
    > news:...
    >> On 2005-11-16, Roedy Green penned:
    >>> On Wed, 16 Nov 2005 17:02:53 +0100, "J. Verdrengh" <>
    >>> wrote, quoted or indirectly quoted someone who said :
    >>>
    >>>> int borderAge = -1;
    >>>> if (isMale){
    >>>> borderAge = 65;
    >>>> } else {
    >>>> borderAge = 60;
    >>>> }
    >>>
    >>> Most java programmers would write that with the ternary operator as:
    >>>
    >>> int borderAge = isMale ? 65 : 60;

    >>
    >> I guess I'm not most java programmers. I find the if/else syntax much
    >> clearer. In fact, I just found a bug in someone else's code in which
    >> the second and third arguments to the ternary operator were reversed;
    >> I'd imagine the bug would have been more obvious if written using
    >> if/else blocks instead.

    >
    > Also, Java does some short circuiting with the ternary operator, so
    > that:
    >
    > int borderAge = isMale ? getMaleBorderAge() : getFemaleBorderAge();
    >
    > will only result in one method call,


    *Same* as with the if/else.


    > which may or may not be the
    > intention of the programmer (this is important if the methods have some
    > sort
    > of side effect). Rewriting this as an if-else-statement usually makes the
    > intent explicit.


    Huh? Explicit how? The if/else *might* have been arranged differently, but
    then, so might the code containing the ?: ternary.


    --
    I've seen this a few times--Don't make this mistake:

    Dwight: "This thing is wildly available."
    Smedly: "Did you mean wildly, or /widely/ ?"
    Dwight: "Both!", said while nodding emphatically.

    Dwight was exposed to have made a grammatical
    error and tries to cover it up by thinking
    fast. This is so painfully obvious that he
    only succeeds in looking worse.
    Thomas G. Marshall, Nov 17, 2005
    #10
  11. Monique Y. Mudama coughed up:
    > On 2005-11-16, Roedy Green penned:
    >> On Wed, 16 Nov 2005 14:24:44 -0700, "Monique Y. Mudama"
    >> <> wrote, quoted or indirectly quoted someone
    >> who said :
    >>
    >>>> int borderAge = isMale ? 65 : 60;
    >>>
    >>> I guess I'm not most java programmers. I find the if/else syntax
    >>> much clearer. In fact, I just found a bug in someone else's code in
    >>> which the second and third arguments to the ternary operator were
    >>> reversed; I'd imagine the bug would have been more obvious if
    >>> written using if/else blocks instead.

    >>
    >> Objectively, the ternary form is clearer. There is less distracting
    >> bubblegum and redundancy. What you really mean is the ternary form
    >> is less familiar.

    >
    > Objectively, I think you're wrong. I really mean that I think the
    > if/else syntax is clearer, just as I think that using curly braces and
    > parentheses even when they are not required by the compiler can make the
    > code clearer.



    Objectively, I think you're wrong. There is far less clutter to express the
    same thing. The only reason I can see that someone might think that

    int borderAge = isMale ? 65 : 60;

    is somehow less clear than

    int borderAge;
    if (isMale)
    borderAge = 65;
    else
    borderAge = 60;

    is simply because they are less familiar with :? or otherwise somehow
    spooked by it.


    --
    I've seen this a few times--Don't make this mistake:

    Dwight: "This thing is wildly available."
    Smedly: "Did you mean wildly, or /widely/ ?"
    Dwight: "Both!", said while nodding emphatically.

    Dwight was exposed to have made a grammatical
    error and tries to cover it up by thinking
    fast. This is so painfully obvious that he
    only succeeds in looking worse.
    Thomas G. Marshall, Nov 17, 2005
    #11
  12. geletine

    Roedy Green Guest

    On Thu, 17 Nov 2005 02:35:23 GMT, "Thomas G. Marshall"
    <> wrote, quoted
    or indirectly quoted someone who said :

    > int borderAge = isMale ? 65 : 60;


    This is elegant. Every fact is specified only once with the absolute
    minimum of clutter.

    Perhaps having a way to speak it internally to yourself might help you
    appreciate it.

    I would say that to myself as

    int borderAge is isMale (question-inflection -- swoop up on word
    male) 65 otherwise 60


    The other form has redundancy:

    int borderAge;
    if (isMale)
    borderAge = 65;
    else
    borderAge = 60;


    you specify the borderAge fact 3 times. You must by eye notice that
    all three are ABSOLUTELY IDENTICAL otherwise the idiom is misleading
    e.g.

    int borderAge;
    if (isMale)
    borderAge = 65;
    else
    borderage = 60;

    Try an even longer variable name and you see the problem more clearly

    int x_coordinate_top_left;
    if (isMale)
    x_coordinatetop_left= 65;
    else
    x_coordinatetop_left= 60;
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 17, 2005
    #12
  13. geletine

    Roedy Green Guest

    On Thu, 17 Nov 2005 02:35:23 GMT, "Thomas G. Marshall"
    <> wrote, quoted
    or indirectly quoted someone who said :

    >Objectively, I think you're wrong. There is far less clutter to express the
    >same thing. The only reason I can see that someone might think that
    >
    > int borderAge = isMale ? 65 : 60;


    No matter which form you chose for your own code, you will run into
    both in other people's code, so it is important to become comfortable
    with both notations.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 17, 2005
    #13
  14. geletine

    Guest

    Thomas G. Marshall wrote:
    > Monique Y. Mudama coughed up:
    > > On 2005-11-16, Roedy Green penned:
    > >> On Wed, 16 Nov 2005 14:24:44 -0700, "Monique Y. Mudama"
    > >> <> wrote, quoted or indirectly quoted someone
    > >> who said :
    > >>


    >
    >
    > Objectively, I think you're wrong. There is far less clutter to express the
    > same thing. The only reason I can see that someone might think that
    >
    > int borderAge = isMale ? 65 : 60;
    >
    > is somehow less clear than
    >
    > int borderAge;
    > if (isMale)
    > borderAge = 65;
    > else
    > borderAge = 60;
    >
    > is simply because they are less familiar with :? or otherwise somehow
    > spooked by it.
    >


    FWIW, I think int borderAge = isMale ? 65 : 60; is more concise than
    the if-statement, and less clear, precisely because the if-statement is
    more like English, and so I would guess that all English speakers are
    more familiar with if-statements than ternary operators.

    I was in a nightclub last night: I went up to a girl and said, "Would
    you like to come back to my place ? Sure, why not : not if you were the
    last programmer on earth." She didn't reply. But she didn't throw an
    exception, either.

    ..ed

    --
    www.EdmundKirwan.com - Home of The Fractal Class Composition.
    , Nov 17, 2005
    #14
  15. geletine

    J. Verdrengh Guest

    Roedy says:
    > Most java programmers would write that with the ternary operator as:
    >
    > int borderAge = isMale ? 65 : 60;


    ok, but you may have forgotten to read the very first line in geletine's
    post: "I am a bigginner java programmer". I think your suggestion would have
    been more confusing then mine.

    The discussion about syntactic sugar is a discussion of tastes. I do believe
    you find = ? : clearer then if-then-else and I also believe Monique thinks
    the opposite. You cannot say in general which one is the clearest because
    you cannot evaluate "A is more clear then B" without *choosing* a definition
    for it. A definition could be "A is more clear then B iff A is more English
    then B", another definition could be "A is more clear then B iff A is
    shorter then B"

    Jeroen
    J. Verdrengh, Nov 17, 2005
    #15
  16. geletine

    Roedy Green Guest

    On Thu, 17 Nov 2005 11:46:39 +0100, "J. Verdrengh" <>
    wrote, quoted or indirectly quoted someone who said :

    >You cannot say in general which one is the clearest because
    >you cannot evaluate "A is more clear then B" without *choosing* a definition
    >for it. A definition could be "A is more clear then B iff A is more English
    >then B", another definition could be "A is more clear then B iff A is
    >shorter then B"


    My definition is "After training, which form can I, in the least
    amount of time, reliably determine what the code does."

    I suggest that if textbooks taught the ternary form first, people
    might decide the if form was more confusing. I think a fair bit of the
    preference for if is a reluctance to learn something new.

    You really have to become skilled in both before you can judge which
    truly is clearer.

    We have here in miniature a situation similar to people coming to
    Java from C who think every difference from C is a bug.

    It is also similar to the situation where people write stuttering
    boolean expressions with ==true. They claim they are clearer, but
    have not yet learned to write code without them. How do they know
    which is clearer (using my definition).

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 17, 2005
    #16
  17. geletine

    HalcyonWild Guest

    Roedy Green wrote:
    > On Thu, 17 Nov 2005 11:46:39 +0100, "J. Verdrengh" <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    > >You cannot say in general which one is the clearest because
    > >you cannot evaluate "A is more clear then B" without *choosing* a definition
    > >for it. A definition could be "A is more clear then B iff A is more English
    > >then B", another definition could be "A is more clear then B iff A is
    > >shorter then B"

    >
    > My definition is "After training, which form can I, in the least
    > amount of time, reliably determine what the code does."
    >
    > I suggest that if textbooks taught the ternary form first, people
    > might decide the if form was more confusing. I think a fair bit of the
    > preference for if is a reluctance to learn something new.
    >
    > You really have to become skilled in both before you can judge which
    > truly is clearer.


    ..... [something i deleted]....

    There has to be a compromise. The ternary operator is good for single
    statement if else blocks. But not when you have multiple statements,
    within if and else. Ternary is just a shortcut for if-else.
    For multi-statement blocks, if-else is a much better idea. Ternary
    would look confusing. Imagine searching for a colon in many lines of
    code, instead of a well indented if - else blocks.
    HalcyonWild, Nov 17, 2005
    #17
  18. geletine

    J. Verdrengh Guest

    >My definition is "After training, which form can I, in the least
    >amount of time, reliably determine what the code does."

    Ok, but this definition is subjective: depending on the person using it, the
    outcome may be different. Thus it can't be used for absolute decisions about
    which form is clearer.
    J. Verdrengh, Nov 17, 2005
    #18
  19. Roedy Green coughed up:
    > On Thu, 17 Nov 2005 02:35:23 GMT, "Thomas G. Marshall"
    > <> wrote, quoted
    > or indirectly quoted someone who said :
    >
    >> int borderAge = isMale ? 65 : 60;

    >
    > This is elegant. Every fact is specified only once with the absolute
    > minimum of clutter.
    >
    > Perhaps having a way to speak it internally to yourself might help you
    > appreciate it.


    REREAD what I wrote. I advocate the :? !!!

    ....[misdirected examples snipped]...


    --
    It's time for everyone to just step back, take a deep breath, relax, and
    stop
    throwing hissy fits over crossposting.
    Thomas G. Marshall, Nov 17, 2005
    #19
  20. coughed up:
    > Thomas G. Marshall wrote:
    >> Monique Y. Mudama coughed up:
    >>> On 2005-11-16, Roedy Green penned:
    >>>> On Wed, 16 Nov 2005 14:24:44 -0700, "Monique Y. Mudama"
    >>>> <> wrote, quoted or indirectly quoted someone
    >>>> who said :
    >>>>

    >
    >>
    >>
    >> Objectively, I think you're wrong. There is far less clutter to express
    >> the
    >> same thing. The only reason I can see that someone might think that
    >>
    >> int borderAge = isMale ? 65 : 60;
    >>
    >> is somehow less clear than
    >>
    >> int borderAge;
    >> if (isMale)
    >> borderAge = 65;
    >> else
    >> borderAge = 60;
    >>
    >> is simply because they are less familiar with :? or otherwise somehow
    >> spooked by it.
    >>

    >
    > FWIW, I think int borderAge = isMale ? 65 : 60; is more concise than
    > the if-statement, and less clear, precisely because the if-statement is
    > more like English, and so I would guess that all English speakers are
    > more familiar with if-statements than ternary operators.
    >
    > I was in a nightclub last night: I went up to a girl and said, "Would
    > you like to come back to my place ? Sure, why not : not if you were the
    > last programmer on earth." She didn't reply. But she didn't throw an
    > exception, either.


    Point. LOL. But this just speaks to being familiar with it, though you are
    correct in that if you are not familiar with it the english in your head
    will not help you much.



    --
    It's time for everyone to just step back, take a deep breath, relax, and
    stop
    throwing hissy fits over crossposting.
    Thomas G. Marshall, Nov 17, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    805
  2. Jay McGavren
    Replies:
    11
    Views:
    1,133
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,083
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    464
    Duncan Booth
    Jul 22, 2004
  5. Replies:
    21
    Views:
    1,055
    Giannis Papadopoulos
    Aug 2, 2005
Loading...

Share This Page