For Statements

For statements allow for execution of a specific instruction/block of code until the condition specified evaluates to false. If the condition is initially false, then the instruction(s) will not be executed. For loops allow initial conditions to be set within the statement. For statements are typically used when a set number of loops will always be executed. *Note* The variable should be declared at the beginning of the program and not within the for statement.

Syntax

  1. for(initial condition; breaking condition;variable delta statement)
    1. Instruction;

--or--

  1. while(initial condition; breaking condition;variable delta statement)
  2. {
    1. Instruction Block
  3. }

Example

  1. void main()
  2. {
    1. int a;
    2. for(a=0; a < 5; a++)
    3. {
      1. printf("\nThe value of A is %d", a);
      2. a = a + 1;
    4. }
    5. for(a=4; a != 0; a = a - 1)
      1. printf("\nSince A=5, this will be printed");
  3. }