answersLogoWhite

0

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.

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

Explain the difference between for and while loop and give the suitable examples in c plus plus?

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.


What are loop detectors are use to do?

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.


When would you use a count controlled loop vs. a flag controlled loop?

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.


How do you loop a program in python 3?

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]


Why you use if loop in telecoms?

No such thing as if-loop. if-else statement is not a loop.

Related Questions

How can I determine the number of iterations a loop runs in Java?

To determine the number of iterations a loop runs in Java, you can use a counter variable that increments each time the loop runs. You can also use a conditional statement to check when the loop should stop running. By keeping track of the counter variable, you can determine the total number of iterations the loop has executed.


Explain the difference between for and while loop and give the suitable examples in c plus plus?

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.


How can I parallelize a for loop in Python for improved performance?

To parallelize a for loop in Python for improved performance, you can use libraries like multiprocessing or concurrent.futures to split the loop iterations across multiple CPU cores. This allows the loop to run concurrently, speeding up the overall execution time.


How can I efficiently utilize the run for loop in parallel in Python to optimize the execution of my code?

To efficiently utilize the run for loop in parallel in Python, you can use the concurrent.futures module to create a ThreadPoolExecutor or ProcessPoolExecutor. This allows you to run multiple iterations of the loop concurrently, optimizing the execution of your code by utilizing multiple CPU cores.


How can you parallelize a for loop in Python effectively?

To parallelize a for loop in Python effectively, you can use libraries like multiprocessing or concurrent.futures to create multiple processes or threads to execute the loop iterations concurrently. This can help improve performance by utilizing multiple CPU cores. Be cautious of shared resources and synchronization to avoid race conditions.


Which is more good to use for loop or while loop?

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 can I use the ffmpeg loop feature to create a continuous video loop?

To create a continuous video loop using the ffmpeg loop feature, you can use the "loop" option in the ffmpeg command followed by the number of times you want the video to loop. For example, you can use the command "ffmpeg -streamloop -1 -i input.mp4 output.mp4" to loop the video indefinitely.


How do you use even and odd numbers for loop and if condition with variable please no use int?

how to use even and odd number with for loop and if condition plz dont use "int"..


What are loop detectors are use to do?

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.


Why you use FOR LOOP in C language?

For LOOP is used, when you want to execute a specific block of code for specific number of times.


How do you use if statement syntax in a for loop?

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); }


Compare While with for Statements?

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.