0

I have created a while loop that takes in a user value b and checks for two conditions. Although I got my code right I originally tried to do this with a single while loop combining the two conditions but it didn't work. My incrementer i++ either counts once or loops forever depending on where I put it.

Is there a way that this could work using a while statement combining the conditions?

Does not work

        //Declare variables
        int i = 0 ;         //Counter Variable.
        int positive = 0 ;  //Holds the result of our positive sum.

        while ( ( i < b ) && ( i % 10 == 0 ) )  //While i is less than the users number.

            {

                positive = i ;
                System.out.println ( positive ) ;
                i++ ;   //Incrementer

            }

This Works

        //Declare variables
        int i = 0 ;         //Counter Variable.
        int positive = 0 ;  //Holds the result of our positive sum.

        while ( i < b ) //While i is less than the users number.

            {

                if ( i % 10 == 0 )

                    {

                        positive = i ;
                        System.out.println ( positive ) ;

                    }

                i++ ;
            }
5
  • What are you actually trying to achieve? ie, what is your expected output for a given input? Commented Feb 20, 2017 at 21:09
  • what is the value of b Commented Feb 20, 2017 at 21:09
  • 1
    No, because they are 2 separate things. One is a loop to go through a range of numbers, and the other is a condition to print. If you combine them, it will just stop as soon as one of the conditions doesn't work, as you experienced. Commented Feb 20, 2017 at 21:09
  • 1
    The first one will leave the loop after executing the body the first time because 1 % 10 == 0 is false. Commented Feb 20, 2017 at 21:10
  • If user enters 100, then it should print out all numbers between 1 and 100 that are also divisible by 10, so 10, 20, 30 etc. Commented Feb 20, 2017 at 21:24

2 Answers 2

1
while ( ( i < b ) && ( i % 10 == 0 ) ) 

loop will run only once because as soon as value of i changes from 0 to 1 because of incrementer

( i % 10 == 0 ) will fail.

Whereas in

 while ( i < b )

code is checking if i is less than b or not and

if ( i % 10 == 0 )

is getting handled as separate condition.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh I see, I assumed it would retest the condition, so in the first it breaks my loop and returns 0 to me. It doesn't fail in second code because it is just counting 0 to 99 and in each iteration it is then checking.
0

Those two chunks of code aren't equivalent and you can't really make them equivalent. In your first block of code, your comment takes into account the first condition, but not the second. You have two conditions there. Both of them must be true for the loop to continue. As soon as one of those conditions is false, the looping stops. So as soon as i % 10 == 0 returns false, the loop ends.

However, you can also solve this by incrementing i by 10 at each loop. So you could have something like and it would produce the same output:

int i = 0 ;
int positive = 0 ;
while (i < b){
    positive = i;
    System.out.println ( positive ) ;
    i += 10;
}

5 Comments

would be even simpler with a for loop. for (int i = 0; i < b; i += 10) {}
Also, this is actually wrong, positive will always be 0, just use i
Yeah for sure it's better as a for loop, but he asked about while loops specifically so I used that. I don't know what you mean with your second comment. It gets incremented.
Fair enough on the loop type. You're printing out the variable positive, which is always 0.
That's what OP's code does, I just copied it. EDIT: oh wow I'm an idiot. That's not what it does. My bad. I'll edit my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.