charly2012
Badges: 1
Rep:
?
#1
Report Thread starter 7 years ago
#1
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
0
reply
Damask-
Badges: 16
Rep:
?
#2
Report 7 years ago
#2
Code:
if (guard) {
//do this
} else if (guard) {
//do this
} else {
//do this
}
0
reply
charly2012
Badges: 1
Rep:
?
#3
Report Thread starter 7 years ago
#3
(Original post by Damask-)
Code:
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
0
reply
Damask-
Badges: 16
Rep:
?
#4
Report 7 years ago
#4
(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.
1
reply
charly2012
Badges: 1
Rep:
?
#5
Report Thread starter 7 years ago
#5
(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.
0
reply
Damask-
Badges: 16
Rep:
?
#6
Report 7 years ago
#6
(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.
0
reply
charly2012
Badges: 1
Rep:
?
#7
Report Thread starter 7 years ago
#7
(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 : )
0
reply
Damask-
Badges: 16
Rep:
?
#8
Report 7 years ago
#8
(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.
0
reply
charly2012
Badges: 1
Rep:
?
#9
Report Thread starter 7 years ago
#9
(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.
0
reply
Damask-
Badges: 16
Rep:
?
#10
Report 7 years ago
#10
(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.
0
reply
charly2012
Badges: 1
Rep:
?
#11
Report Thread starter 7 years ago
#11
(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 ;
}
0
reply
Damask-
Badges: 16
Rep:
?
#12
Report 7 years ago
#12
(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.
0
reply
charly2012
Badges: 1
Rep:
?
#13
Report Thread starter 7 years ago
#13
(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.
0
reply
Planto
Badges: 16
Rep:
?
#14
Report 7 years ago
#14
(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.
0
reply
DrAndrewColes
Badges: 2
Rep:
?
#15
Report 7 years ago
#15
(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.
0
reply
DrAndrewColes
Badges: 2
Rep:
?
#16
Report 7 years ago
#16
(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 .
0
reply
charly2012
Badges: 1
Rep:
?
#17
Report Thread starter 7 years ago
#17
(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
0
reply
Planto
Badges: 16
Rep:
?
#18
Report 7 years ago
#18
(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 .
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.
0
reply
DrAndrewColes
Badges: 2
Rep:
?
#19
Report 7 years ago
#19
(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 .

switch (a < b ? 1 : 0) {
...
}
0
reply
Planto
Badges: 16
Rep:
?
#20
Report 7 years ago
#20
(Original post by DrAndrewColes)
Chapeau, didn't know Java had that restriction. Nothing a ternary operator can't fix .

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.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Have you experienced financial difficulties as a student due to Covid-19?

Yes, I have really struggled financially (44)
18.11%
I have experienced some financial difficulties (68)
27.98%
I haven't experienced any financial difficulties and things have stayed the same (92)
37.86%
I have had better financial opportunities as a result of the pandemic (30)
12.35%
I've had another experience (let us know in the thread!) (9)
3.7%

Watched Threads

View All