It doesn't matter if you know the number of iterations or not, the for-loop is the same. Here is an example:
for (leave= 0; ! leave; ) {
/* read from file, on EOF set leave= 1; */
}
Yes. The for loop is not constrained to be used for simple counting tasks. You can, for example, have the control variable be a pointer an walk it through a tree. All you need to know is that the loop expression is executed at the end, followed by the test expression. Nothing in the spec says these have to be integer counting variables.
There is no actual difference; a for loop is just syntactic sugar for a while loop. Which you use depends largely upon which makes the most sense within the context of your source code. The for loop is clearly more flexible, but you will generally use a for loop whenever the number of iterations is known in advance, such as when counting iterations, whereas while loops are generally used whenever the number of iterations is unknown or infinite. Regardless, this has no effect on the efficiency of your code (the machine code maps almost directly, 1-to-1, with a while loop), it's just a question of which makes your code easier to read. One useful property of a for loop is that you can declare and initialise a control variable in the initial expression. This renders the control variable local to the loop, which is something you cannot achieve with a while loop. This has no effect on the resultant machine code, but by scoping variables within a for loop you automatically enlist the help of the compiler to eliminate bugs. It should be noted that the do-while loop is similar to a while loop, except that a do-while loop always executes its statements at least once, because the conditional expression is evaluated at the end of each iteration, rather than before each iteration as it is with a while loop. Again, a for loop can be used to achieve a do-while loop, however the do-while loop maps closely with the resultant machine code, and is generally much easier to read.
A smoke detector detects smoke, a fire detector detects fire. A loop detector, surely, detects a loop. Many detectors are also based on a loop of wire. For example, loops within the tarmac detect the presence of a car (a mass of ferromagnetic material) and triggers or promotes a change if the lights from red to green. Other sensors use loops as aerials, for example in theft-prevention systems used in shops. In the context with computer programming, one can envision a variety of methods to detect a loop which fails to terminate (and unintentionally endless loop, or one which doesn't terminate in a timely fashion). Critical loops can be monitored for a maximum number of iterations or a maximum duration.
Counter Loop:Counter loop is a loop which executes statement up to a fixed number of time.In GW FOR ... NEXT loop is used as counter loop.Controlled Loop:Controlled loop is used to extend the statements till a specific condition is satisfied. In GW WHILE ... WEND is used as controlled loop.
A program can be looped in Python by wrapping the entire program in a for or while loop. If you wish to loop the program a finite amount of times, it can be done like so (x = the amount of times you want it to loop, this can be a number or variable storing a number): for i in range(0,x): [code] If you wish to loop the program infinitely, you can use a simple while loop: while True: [code]
PCH answer is I.An integer, often unsigned, named usually i,j,k,l,m,n.
There is no actual difference; a for loop is just syntactic sugar for a while loop. Which you use depends largely upon which makes the most sense within the context of your source code. The for loop is clearly more flexible, but you will generally use a for loop whenever the number of iterations is known in advance, such as when counting iterations, whereas while loops are generally used whenever the number of iterations is unknown or infinite. Regardless, this has no effect on the efficiency of your code (the machine code maps almost directly, 1-to-1, with a while loop), it's just a question of which makes your code easier to read. One useful property of a for loop is that you can declare and initialise a control variable in the initial expression. This renders the control variable local to the loop, which is something you cannot achieve with a while loop. This has no effect on the resultant machine code, but by scoping variables within a for loop you automatically enlist the help of the compiler to eliminate bugs. It should be noted that the do-while loop is similar to a while loop, except that a do-while loop always executes its statements at least once, because the conditional expression is evaluated at the end of each iteration, rather than before each iteration as it is with a while loop. Again, a for loop can be used to achieve a do-while loop, however the do-while loop maps closely with the resultant machine code, and is generally much easier to read.
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.
how to use even and odd number with for loop and if condition plz dont use "int"..
A smoke detector detects smoke, a fire detector detects fire. A loop detector, surely, detects a loop. Many detectors are also based on a loop of wire. For example, loops within the tarmac detect the presence of a car (a mass of ferromagnetic material) and triggers or promotes a change if the lights from red to green. Other sensors use loops as aerials, for example in theft-prevention systems used in shops. In the context with computer programming, one can envision a variety of methods to detect a loop which fails to terminate (and unintentionally endless loop, or one which doesn't terminate in a timely fashion). Critical loops can be monitored for a maximum number of iterations or a maximum duration.
For LOOP is used, when you want to execute a specific block of code for specific number of times.
You can use any number of if staments within a for-loop, eg: for (i=0; i<10; ++i) { if (i=1) printf ("%d=1\n",i); }
The while loop is the more general one because its loop condition is more flexible and can be more complicated than that of a for loop. The condition of a for loop is always the same and implicit in the construction. A for loop stops if there are no more elements in the collection to treat. For simple traversals or iterations over index ranges it is a good advice to use the for statement because it handles the iteration variable for you, so it is more secure than while where you have to handle the end of the iteration and the change of the iteration variable by yourself.
You can use a for loop whenever you can use a while loop; it's the same.
Counter Loop:Counter loop is a loop which executes statement up to a fixed number of time.In GW FOR ... NEXT loop is used as counter loop.Controlled Loop:Controlled loop is used to extend the statements till a specific condition is satisfied. In GW WHILE ... WEND is used as controlled loop.
A program can be looped in Python by wrapping the entire program in a for or while loop. If you wish to loop the program a finite amount of times, it can be done like so (x = the amount of times you want it to loop, this can be a number or variable storing a number): for i in range(0,x): [code] If you wish to loop the program infinitely, you can use a simple while loop: while True: [code]
No such thing as if-loop. if-else statement is not a loop.
PCH answer is I.An integer, often unsigned, named usually i,j,k,l,m,n.