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: Logical Operators

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Logical Operators

    Ok so I was reading my textbook when I came to 'logical operators' (! && and ||). I understand what they do, but my question is, in what circumstance (examples?) would you want to use the !variable (logical NOT) operator? It seems kind of counter-intuitive. Examples (pref. simple ones) would be great if you have any off the top of your head. Thanks guys!


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Logical Operators

            boolean programStopped = false;
            while (!programStopped) {
                //do something - ends when programStopped = true
            }

            Scanner input = new Scanner(System.in);
            int x = input.nextInt();
            if(x != 2){
                System.out.println("Wrong Answer..Number I was thinking of was 2");
            }
            if(myArrayList != null){
            //get item from first index!
            }
            else{
                //Please insert something into ArrayList first..
            }

    Can be used often to stop exceptions arising in the first place, where the above without any operators would throw an IndexOutOfBoundsException if you tried doing it directly, which would then involve try-catch blocks.

    There are tons of examples I'm sure, but those are what I can think of right now
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    truebluecougarman (January 22nd, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Logical Operators

    very helpful. Thanks for taking the time to help me out!

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Logical Operators

    They're also useful for testing multiple conditions.
    boolean isRed = true;
    boolean isLarge = false;
    if(isRed && isLarge)
    {
        System.out.println("red and large");
    }
    if (isRed || isLarge)
    {
        System.out.println("is red or is large or both.");
    }

Similar Threads

  1. Need Help with Operators/ logic
    By codekiller in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2010, 09:25 AM
  2. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 12:17 AM
  3. Proper output for Non preemptive scheduling problem
    By haygaurav in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2009, 06:58 AM