Jumat, Juli 04, 2014

for Looping On C Programming Language

Syntax for looping on C programming language is:

for(inisialisasi nilai; syarat perulangan; perubahan nilai)
{
  statement_will_be_repeated;
}


Example of for looping C program:

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


On program syntax is define that initial value of a is 0, looping condition is repeated if a<2, and variable update by add integer 1 using a++
  • a=0, now a valued 0 still small than or equals 2 (0<2), value of a is added 1 so that becomes 0+1 = 1, because on looping condition still qualify condition a<2, then printed Test word
  • a=1, now a valued 1 still small than or equals 2 (1<2), value of a is added 1 so that becomes 1+1 = 2, because on looping condition still qualify condition a<2, then printed Test word
  • a=2, now a valued 2 is not still small than (2<=2), value of a is added 1 so that becomes 2+1 = 3, because on looping condition is not qualify condition a<2, then not printed Test word
So printed 2 words Test

If looping condition a<2 is changed becomes a<=2, so that program syntax becomes:

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


On program syntax is define that initial value of a is 0, looping condition is repeated if a<2, and variable update by add integer 1 using a++
  • a=0, now a valued 0 still small than or equals 2 (0<=2), value of a is added 1 so that becomes 0+1 = 1, because on looping condition still qualify condition a<2, then printed Test word
  • a=1, now a valued 1 still small than or equals 2 (1<=2), value of a is added 1 so that becomes 1+1 = 2, because on looping condition still qualify condition a<2, then printed Test word
  • a=2, now a valued 2 still small than or equals 2 (2<=2), value of a is added 1 so that becomes 2+1 = 3, because on looping condition still qualify condition a<2, then printed Test word
  • a=3, now a valued 3 is not still small than or equals 2 (3≠2), value of a is added 1 so that becomes 3+1 = 4, because on looping condition is not still qualify condition a<=2, then not printed Test word
So printed 3 words Test

Can be used decrement operator do make variable update:

main()
{
  int a;
  for (a=0; a>=2; a--)
  {
    printf("Test\n");
  }
  getch();
}


So and so much... : )

0 komentar: