Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Infinite loop using boolean as condition

    I'm getting what I believe is an infinite loop after inputting an integer >= 1. Why? Any help is appreciated.

    Java Code:
    public int calculateSequence(){
            int n = 1;
            int fnew = 1;
            boolean calculateN = true;
            boolean checkForValidInt = true;
            while (checkForValidInt) {
                System.out.print("Fibonacci Sequence - enter an integer to calculate: ");
                Scanner in = new Scanner(System.in); 
                if (in.hasNextInt()){
                    n = in.nextInt();
                    if (n < 1){
                        System.out.println("Error: must be greater than or equal to one");
                    }
                    else{
                        checkForValidInt = false;
                    }
                }
                else {
                    System.out.println("Error: must be an integer");
                }
            }
            while (calculateN && n != 1 || n != 2 || n != 3) {
               if (n != fnew){
                   n++;
                   fold1 = fold2;
                   fold2 = fnew;
                   fnew = fold1 + fold2;
               }
               else {
                   calculateN = false; 
               }
            }
            if (n == 1) {fnew = 1;} //sets value of fold1 to 1
            if (n == 2) {fnew = 1;} //sets value of fold2 to 1
            if (n == 3) {fnew = 2;} //sets value of fold 3 to 2
            
            return fold1 + fold2;
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,773
    Rep Power
    25

    Default Re: Infinite loop using boolean as condition

    The condition n != 1 is true for all the other billion values of n that are not 1.
    Using OR operators means that if any one of the conditions is true, the full expression is true.

    Using an AND operator means that ALL of the conditions must be true for the full expression to be true.

    Putting all sub conditions in ()s is safer code.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Infinite loop using boolean as condition

    for you. Thanks.

Similar Threads

  1. Infinite loop
    By F.S. in forum New To Java
    Replies: 3
    Last Post: 03-09-2012, 01:52 PM
  2. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 09:59 AM
  3. boolean condition creating problem
    By Basit781 in forum CLDC and MIDP
    Replies: 1
    Last Post: 05-25-2010, 08:57 AM
  4. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 02:34 AM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 11:11 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
  •