if statement

G

geletine

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");

}



}

}
 
R

Roedy Green

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

J. Verdrengh

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?");
}
}
 
R

Roedy Green

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;
 
M

Monique Y. Mudama

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

Oliver Wong

Monique Y. Mudama said:
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
 
R

Roedy Green

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

Monique Y. Mudama

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

Thomas G. Marshall

Oliver Wong coughed up:
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.
 
T

Thomas G. Marshall

Monique Y. Mudama coughed up:
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.
 
R

Roedy Green

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;
 
R

Roedy Green

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

iamfractal

Thomas said:
Monique Y. Mudama coughed up:
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
 
J

J. Verdrengh

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
 
R

Roedy Green

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

HalcyonWild

Roedy said:
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.
 
J

J. Verdrengh

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

Thomas G. Marshall

Roedy Green coughed up:
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]...
 
T

Thomas G. Marshall

(e-mail address removed) coughed up:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top