-1
class Testing {
 public static void main(String args[]) {

   int e;
   int result;

   for(int i = 0; i < 10; i++) {
     result = 1;
     e = i;
     while(e > 0) {
       result *= 2;
        e--;
     }

     System.out.println("2 to the " + i + " power is " + result);
   }
 }

The above code tests the integer power of 2 from 0 to 9. I'm confused as to why e--; is required in the while loop? Removing it causes an infinite loop error, but why? Wouldn't the code automatically break on the 9th time considering e = i and i already having a limit to be less than 10?

I would appreciate a simple/beginner explanation.

TylerH
  • 20,799
  • 66
  • 75
  • 101
BradleyPeh
  • 35
  • 7
  • 15
    Prety obvious: How else would `e > 0` ever become false if it is true at least once? – OH GOD SPIDERS Jun 18 '18 at 15:10
  • 3
    [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  Jun 18 '18 at 15:15
  • 1
    *"Wouldn't the code automatically break on the 9th time"* - How would it ever *reach* a "9th time"? Reduce the complexity of what you're asking to specifically the `while` loop. If you remove the decrement line, what would ever cause that `while` loop to exit? – David Jun 18 '18 at 15:16
  • So e--; is just there to break the loop ? I know its infinite because e > 0 is always true in this statement, But why is e continuously reduced to zero immediately from 9 to 8 to 7 etc.... in this decrement ? Isnt the -- operator suppose to decrease slowly by 1 – BradleyPeh Jun 18 '18 at 15:17
  • in doubt, add print – jhamon Jun 18 '18 at 15:17
  • 3
    @BradleyPeh: When you step through this code in your debugger and observe the runtime behavior, at what point does a specific operation produce an unexpected result? What operation was it? What were the inputs? What result did you expect? Why? (Basically, stop guessing and start debugging.) – David Jun 18 '18 at 15:18
  • 2
    Erm, I'm not sure we are using the same vocabulary here, but where I'm from going "from 9 to 8 to 7 etc." is calles decreasing by 1 in each step. Not sure what you mean by "slowly". If you decrease something by 1 you decrease it by 1... there is no "slow" or "fast" decreasing by 1. – OH GOD SPIDERS Jun 18 '18 at 15:21

3 Answers3

0
for(int i = 0; i < 10; i++) {
     result = 1;
     e = i;
     while(e > 0) {
       result *= 2;
        e--;
     }
}

e is decremented so that at every instance the value of e is brought down to end the while loop, else it will be an infinite loop as i is getting incremented. So at the second iteration, i = 1, e = i => e = 1, making the inner while loop always true, as 1 > 0. Therefore the inner e-- is important to bring it down to break out of the loop.

To fix this you could make the inside while loop a for loop and decrement e there if that makes it clearer for you. Also, I would suggest having a better variable than e. Good coding habits are important to inculcate early on.

TylerH
  • 20,799
  • 66
  • 75
  • 101
inspired_learner
  • 142
  • 1
  • 11
0

Look every i is assigning value to e like

i=1 then e=1
i=2 then e=2

........

So if you will not decrease the value of e while loop will run infinite.

Example if if i=3 then e=3 so while loop will run infinite if we will not add decrement

it is like nested loop

  for(int i = 0; i < 10; i++)
  {
      result = 1;

      for (int e = i; e > 0;)
      {
           result *= 2;
      }
  }

So answer is

for (int i = 0; i < 10; i++)
{
    result = 1;

    for (int e = i; e > 0; e--)
    {
       result *= 2;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

Simply look at the inner while-loop:

 while(e > 0) {
   result *= 2;
    e--;
 }

The breaking condition is when e is not more than zero. Since e starts as a positive number, you have to decrease it for it to ever not be greater than zero. It's equivalent to:

for(int j = 0; j < i; j++) {
   result *= 2;
}    
GBlodgett
  • 12,704
  • 4
  • 31
  • 45