The Student Room Group

help with java condition ( IF statement )

Can someone assist me with if statements , i have already tried to do it but i have error , as i am not sure how to write multiples of if's. any helps appreciated.

ifstatment.docx

Scroll to see replies

Reply 1

if (guard) {
//do this
} else if (guard) {
//do this
} else {
//do this
}
Reply 2
Original post by Damask-

if (guard) {
//do this
} else if (guard) {
//do this
} else {
//do this
}



sorry i am a beginner in java , i have followed your structure in my code but i get an Error still , could you please look at my code below and point me to the right direction . thank you.

ifstatment.docx
Reply 3
Original post by charly2012
sorry i am a beginner in java , i have followed your structure in my code but i get an Error still , could you please look at my code below and point me to the right direction . thank you.

ifstatment.docx


"else" does not need a condition. Else is just what happens if none of the other statements run, so take away the last set of brackets and the stuff inside them.
Reply 4
Original post by Damask-
"else" does not need a condition. Else is just what happens if none of the other statements run, so take away the last set of brackets and the stuff inside them.


Right , so that means if it is equal which is not > or < then it should return "perfect"

thanks a lot.
Reply 5
Original post by charly2012
Right , so that means if it is equal which is not > or < then it should return "perfect"

thanks a lot.


Exactly!

And you're welcome.
Reply 6
Original post by Damask-
Exactly!

And you're welcome.



Thanks i think i found myself a teacher : D is it allright if i ask you for help with java when required : )
Reply 7
Original post by charly2012
Thanks i think i found myself a teacher : D is it allright if i ask you for help with java when required : )


It's probably quicker if you just post it to the forum, but by all means PM me if you'd rather. I can't promise to be able to solve everything - I'm not too experienced with Java myself.
Reply 8
Original post by Damask-
It's probably quicker if you just post it to the forum, but by all means PM me if you'd rather. I can't promise to be able to solve everything - I'm not too experienced with Java myself.


allright thank you .

if i have method : public boolean isTooSmall(int number)

how can i make this method to return a true or false value without using If statement.
Reply 9
Original post by charly2012
allright thank you .

if i have method : public boolean isTooSmall(int number)

how can i make this method to return a true or false value without using If statement.


Use a switch (case) statement.
Reply 10
Original post by Damask-
Use a switch (case) statement.


sorry i am not quite sure about this statement (haven't learnt it yet) , is it something you learn in the Beginning like if statement ?

say if i use conditional statment , would it be :

public boolean isTooSmall(int number)
{
if (number < referenceNumber)
{ return true;}

else return false ;
}
(edited 10 years ago)
Reply 11
Original post by charly2012
sorry i am not quite sure about this statement (haven't learnt it yet) , is it something you learn in the Beginning like if statement ?

say if i use conditional statment , would it be :

public boolean isTooSmall(int number)
{
if (number < referenceNumber)
{ return true;}

else return false ;
}


It's similar to an if statement, but more concise and the syntax is different - it's used for different things. You can read about them here.
Reply 12
Original post by Damask-
It's similar to an if statement, but more concise and the syntax is different - it's used for different things. You can read about them here.


i had a look at it ,but the thing is , i am going through a book and i am done with chapter 2 , but it hasn't mentioned case statement yet , but i guess my if statement is still right for that method isn't it ?

i have a short exercise for chapter 2 , is it allright if i show you my working for checking incase if i am doing the whole thing correctly.
Reply 13
Original post by Damask-
Use a switch (case) statement.


This won't work, switch statements can only compare values against specific compile time constants, so unless he has a finite amount of numbers that are too small/big enough (not likely given the nature of numbers) there's no way to achieve this with a switch statement.
(edited 10 years ago)
Original post by charly2012
allright thank you .

if i have method : public boolean isTooSmall(int number)

how can i make this method to return a true or false value without using If statement.

I'll give you the pieces of a puzzle and see if you can put them together. (Just because I'm mean, and don't like telling people the answers straight off :cool: )

1) Comparison operators are methods in disguise

Comparison operators can be thought of as methods that return a boolean value - the piece of code:

(a < b)

...returns true if a is less than b; otherwise, it returns false. I emphasise this because people often think comparison operators are these magical things that we can *only* use in the conditions of if statements, while loops etc:

if (a < b) {
// do something
} else {
// do something else
}


This is a perfectly reasonable use of <. We can also, quite legitimately, write:

int a = 3;
int b = 4;
boolean c = (a < b);

If that hurts your head, read the last line as:

boolean c = lessThan(a,b);

...because, after all, comparison operators are just methods in disguise.

(Actually, if you ever do python or C++, you can write your own comparison operators, as methods. Java leaves these out, instead using the Comparable interface. But, you don't need to understand this to answer your question.)


2) What does a return statement do?

Return will evaluate an expression, and return the resulting value. The simple case is:

return true;

...which evaluates the expression (in this case, the constant value true), and returns its value (i.e. true). But, it's happy to do some work: it doesn't have to just take a constant, or a single variable. It can call methods, add numbers together....:

return (someVariable + 3 + 4);
return callSomeMethod(x,y,z);

etc. etc.
Original post by Planto
This won't work, switch statements can only compare values against specific compile time constants, so unless he has a finite amount of numbers that are too small/big enough (not likely given the nature of numbers) there's no way to achieve this with a switch statement.

Actually it is possible...

switch (the same condition as the if) {
case true: {
return true;
}
default: {
return false;
}
}

...though, this is almost certainly not the answer that was expected :wink:.
Reply 16
Original post by DrAndrewColes
I'll give you the pieces of a puzzle and see if you can put them together. (Just because I'm mean, and don't like telling people the answers straight off :cool: )

1) Comparison operators are methods in disguise

