Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vòng lặp:
- 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.
- for(initialize counter; relational expression; re-evaluation counter)
- (khởi tạo biến đếm; điều kiện; tăng/giảm biến đếm)
- {
- statements
- }
- Initialize conter is an assignment statement that sets the loop control variable, before entering the loop
- Relational expression determines when the loop will exit. It's the expression of the counter
- Re-evaluation counter defines how the loop control variable changes, each time the loop is executed
- E.g:
- int i;
- for(i=0;1<10000;i++)
- printf("\nA1205m")
- The scope of the for loop can be extended by including more than one initializations or increment expressions in the for loop specification
- for(1 , 2);
- The for loop will be termed as a nested for loop when it is written as follows
- for(i=1;i<max1;i++)
- {
- .
- .
- for(j=0;j<=max2;j++)
- {
- .
- .
- }
- .
- .
- }
- E.g:
- int i,j;
- for(i=0;i<100;i++) // 1i=100j loop
- for(j=0;j<100;j++) // total of 100^2
- printf("\nsumthin");
- -----------------------------------------------------------------------------------------
- THUC HANH
- #include <stdio.h>
- #include <conio.h>
- #include <math.h>
- int main()
- {
- int i,dem=0,j,n,k;
- /*
- // for(i=0;i<10;i++)
- // for(i=12;i>=0;i-=2)
- for(i=20;i>=0;i--)
- if(i%2!=0)
- // printf("\n%d",i);
- {
- for(j=0;j<dem;j++)
- printf(" ");
- // printf("\t");
- printf("%d\n\n",i);
- dem++;
- }
- // printf("\na1205m");
- getch();
- */
- /* printf("\nEnter some shit: ");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- for(j=0;j<=i;j++)
- printf("*");
- printf("\n");
- }
- */
- //Triagle STUFF
- /* printf("\nEnter sum shit:");
- scanf("%d",&n);
- for(i=n;i>0;i--)
- {
- for(j=0;j<i;j++)
- printf("*");
- printf("\n");
- }
- for(i=0;i<n;i++)
- {
- for(j=0;j<=i;j++)
- printf("*");
- printf("\n");
- }
- */
- //Square triagle
- /*printf("\ninsert shit ");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- for(k=0;k<n-1-i;k++) //Cong thuc in tam giac vuong: n-1-i
- printf(" ");
- for(j=0;j<=i;j++)
- printf("*");
- printf("\n");
- }
- */
- //TAM GIAC CAN
- printf("\ninsert shit ");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- for(k=0;k<n-1-i;k++)
- printf(" ");
- for(j=0;j<2*i+1;k++)
- printf("*");
- printf("\n");
- }
- getch();
- }
- //tam giac can, so le: 2*n+-1
Add Comment
Please, Sign In to add comment