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
arrays - Java programming Beginner While-Loop - Stack Overflow
do while loop - Java Basic
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.
My Intro to programming class has covered how to use both for and while loops. As for the code I am good to go with it but I am having a little trouble understanding them.
When would I use a while loop? Why would that be the better choice for the loop over a for loop?
Though I know how to code them I do not quite have a full comprehension on determining which to use and why one would be better than the other.
Any examples and/or like laymen's break down would be much appreciated!
If you want to read data from a network socket until a character sequence is found, you first need to read the data and then check the data for the escape sequence.
do
{
// read data
} while ( /* data is not escape sequence */ );
The while statement continually executes a block of statements while a particular condition is true
while (expression) {
statement(s)
}
do-while evaluates its expression at the bottom of the loop, and therefore, the statements within the do block are always executed at least once.
do {
statement(s)
} while (expression);
Now will talk about functional difference,
while-loops consist of a conditional branch instructions such as if_icmpge or if_icmplt and a goto statement. The conditional instruction branches the execution to the instruction immediately after the loop and therefore terminates the loop if the condition is not met. The final instruction in the loop is a goto that branches the byte code back to the beginning of the loop ensuring the byte code keeps looping until the conditional branch is met.
A Do-while-loops are also very similar to for-loops and while-loops except that they do not require the goto instruction as the conditional branch is the last instruction and is be used to loop back to the beginning
A do-while loop always runs the loop body at least once - it skips the initial condition check. Since it skips first check, one branch will be less and one less condition to be evaluated.
By using do-while you may gain performance if the expression/condition is complex, since it is ensured to loop atleast once. In that casedo-while could call for performance gain
Very Impressive findings here, http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne.html#while_loop
If I'm interpreting your question correctly you'd like real life examples that translate to code. Maybe something like the following.
You want to drive or ride your bike to the ice-cream shop a few miles away. You don't know how far it is exactly but you will recognize it when you get there (while), VS You want to ride your bike 15 miles out and then return and you have an odometer on the bike (for)
You want to shoot baskets until you have made 100 attempts (for) VS You want to shoot baskets until you have made 50 successful shots (while). Outside the US, substitute free kicks.
You want to write a 15 page paper on looping in Java (for) VS You want to work on your "looping in Java" paper until you are satisfied with the result (while).
You want to boil potatoes until a fork can be easily inserted (while) VS You want to boil potatoes for 11 minutes (for).
To translate these into code you need some imagined functions for the actual activities (void boilPotatoes()).
For some of them it is useful to discuss the empty case: you are already at the ice-cream shop, etc. For others, a discussion of "at least one iteration" might be useful to have.
If you want to include foreach in the game:
- You want to polish all of your mom's teacups (foreach) VS You want to polish her ten most valuable teacups (for) VS You want to polish teacups until you run out of polish (while).
You would know beforehand when the for loop would terminate, this is not clear in a while loop. I basically tell my students "if you know when it ends, it's a for". (Sure, one can construct pathological cases and there is always this for (;;), but for basic understanding this issue is the crucial difference between for and while.)
As for examples:
- print all elements of this array is a
for, - play so many rounds of tic-tac-toe until the human player quits:
while.
You might want to force them to convert for to while in an exercise and demostrate that the converse is not always possible.