answersLogoWhite

0

What is the syntax for a For Loop with a counter?

Updated: 8/16/2019
User Avatar

Wiki User

15y ago

Best Answer

for(int i = 0; i < number; i++){

// ... Do stuff ...

}

A for loop behaves exactly like:

int i = 0;

while(i < number){

// ... Do stuff ...

i++;

}

The first of the three parameters in the for loop simply initiates a variable, in the example, 'int i = 0'. If you are initiating an already existing int, you must drop the 'int' declaration and just do 'i = 0'.

The second parameter (after the first semicolon(;) ) tells the for loop when to execute. The statement must resolve to true or false. It is evaluated before each loop and, if true, the loop executes.

The third parameter (i++) executes at the end of every loop. This is where you would provide your incrementer for your counter.

The for loop was made to make the while loop with a counter easier to read and write. You could also write:

int i = 0;

for(;i < number;){

// ... Do stuff...

i++;

}

And your for loop has become a while loop.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the syntax for a For Loop with a counter?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Turning a while loop to a for loop?

A for loop is just a while loop with a built-in counter. For example, the following programs are functionally identical: While loop: int counter = 0; while(counter &lt; 10) { printf("counter = %d\n", counter); counter++; } For loop: for(int counter = 0; counter &lt; 10; counter++) { printf("counter = %d\n", counter); }


What is the definition and syntax of for next loops?

loop within in a loop is called for next loop


How do you use a for loop?

The FOR loop syntax is as follows for(counter initiation/declaration; condition; counter increment){ code.... } example: for(int i = 0; i &lt; 10; i++){ System.out.println(i); } In the above code the variable i is the loop counter. We have initiated in the first part of the for loop. The second part is the condition. The loop would be executed until the value of i is less than 10. The third is the loop increment to ensure that the value of i would not remain the same causing an infinite loop. for(int i = 0; i &lt; 10; ){ System.out.println(i); } The above for loop usage is an infinite loop because the value of i would never change and the loop would go on forever. for(int i = 0; i &lt; 10; ){ System.out.println(i); i++; } You can even opt to have the loop counter incremented inside the loop construct. This would make it similar to a while loop. but anyways the purpose of the increment remains the same.


Syntax of for loop in c language?

for(i=0;i&lt;=0;i++)


What is counter loop?

A counted loop is a loop that executes the loop's statement a pre-determined number of times. The count represent the exit condition of the loop. A loop that is not counted is an infinite loop.


Which loop specifically designed to initialize test and increment a counter?

A for loop.


Is it possible to use a for loop in place of a while loop?

Yes. while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below: for(;condition;) eg: for(;i&lt;=n;)


What is the difference between a for loop and a while loop in java?

ComparisonThe conditions for both the 'while' and 'for' loop are exactly the same, but in each flow structure the conditions are placed in different locations. A 'for' loop places the full condition within the 'for' loop whereas the 'while' loop places the counter variable outside of the loop.The 'for' loop is used when the length of the loop is known whereas the 'while' loop is usually used when the length of the loop is unknown.'for' loop exampleWithin the parentheses the complete condition is contained. The condition has 3 parts delimited by a colon ';'. The first part sets the counter 'int i' to 0, the second part tells the loop to stop when the counter is 5. The third part tells java to increment the counter by 1 value each time the loop iterates.for ( int i = 0; i < 5; i++)'while' loop exampleWith the 'while' loop the counter is initialized outside of the 'while' loop. Inside the parentheses of the loop the condition is set to stop looping when the counter reaches a value of 5. Inside of the 'while' loop block the counter is set to increment by 1 value each time the loop iterates.int i = 0;while ( i < 5 ) {i++}


Which loop is specifically designed to initiate test and increment a counter variable?

for loop


What is the difference between do while and for loop?

do { //statements }while(condition); The statements inside the block get executed at-least once, no matter what condition you have placed. Only from the 2nd time the condition is checked, simply because the condition is at the last. for(initialization; condition; updation) { //statements } Here the statements don't get executed even once if the condition fails initially. The condition is at the entry itself.


A loop control variable that is incremented a specific number of times is known as?

A loop control variable is widly known as a "counter".


Is an infinite loop an example of a syntax error?

No. A syntax error is a statement that fails to compile. Infinite loops are simply loops for which the number of iterations is unknown. However, all loops, whether counted loops or infinite loops, must have a reachable exit condition. If a loop does not have a reachable exit condition then it is a logic error, not a syntax error.