Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Here's a snippet from my code...
Boolean Add;
Double Answer;
Add = false;
if (Add == true);
{
Answer = (3 + 6);
System.out.print(Answer);
}
What baffles me is that at the end of my code it keeps evaluating and printing 3 + 6 even though the code is specifically geared to make it NOT show up. Any help would be appreciated.
EDIT: Thanks everyone
You have a semicolon at the end of your if condition. Java will treat that as the body for your if condition. The block with braces below it is detached from the if condition and will always run.
Remove the semicolon, and Java will interpret the block with braces as the body for your if statement.
Change
if (Add == true);
to
if (Add == true) // no semicolon
Additionally, Add is already a boolean value (a Boolean unboxed to a boolean). You can just say:
if (Add)
Remove ; after if condition.
if (Add == true);//<-here
If you format this code you will get
if (Add == true)
;
which is the same as placing empty block after if
if (Add == true)
{}//empty block
so this block will always execute because it is not actually no longer related to if condition
{
Answer = (3 + 6);
System.out.print(Answer);
}
BTW == true part is redundant in condition. You can just write it as if (Add)
You're mixing primitive types with reference types (not a problem really, but a bad practice nevertheless), and more importantly, there's an extra semicolon after the condition!. Try this:
boolean add;
double answer;
add = false;
if (add == true) // remove the ; from here
{
answer = 3 + 6;
System.out.print(answer);
}
That seemingly inoffensive ; was the cause of the problem. If you add a ; after the condition, you're stating: here's the end of the condition, there won't be any instructions inside. The rest of the code, the part that's inside {} runs inside a block, but will always be executed - it's outside the if statement.
You have a semicolon immediately after if so it is not executed. Change:
if (Add == true);
to:
if (Add == true)
Also you should consider using boolean primitive instead of Boolean class.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I hope my question was worded correctly.. here is a snippet of code, if I uncomment the line System.out.println(hs.add(ar)); console will print true, so why are reaching inside the following if statement?
public static void duplicateExists(String [] array)
{
Set<String> hs = new HashSet<String>();
for (String ar : array)
{
// System.out.println(hs.add(ar));
if((hs.add(ar)) == false);
{
System.out.println("reaches here every time but shouldn't ");
}
}
}
public static void main(String[] args) {
duplicateExists(new String[] {"1","2","5","3","6","8"});
}
This line:
if((hs.add(ar)) == false);
Does nothing, because of the ending semicolon. The element is actually added to the set, despite the condition of the if is never satisfied.
Then, you have the following block of code:
{
System.out.println("reaches here every time but shouldn't ");
}
It's awkward, but in Java, you can have arbitrary blocks of code surrounded by braces. This one is always executed, because (due to the semicolon) is totally independent of the previous if.
Try removing the semicolon:
if ((hs.add(ar)) == false) {
System.out.println("reaches here every time but shouldn't");
}
Formatting the code might be seen as something not very important, but here is a case that clearly shows that when code is not nicely formatted, unintuitive, unexpected, hard-to-debug issues might happen.
Besides, you could simplify your code:
if (!hs.add(ar)) {
System.out.println("reaches here every time but shouldn't");
}
Now it doesn't reach the println.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
enter image description here
I need some help for an if condition. I know how the basic one works: if x == 1, return something. But how am I supposed to do it, when I need to define certain cases for different operators. Like if the operators is + then the result denoted the sum.
So basically I need to translate the condition in the link into base cases for a recrsive method. We learned that we always use if for basecases. I know how to that with smaller or bigger then, but with operators I don't know.
FYI: if() is not a loop while() is. if() statement works as true or false, if the statement is true then execute a certain code other wise some other code.
For example:
if(1==1){// yourcode } // Always as true
// or
String hello="hi there";
if(hello.contains("hi there")){ // Your code which if the statement happen to be true }
else { // Not true}
int x=3, s=1, i=2;
if(x==(s+i)){ // Your code which if the statement happen to be true }
else { // Not true}
also you can find a lot of tutorials online to help you better understand all the operators!
Are you trying to state if its positive or negative? If so you would do the following...
if(x >= 0){ //this operator is saying if x is greater than or equal to 0
// you can remove the equal sign to have it just greater or
// switch it to less than.
//if positive
}else{
//all other numbers, which would just be negative numbers
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am unsure why my code is not working:
public int caughtSpeeding(int speed, boolean isBirthday) {
if(isBirthday=true){
speed = speed - 5;
}
if(speed<=60){
return 0;
}
if(speed>=81){
return 2;
}
return 1;
}
The question is:
You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
You are using an assignment operator here =, where you should be using an == operator for comparison. So it should be
if(isBirthday==true){
speed = speed - 5;
}
Here's the problem:
if (isBirthday = true) {
It should be:
if (isBirthday == true) {
We use two equal signs for comparison, otherwise you're just assigning a true value to the variable, making it always true. We can go even further and simplify the expression like this:
if (isBirthday) {
Your code is not working because you have a typo here:
if(isBirthday=true){
this is setting the variable to true instead of checking its value
if(isBirthday==true){ is what you are looking for
and in almost all languages is better when you write
if(isBirthday){ for checking if true
if(!isBirthday){ for checking if false
instead
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
If I have an if statement that includes a string like this:
double GUInumber1 = ((GUInumber1 >= 0 || <= 0)? Double.parseDouble(GUIfirstNumber) : 0);
('GUIfirstNumber' is the string)
Why does it come up as a error? Do I need brackets somewhere, do I need to use 'If/else' instead?
Supposedly my compiler says it does not recognize or (||), is this supposed to be something else or does this work in a completely different way.
Any help on this situation would be appreciated, also, if you need to know or are just wondering why I want to make a if statement for a string its because whenever I try to put in a letter instead of a number Java crashes, I'm hoping I could get this to alternatively solve the situation instead.
EDIT:
Solved, had to remove 'or' statements and alternatively make more else statements instead.
This expression (GUInumber1 >= 0 || <= 0) is not correct.
You will need something on both sides of the <= operator.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Few years back I was writing code for conditional check as like below
boolean isTesting ==false;
if(isTesting==false)
And one fine day one of senior guy came to me and told me this is not efficient way to do. And then explained something about this to me. I heard But I guess didn’t listened.
So I started to write like as below without knowing the purpose of it.
if(false==isTesting)
Now sudden curiosity came on this subject again is really efficient!? which one is best way to do so?
if(isTesting==false) ?
if(false==isTesting) ?
if(!isTesting) ?
And same doubt about if(null==value) and if(value==null). Please clarify
Thanks in advance.
if (isTesting == false)
is a bit dangerous, because you could forget an = sign and write
if (isTesting = false)
which compiles (it assigns false to isTesting, and evaluates to false).
if (false == isTesting)
avoids the problem because forgetting an = would make the code invalid. But it's much too verbose, and compares what is already a boolean to another boolean, to produce a boolean. So
if (!isTesting)
does the same thing, shows that you understand what a booelan expression is, and doesn't have the risk of forgetting an =. You should of course prefer this last way.
Writing
if (null == value)
is unnecessary, because forgetting an equal in
if (value = null)
would cause the code to become invalid. value = null is not a boolean expession (unless value is of type Boolean). So use value == null, which is more natural.
Anyway, unless you frequently switch to a language that uses = instead of == for comparisons, you normally won't forget to use == instead of =. It becomes natural after a few days. Good IDEs and code quality tools warn you when you use = instead of == in a boolean expression.
The best of these is if (!isTesting) as it's simplest and cleanest.
Of the other two options if (value==null) is
more natural so I would recommend using that one.
Functionally they are all equivalent.
That said, the recommendation to write the constant / literal on the left side of the == is motivated by the impossibility to accidentially write = (assignment) instead of == (equality). Since you can't assign to a constant / literal the compiler would complain if you accidentially wrote =.
The short, concise !expression has no such problems, and it furthermore makes it totally obvious that expression must evaluate to a boolean (everything else would be a compile error). So a simple if (boolean) / if (!boolean) is commonly considered the most logical idiom.