What they're doing there is for each iteration of i, they're multiplying the result by 2, i times.
The walkthrough would be
i = 0, e = 0, result not multiplied, output 2 to the power of 0 is 1
i = 1, e = 1, result multiplied one time by 2, 2 to the power of 1 is 2
etc
They're decrementing e there to "count i backwards" down to 0. E is reset each time, and will always enter the while loop on iterations after the first one, and will always exit the while loop once it counts down to zero.
Answer from dbillz on Stack OverflowWhile loop condition in java - Stack Overflow
JAVA - using FOR, WHILE and DO WHILE loops to sum 1 through 100 - Stack Overflow
What makes for-loops difficult for beginners?
for loop in Python and for loop in Java: differences
Videos
What they're doing there is for each iteration of i, they're multiplying the result by 2, i times.
The walkthrough would be
i = 0, e = 0, result not multiplied, output 2 to the power of 0 is 1
i = 1, e = 1, result multiplied one time by 2, 2 to the power of 1 is 2
etc
They're decrementing e there to "count i backwards" down to 0. E is reset each time, and will always enter the while loop on iterations after the first one, and will always exit the while loop once it counts down to zero.
If you do not do e-- you will get stuck in an endless while loop.
Look at it like this:
Let's go into a random iteration of the for loop.
Let's say i=5.
Then e=i so e=5.
We now jump into the while loop.
while (e >0) {
result*=2;
e--;
}
If we did NOT do e--, e would always stay at 5 and the while loop would never terminate.
By doing e--, e will start at 5, then 4, .... then 0 where the while loop will terminate and we will jump back into the for loop, raise i (and consequently e) and repeat the whole process.
- 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);
}
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.