one or more
a loop consist of data initialization;test condition;updation; example a for loop for(int a=1;a<5;a++) the loop will be executed 5 times four positives result and the last test condition will be failed and the loop will be exited there are many loops some of them are while loop,do...while loop,for loop,maybe more...... do while is an exit check loop and while and for are entry check loop.
Easy: if-else is not a loop; while, for and do-while are loops.if-else just run once, but do-while run many times.
It's best to use a for loop.For example, if you want 10 iterations:for (int i = 1; i
The for loop is used when you want to do a procedure, a certain amount of times. The for loop is used when you already know how many times the loop will be repeated. for example... you want to scan an array. you can do something like this. for(i=0;i<myArray.length;i++){ if(myArray[i] == 5){ alert("This array position contains the number five."); } } the code above is javascript. But the idea is the same in any language. This is one use, but as I said. You use it when you already know the amount of times the loop will be repeated.
"while" is an entry controlled loop statement i.e the condition is evaluated first and if it is true the body of the loop is evaluated.This process is repeated until the test condition is false;then the control is transfered out of the loop. The general form of while is as following syntax: while (<condition>) { <statements>; }
Easy: if-else is not a loop; while, for and do-while are loops.if-else just run once, but do-while run many times.
a loop consist of data initialization;test condition;updation; example a for loop for(int a=1;a<5;a++) the loop will be executed 5 times four positives result and the last test condition will be failed and the loop will be exited there are many loops some of them are while loop,do...while loop,for loop,maybe more...... do while is an exit check loop and while and for are entry check loop.
Easy: if-else is not a loop; while, for and do-while are loops.if-else just run once, but do-while run many times.
It's best to use a for loop.For example, if you want 10 iterations:for (int i = 1; i
The for loop is used when you want to do a procedure, a certain amount of times. The for loop is used when you already know how many times the loop will be repeated. for example... you want to scan an array. you can do something like this. for(i=0;i<myArray.length;i++){ if(myArray[i] == 5){ alert("This array position contains the number five."); } } the code above is javascript. But the idea is the same in any language. This is one use, but as I said. You use it when you already know the amount of times the loop will be repeated.
4
"while" is an entry controlled loop statement i.e the condition is evaluated first and if it is true the body of the loop is evaluated.This process is repeated until the test condition is false;then the control is transfered out of the loop. The general form of while is as following syntax: while (<condition>) { <statements>; }
The main difference comes into picture when you use continue with them i.e. for and while. In a while loop if continue is used before the increment of a variable is done it converts into a infinite loop. i=1; while(i<10) { /* do stuff */ if(i==6); continue; i++; } The above piece of code will turn into an infinite loop. for(i=1;i<10;i++) { /* do stuff */ if(i==6); continue; } In the above for loop the value of will be incremented once it attains the value 6. Therefore it will not turn into a infinite loop. Cheers. astvansh> 'for' statement takes the start condition, stop condition, and the increment. 'while' statement just takes the stop condition. One scenario (which i can think of) where while 'needs' to be used rather than a for, would be when the counter needs to be incremented/decremented conditionally... string[] arr = {"a", "b", "b", "c"}; int i = 0; while( i< 10) { // do something constructive if(arr[i] == "b") { i = i + 2; } else { i++; } } Cheers, Ajeesh Another main difference between the two: a 'for' loop is called a determinate loop, meaning that we usually use it when we know ahead of time how many iterations we want. The 'while' loop is an 'indeterminate' loop, because we usually use it in situations where we do not know the number of times the loop may iterate.
6 times. When x is 7, the loop ends.
A "null loop" is typically a loop which has no code in the loop body. for(int i = 0; i < 57; ++i) { }
Use loops. int i; // for loop for(i = 0; i < 10; ++i) { System.out.println(i); } // do loop i = 0; do { System.out.println(i++); } while(i < 10); // while loop i = 0; while(i < 10) { System.out.println(i++); } Each of the above blocks of code will print the values 0-9. Replace the body of the loops to make the code it executes useful. Replace the conditions to change when the loops exit.
Features wise they are both the same. The difference lies in the way they are implemented.while LoopThe while loop is good for scenarios where you don't know how many times a block or statement should repeat, but you want to continue looping as long as some condition is true. A while statement looks like this:while (expression) {// do stuff}In this case, as in all loops, the expression (test) must evaluate to a boolean result. The body of the while loop will only execute if the expression (sometimes called the "condition") results in a value of true. Once inside the loop, the loop body will repeat until the condition is no longer met because it evaluates to false.Any variables used in the expression of a while loop must be declared before the expression is evaluated. In other words, you can't saywhile (int x = 2) { } // This is not legalThe key point to remember about a while loop is that it might not run at all. If the test expression is false the first time the while expression is checked, the loop body will be skipped and the program will begin executing at the first statement after the while loop. Look at the following example:int x = 8;while (x > 8) {System.out.println("in the loop");x = 10;}System.out.println("past the loop");Running this code producespast the loopBecause the expression (x > 8) evaluates to false, none of the code within the while loop ever executesThe for loop is especially useful for flow control when you already know how many times you need to execute the statements in the loop's block. The for loop declaration has three main parts, besides the body of the loop:• Declaration and initialization of variables• The boolean expression (conditional test)• The iteration expressionThe three for declaration parts are separated by semicolons. The following two examples demonstrate the for loop. The first example shows the parts of a for loop in a pseudocode form, and the second shows a typical example of a for loop.for (/*Initialization*/ ; /*Condition*/ ; /* Iteration */) {/* loop body */}Ex:for (int i = 0; i