Results 1 to 7 of 7
  1. #1
    PainSama is offline Member
    Join Date
    Apr 2016
    Posts
    3
    Rep Power
    0

    Default 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. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    PainSama is offline Member
    Join Date
    Apr 2016
    Posts
    3
    Rep Power
    0

    Default 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. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default 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

    (See also reply #3)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  6. #6
    PainSama is offline Member
    Join Date
    Apr 2016
    Posts
    3
    Rep Power
    0

    Default 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 12:26 PM.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default 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 02:02 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

Similar Threads

  1. Two conditions in a while statement
    By loki88 in forum New To Java
    Replies: 31
    Last Post: 08-22-2013, 07:37 PM
  2. How to avoid many if else conditions
    By Addymails in forum New To Java
    Replies: 9
    Last Post: 03-22-2012, 01:39 PM
  3. Problem in while conditions
    By davie89 in forum New To Java
    Replies: 2
    Last Post: 03-20-2012, 01:37 AM

Posting Permissions

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