Guest User

Untitled

a guest
Dec 6th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. Vòng lặp:
  2. 1 đoạn lệnh (khối lệnh)nào đó lặp đi lặp lại nhiều lần cho đến khi 1 điều kiện xác định nào đó không còn thỏa mãn.
  3.  
  4. for(initialize counter; relational expression; re-evaluation counter)
  5. (khởi tạo biến đếm; điều kiện; tăng/giảm biến đếm)
  6. {
  7. statements
  8. }
  9. Initialize conter is an assignment statement that sets the loop control variable, before entering the loop
  10. Relational expression determines when the loop will exit. It's the expression of the counter
  11. Re-evaluation counter defines how the loop control variable changes, each time the loop is executed
  12.  
  13. E.g:
  14. int i;
  15. for(i=0;1<10000;i++)
  16. printf("\nA1205m")
  17.  
  18. The scope of the for loop can be extended by including more than one initializations or increment expressions in the for loop specification
  19. for(1 , 2);
  20.  
  21. The for loop will be termed as a nested for loop when it is written as follows
  22. for(i=1;i<max1;i++)
  23. {
  24. .
  25. .
  26. for(j=0;j<=max2;j++)
  27. {
  28. .
  29. .
  30. }
  31. .
  32. .
  33. }
  34.  
  35. E.g:
  36. int i,j;
  37. for(i=0;i<100;i++) // 1i=100j loop
  38. for(j=0;j<100;j++) // total of 100^2
  39. printf("\nsumthin");
  40.  
  41. -----------------------------------------------------------------------------------------
  42.  
  43. THUC HANH
  44.  
  45. #include <stdio.h>
  46. #include <conio.h>
  47. #include <math.h>
  48.  
  49. int main()
  50. {
  51. int i,dem=0,j,n,k;
  52. /*
  53. // for(i=0;i<10;i++)
  54. // for(i=12;i>=0;i-=2)
  55. for(i=20;i>=0;i--)
  56. if(i%2!=0)
  57. // printf("\n%d",i);
  58. {
  59. for(j=0;j<dem;j++)
  60. printf(" ");
  61. // printf("\t");
  62. printf("%d\n\n",i);
  63. dem++;
  64. }
  65. // printf("\na1205m");
  66.  
  67. getch();
  68. */
  69. /* printf("\nEnter some shit: ");
  70. scanf("%d",&n);
  71. for(i=0;i<n;i++)
  72. {
  73. for(j=0;j<=i;j++)
  74. printf("*");
  75. printf("\n");
  76. }
  77. */
  78.  
  79. //Triagle STUFF
  80. /* printf("\nEnter sum shit:");
  81. scanf("%d",&n);
  82. for(i=n;i>0;i--)
  83. {
  84. for(j=0;j<i;j++)
  85. printf("*");
  86. printf("\n");
  87. }
  88. for(i=0;i<n;i++)
  89. {
  90. for(j=0;j<=i;j++)
  91. printf("*");
  92. printf("\n");
  93. }
  94. */
  95.  
  96. //Square triagle
  97. /*printf("\ninsert shit ");
  98. scanf("%d",&n);
  99. for(i=0;i<n;i++)
  100. {
  101. for(k=0;k<n-1-i;k++) //Cong thuc in tam giac vuong: n-1-i
  102. printf(" ");
  103. for(j=0;j<=i;j++)
  104. printf("*");
  105. printf("\n");
  106. }
  107. */
  108.  
  109. //TAM GIAC CAN
  110. printf("\ninsert shit ");
  111. scanf("%d",&n);
  112. for(i=0;i<n;i++)
  113. {
  114. for(k=0;k<n-1-i;k++)
  115. printf(" ");
  116. for(j=0;j<2*i+1;k++)
  117. printf("*");
  118. printf("\n");
  119. }
  120. getch();
  121. }
  122.  
  123.  
  124. //tam giac can, so le: 2*n+-1
Add Comment
Please, Sign In to add comment