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.
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.
It's best to use a for loop.For example, if you want 10 iterations:for (int i = 1; i
"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.
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.
It's best to use a for loop.For example, if you want 10 iterations:for (int i = 1; i
For loop: A for loop is a control flow statement that repeats a block of code a set number of times based on a predefined condition. It is commonly used when you know in advance how many iterations are needed. While loop: A while loop is another control flow statement that repeats a block of code as long as a specified condition is true. It is useful when you do not know in advance how many times the code needs to be executed.
4
A for loop typically runs a specific number of times in each iteration, as determined by the loop's initialization, condition, and increment/decrement statements.
The for loop would execute 10 times in the following code snippet.
6 times. When x is 7, the loop ends.
"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.