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: WHILE LOOP USING || (or) SIMPLE PROBLEM

  1. #1
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Angry WHILE LOOP USING || (or) SIMPLE PROBLEM

    Dear all:

    I thought that a while statement of : (argument || argument), where at least one of the arguments was correct, the do loop attached to the while would end.

    QUESTION IS AT VERY END OF POST.

    Code snippet works if you want to try it.

     
    import java.util.Scanner;
    import java.lang.Math;
    public class FixChap5
    {
    public static void main(String[] args)
    {
    	Scanner keyboard = new Scanner(System.in);
    	int x = 1;
    	boolean b = false;
    	do
    	{
    		if ( x%2 == 0)
    	 		b = true;
    		else
    			b = false;
     
    		x++;
    	}
    	while (!b || x < 7 );
    	System.out.println(" x = " + x);
    }}

    So the above bit of code works and gives you X = 7.

    Without the: || x < 7 in the while line, the code stops at x = 3.

    From the book I read:
    Value of A = True , Value of B = False, Combined value of A||B = True

    QUESTION 1.
    So why is the do loop still going around when it should have stopped because b = false and thus printed out x = 3. Instead it waits until both arguments are correct and then stops at x = 7.

    QUESTION 2.
    I wanted to end a while statement with one of two different arguments being true, how do it do it?
    Last edited by SPACE MONKEY; May 6th, 2012 at 11:29 PM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: WHILE LOOP USING || (or) SIMPLE PROBLEM

    So why is the do loop still going around when it should have stopped because b = false and thus printed out x = 3. Instead it waits until both arguments are correct and then stops at x = 7.
    Because that's what "or" means! The point is that the "or" gives two possible reasons for the while loop to keep going: the more alternatives you put in the while condition, the longer the loop may keep going.

    QUESTION 2.
    I wanted to end a while statement with one of two different arguments being true, how do it do it?
    Remember that the do loop will finish when the condition becomes false, so you are looking for a statement that becomes false when one of its two halves is true. Consider "!foo && !bar" and "(!foo && !bar) || (foo && bar)". They are different (deliberately).
    Last edited by pbrockway2; May 6th, 2012 at 11:06 PM. Reason: (mucked up example)

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

    SPACE MONKEY (May 6th, 2012)

  4. #3
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: WHILE LOOP USING || (or) SIMPLE PROBLEM

    Dear pbrockway2:

    Thanks for your response.
    Remember that the do loop will finish when the condition becomes false
    I was confused, I was looking for the loop to finish when one argument was TRUE.

    But the loop to finishes when the result is FALSE.

    In my case, with one argument being false and the other argument being true, the final answer returned to the while method is TRUE.

    So the loop continues on another turn, instead of ending.

    Thanks for the help, another day of pain and blindness ended.

    How do I show this thread has been solved?
    Last edited by SPACE MONKEY; May 6th, 2012 at 11:29 PM.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: WHILE LOOP USING || (or) SIMPLE PROBLEM

    You're welcome.

Similar Threads

  1. One of the most simple loop awnsers
    By Firestar912 in forum Loops & Control Statements
    Replies: 8
    Last Post: May 7th, 2012, 07:13 PM
  2. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM
  3. Replies: 4
    Last Post: May 15th, 2011, 06:03 AM
  4. [SOLVED] ArrayOutofBoundary in Simple While Loop
    By Johnpower in forum Loops & Control Statements
    Replies: 6
    Last Post: June 13th, 2010, 04:31 AM
  5. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM