While Statements

While 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. While statements should be used when it is not known ahead of time how many loops (if any) will occur.

Syntax

  1. while(condition)
    1. Instruction;

--or--

  1. while(condition)
  2. {
    1. Instruction Block
  3. }

Example

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