Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Difference in the code on changing logical operators

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Difference in the code on changing logical operators

    import java.util.Scanner;
     
    	class TicketPriceWithDiscount {
    		public static void main(String args[])
    		{
    			Scanner myScanner = new Scanner(System.in);
    			int age;
    			double price = 0.00;
    			char reply;
     
    			System.out.print("How old are you? ");
    			age = myScanner.nextInt();
     
    			System.out.print("Do you have a coupon? (Y/N)");
    			reply = myScanner.findInLine(".").charAt(0);
     
    			if(age >= 12 && age < 65){
    				price = 9.25;
    				}
    			if(age < 12 || age >= 65){
    				price = 5.25;
    			}
    			if(reply == 'Y' || reply =='y'){
    				price -= 2.00;
    			}
    			if(reply !='Y' && reply !='y' && reply !='N' &&
    				reply != 'n' ){
    					System.out.println("Huh");
    				}		
    		}
     
    }

    for the above code i am wondering if i were to change the 1st IF condition
    if(age >= 12 && age < 65)
    into
    if(age >= 12 || age < 65)
    would there be a difference?? right now i am having problems differentiating && and ||. just started out programming here


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Question

    && requires both statements to be true before the if statement will execute, whereas
    || requires only 1 statement to be true before the if statement will execute.

    Example

    public class lotus {
         public static void main(String[] args){
              int a = 5, b = 3;
              if(a == 5 && b == 3) System.out.println("Look they both are true");
              if(a == 5 && b == 7) System.out.println("Look only one is true, but using and so not printed");
              if(a == 5 ||  b == 3) System.out.println("Look they both are true, so or will print");
              if(a == 5 ||  b == 9) System.out.println("Look only one is true, by or means it will still print me");
              if(a == 9 ||  b == 9) System.out.println("Neither are ture so im not printed");
         }
    }
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    lotus (June 20th, 2009)

  4. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Question

    ok i got it thanks

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Question

    No problem, if thats everything could you please mark this as solved.

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 02:49 AM
  2. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM
  3. Java operaton on boolean varibles
    By big_c in forum Java Theory & Questions
    Replies: 5
    Last Post: May 12th, 2009, 04:40 AM
  4. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 11:38 AM
  5. How to show 2D array using combobox and radiobutton?
    By Broken in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2009, 06:01 AM