# Thread: Can i put Two conditions in IF statement?

1. Member
Join Date
Apr 2016
Posts
3
Rep Power
0

## Can i put Two conditions in IF statement?

Hello Everybody,
I want o put two conditions in one IF statement or something that can work for me in this sityuation would be highly appreciates.. below is a sample of what i want to do....

so i have different rates like
1\$<=10 i charge 2\$
11<=20 i charge 4\$

Something like so if i wanted to put both Conditions in one statement how do i go by it?
i have tried
something like

switch (Rates) {

case 1:

if(x<=10){
charge=2;
}

now the problem come for the second condition because it has to be
x starting from 10 but less than 20 to charge 4
i tried this but it failed
case 2:

if(x=10<=20){
charge=4;
}
any help will be highly appreciated

2. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Can i put Two conditions in IF statement?

If you want to check for x between 10 and 20 inclusive:
Java Code:
`if (x >= 10 && x <= 20)`
in other words you treat it as two boolean statements, both of which have to be true.

I can't quite understand the rest of what you;re trying to achieve with the switch statement, but the above would cover your 'if'.

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Can i put Two conditions in IF statement?

Yes you can do it using && or ||. Just be careful your comparison makes sense. For example:

Java Code:
```if (a < 10 && a > 20) {
// do something
}```
This condition will never evaluate to true since a cannot be < 10 "and" > 20 at the same time.

Regards,
Jim

4. Member
Join Date
Apr 2016
Posts
3
Rep Power
0

## Re: Can i put Two conditions in IF statement?

i have tried that but i set int a to get its value from a Scanner input.
But then when i print, if statement doesn't execute anything. here is a sample of my code if any of you can help.

import java.util.*;
public class Mmoney {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int Amnt=0;
int Chrge=0;
Scanner sc =new Scanner(System.in);
System.out.println("Enter amount");
Amnt=sc.nextInt();

if(Amnt <10&& Amnt>20){
Chrge=500;
System.out.println(Chrge);
}
if(Amnt >=21 && Amnt<30){
Chrge=1000;
System.out.println(Chrge);
}
else{
System.out.println("worng input");

}
}

}

5. ## Re: Can i put Two conditions in IF statement?

Give me a number for Amnt which is smaller than 10 AND greater than 20 at the same time, because that's what this statement says: Amnt <10 && Amnt > 20

6. Member
Join Date
Apr 2016
Posts
3
Rep Power
0

## Re: Can i put Two conditions in IF statement?

i figured that out help me if you got any simpler ways to do it or an operand to use.. because if i use || then numbers between 10 & 20 are ignored. Thanks though
Last edited by PainSama; 04-29-2016 at 11:26 AM.

7. ## Re: Can i put Two conditions in IF statement?

That is because you have no logic for numbers between 10 and 20. Only for smaller than 10, greater than 20 and between 21 and 30.

Amnt < 10 && Amnt > 20 -> can never be true, because there is no such number smaller than 10 AND greater than 20 at the same time
Amnt < 10 || Amnt > 20 -> is true for a number smaller than 10 OR larger than 20, i.e. all the numbers NOT between 10 and 20
Last edited by SurfMan; 04-29-2016 at 01:02 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•