Minggu, Juli 06, 2014

while Looping On C Programming Language

Syntax for while loop on C programming language:

while (condition)
{
  statement_will_be_repeated;
}


Example of while looping on C programming language:

main()
{
  int a=0;
  while (a<2)
  {
    printf("Test\n");
    a=a+1;
  }
  getch();
}

  • First, a valued 0, and 0<2, it means qualify condition for a<2, then printed Test word and add value of a from 0 becomes 1 (a=a+1, a=0+1, a=1)
  • Now, a valued 1, and 1<2, it means still qualify condition for a<2, then printed Test word and add value of a from 1 becomes 2 (a=a+1, a=1+1, a=2)
  • Now, a valued 2, and 2 is not small than 2, it means does not qualify condition for a<2, then not printed Test word and add value of a from 2 becomes 3
So printed 2 Test word

If looping conditional is changed from 1<2 becomes a<=2, so that program syntax becomes:

main()
{
  int a=0;
  while (a<=2)
  {
    printf("Test\n");
    a=a+1;
  }
  getch();
}

  • First, a valued 0, and 0<2, it means qualify for a<=2, then printed Test word and add value of a from 0 becomes 1 (a=a+1, a=0+1, a=1)
  • First, a valued 1, and 1<2, it means qualify for a<=2, then printed Test word and add value of a from 1 becomes 2 (a=a+1, a=1+1, a=2)
  • First, a valued 2, and 2<2, it means qualify for a<=2, then printed Test word and add value of a from 2 becomes 3 (a=a+1, a=2+1, a=3)
  • First, a valued 3, and 3 is not < 2, it means does not qualify for a<=2, then not printed Test word and add value of a from 3 becomes 4
So printed 3 Test words

Can be used looping conditional large than (>):

main()
{
  int a=7;
  while (a>=5)
  {
    printf("Test\n");
    a=a-1;
  }
  getch();
}


Thank you... : )

0 komentar: