0

As the title states, I have trouble understanding loops and have come up with a way to do a simple 1 through 100 sum, but like I said, the loops are causing me some confusion. I think I have the FOR Loop figured.

Here's what I've come up with.

int sum = 0; 
for (int i = 1; i <= 100; i++) sum += i; 
System.out.println("The sum is " + sum);
3
  • 2
    What have you got with while and do while so far?
    – assylias
    Sep 16, 2012 at 18:02
  • Have you tried reading the Oracle Java tutorials? docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html Sep 16, 2012 at 18:13
  • I've been using Java the Complete Reference trying to teach myself and found the explanation difficult to follow. I'm auditing a course at the Junior College trying to pick up what I can. I'll the Oracle Tutorial to my reference library.
    – Zerodown
    Sep 18, 2012 at 2:02

3 Answers 3

7

- First to me Iterating and Looping are 2 different things.

Eg: Increment a variable till 5 is Looping.

    int count = 0;

    for (int i=0 ; i<5 ; i++){

        count = count + 1;

   }

Eg: Iterate over the Array to print out its values, is about Iteration

    int[] arr = {5,10,15,20,25};

    for (int i=0 ; i<arr.length ; i++){

        System.out.println(arr[i]);

   }

Now about all the Loops:

- Its always better to use For-Loop when you know the exact nos of time you gonna Loop, and if you are not sure of it go for While-Loop. Yes out there many geniuses can say that it can be done gracefully with both of them and i don't deny with them...but these are few things which makes me execute my program flawlessly...

For Loop :

int sum = 0; 

for (int i = 1; i <= 100; i++) {

  sum += i; 

}

 System.out.println("The sum is " + sum);

The Difference between While and Do-While is as Follows :

- While is a Entry Control Loop, Condition is checked in the Beginning before entering the loop.

- Do-While is a Exit Control Loop, Atleast once the block is always executed then the Condition is checked.

While Loop :

int sum = 0; 
int i = 0;       // i is 0 Here

    while (i<100) {

      sum += i; 
      i++;

    }

  System.out.println("The sum is " + sum);

do-While :

int sum = 0; 
int i = 0;      // i is 0 Here

    do{ 

      sum += i; 
       i++
    }while(i < 100; );

     System.out.println("The sum is " + sum);

From Java 5 we also have For-Each Loop to iterate over the Collections, even its handy with Arrays.

ArrayList<String> arr = new ArrayList<String>();

arr.add("Vivek");
arr.add("Is");
arr.add("Good");
arr.add("Boy");

for (String str : arr){         // str represents the value in each index of arr

    System.out.println(str);     

 }
3
  • I appreciate the explanation and its making more sense now.
    – Zerodown
    Sep 18, 2012 at 1:59
  • @Zerodown Atleast i deserve 1 upvote if it really did make any sense.... well i am glad this was helpful to u Sep 18, 2012 at 7:23
  • I tried for the upvote, but my rep is low since I'm brand new. Once I get my rep up, you got it!
    – Zerodown
    Sep 20, 2012 at 0:35
5

Your for loop looks good.

A possible while loop to accomplish the same thing:

int sum = 0;
int i = 1;
while (i <= 100) {
    sum += i;
    i++;
}
System.out.println("The sum is " + sum);

A possible do while loop to accomplish the same thing:

int sum = 0;
int i = 1;
do {
    sum += i;
    i++;
} while (i <= 100);
System.out.println("The sum is " + sum);

The difference between the while and the do while is that, with the do while, at least one iteration is sure to occur.

0
2

Well, a for or while loop differs from a do while loop. A do while executes the statements atleast once, even if the condition turns out to be false.

The for loop you specified is absolutely correct.

Although i will do all the loops for you once again.

int sum = 0;
// for loop

for (int i = 1; i<= 100; i++){
    sum = sum + i;
}
System.out.println(sum);

// while loop

sum = 0;
int j = 1;

while(j<=100){
    sum = sum + j;
    j++;
}

System.out.println(sum);

// do while loop

sum = 0;
j = 1;

do{
    sum = sum + j;
    j++;
}
while(j<=100);

System.out.println(sum);

In the last case condition j <= 100 is because, even if the condition of do while turns false, it will still execute once but that doesn't matter in this case as the condition turns true, so it continues to loop just like any other loop statement.

1
  • Thank you! Its making more sense now, I still have a long way to go but this definitely helped.
    – Zerodown
    Sep 18, 2012 at 2:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.