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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help understand this while loop.

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Thanked 0 Times in 0 Posts

    Default Need help understand this while loop.

    Hello everyone,

    I was reading a book and came across this while loop.

    public class Powers {
    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;
    	System.out.println("2 to the " + i + " power is " + result);

    This loop presents the following (I'm sure it's not necessary):

    2 to the 0 power is 1
    2 to the 1 power is 2
    2 to the 2 power is 4
    2 to the 3 power is 8
    2 to the 4 power is 16
    2 to the 5 power is 32
    2 to the 6 power is 64
    2 to the 7 power is 128
    2 to the 8 power is 256
    2 to the 9 power is 512

    I am just having a difficult time understand and grasping this concept. My main issue is result *=2; this is making it very difficult to understand. How is result being replace if it only equals to 1.
    Last edited by xXBurnt; July 6th, 2014 at 03:38 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,693 Times in 2,643 Posts

    Default Re: Need help understand this while loop.

    I don't understand what the problem is. Could you be more specific? Are there some statements that you don't understand.
    For example: e--; // Decrements the value in e by 1

    Please edit your post and wrap your code with code tags:
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Thanked 0 Times in 0 Posts

    Default Re: Need help understand this while loop.

    Never mind I managed to figure it out.

    Since result is set to 1, when the while loop is called the result of one is being constantly multiplied by 2 until the loop has reached the value of 9.

Similar Threads

  1. Do not understand this API
    By sravya in forum Collections and Generics
    Replies: 2
    Last Post: August 30th, 2013, 12:38 PM
  2. Please Help me to understand
    By Theillusion in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 17th, 2013, 03:09 AM
  3. Please help to Understand x=++x+ + +x+x++
    By rayan2004 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 2nd, 2012, 07:47 PM
  4. Help me to understand this
    By Madhushan in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2011, 08:47 AM
  5. Can't seem to understand why my while loop doesn't exit!
    By vortexnl in forum Loops & Control Statements
    Replies: 6
    Last Post: February 11th, 2011, 03:43 PM