Comparison operators can be thought of as methods that return a boolean value - the piece of code:

(a < b)

...returns true if a is less than b; otherwise, it returns false. I emphasise this because people often think comparison operators are these magical things that we can *only* use in the conditions of if statements, while loops etc:

if (a < b) {
// do something
} else {
// do something else
}


This is a perfectly reasonable use of <. We can also, quite legitimately, write:

int a = 3;
int b = 4;
boolean c = (a < b);

If that hurts your head, read the last line as:

boolean c = lessThan(a,b);

...because, after all, comparison operators are just methods in disguise.

(Actually, if you ever do python or C++, you can write your own comparison operators, as methods. Java leaves these out, instead using the Comparable interface. But, you don't need to understand this to answer your question.)


2) What does a return statement do?

Return will evaluate an expression, and return the resulting value. The simple case is:

return true;

...which evaluates the expression (in this case, the constant value true), and returns its value (i.e. true). But, it's happy to do some work: it doesn't have to just take a constant, or a single variable. It can call methods, add numbers together....:

return (someVariable + 3 + 4);
return callSomeMethod(x,y,z);

etc. etc.


so in my case :

(number < referenceNumber)

int number = 4 ;
int referenceNumber = 9 ;

like that ? sorry i may not get the structure am new to java
Reply 17
Original post by DrAndrewColes
Actually it is possible...

switch (the same condition as the if) {
case true: {
return true;
}
default: {
return false;
}
}

...though, this is almost certainly not the answer that was expected :wink:.


This won't even compile - you can't switch over a boolean in Java.

You could cast to a string first, I suppose, but... why? To get that far, you'd have to have already solved the problem.
(edited 10 years ago)
Original post by Planto
This won't even compile - you can't switch over a boolean in Java.

You could cast to a string first, I suppose, but... why? To get that far, you'd have to have already solved the problem.

Chapeau, didn't know Java had that restriction. Nothing a ternary operator can't fix :wink:.

switch (a < b ? 1 : 0) {
...
}
(edited 10 years ago)
Reply 19
Original post by DrAndrewColes

Chapeau, didn't know Java had that restriction. Nothing a ternary operator can't fix :wink:.

switch (a < b ? 1 : 0) {
...
}


Again, that's not really solving the problem with a switch statement, it's solving the problem, yielding an integer and then putting a redundant switch statement after it. You still can't solve the original problem with a switch statement.
(edited 10 years ago)

Quick Reply

Latest

Trending

Trending