Question #6882   Submitted by Answiki on 10/12/2022 at 01:17:41 PM UTC

Syntax of a for loop in C?

Answer   Submitted by Answiki on 10/12/2022 at 01:26:47 PM UTC

In C, the for loop is used to repeat a block of instructions several times. It has three parameters:

  • an initialization which is usually used to assign a value to a variable;
  • a condition which is a test that will determine when the loop should stop;
  • an iteration which is an operation that specifies the step of the loop.


Syntax:

for (initialization ; test ; iteration)
{
	// Repeated code
}

Flowchart:

Example :

for (i=0 ; i<10 ; i++)
{
	printf ("%d\n", i);
}

Try online on repl.it.


The parameters of the loop are optional. The following code is an infinite loop, equivalent to while(1); :

for (;;);

See also:


3 events in history
Answer by Answiki on 10/12/2022 at 01:26:47 PM

In C, the for loop is used to repeat a block of instructions several times. It has three parameters:

  • an initialization which is usually used to assign a value to a variable;
  • a condition which is a test that will determine when the loop should stop;
  • an iteration which is an operation that specifies the step of the loop.


Syntax:

for (initialization ; test ; iteration)
{
	// Repeated code
}

Flowchart:

Example :

for (i=0 ; i<10 ; i++)
{
	printf ("%d\n", i);
}

Try online on repl.it.


The parameters of the loop are optional. The following code is an infinite loop, equivalent to while(1); :

for (;;);

See also:


Answer by Answiki on 10/12/2022 at 01:25:04 PM

In C, the for loop is used to repeat a block of instructions several times. It has three parameters:

  • an initialization which is usually used to assign a value to a variable;
  • a condition which is a test that will determine when the loop should stop;
  • an iteration which is an operation that specifies the step of the loop.


Syntax:

for (initialization ; test ; iteration)
{
	// Repeated code
}

Flowchart:

Example :

for (i=0 ; i<10 ; i++)
{
	printf ("%d\n", i);
}

Try online on repl.it.


The parameters of the loop are optional. The following code is an infinite loop, equivalent to while(1); :

for (;;);

See also:


Question by Answiki 10/12/2022 at 01:17:41 PM
Syntax of a for loop in C?
# ID Query URL Count

Icons proudly provided by Friconix.