*

1. For example, the opening

The print out your name so simple is not. A very simple programs written.

Bạn đang xem: Vòng lặp for trong c

#include int main() printf("Nguyen Van Quan "); return 0;Ok. So now I want you to print out 10 times your name?

#include int main() printf("Nguyen Van Quan "); printf("Nguyen Van Quan "); printf("Nguyen Van Quan "); printf("Nguyen Van Quan "); printf("Nguyen Van Quan "); printf("Nguyen Van Quan "); printf("Nguyen Van Quan "); printf("Nguyen Van Quan "); printf("Nguyen Van Quan "); printf("Nguyen Van Quan "); return 0;Pretty simple, just copy the 10 printf line is finished. But if not 10, that is 1000 or more? Obviously copying is not feasible.

In reality, the software will be work to lớn be repeated many times as in the example above. Or like your software khổng lồ print a list of employees, every employee information (content) different but the structure is very similar. Or simply that you surf facebook & see stories from your friends, the news was not identical in nội dung, but the structure is very similar.

To accomplish the tasks that need repeated such, we will use the loop structure consisting for, while, do-while. This lesson we will learn about the loop for.

We will revise the example in 10 once named you as follows:

#include int main(){ int i; for (i = 1; i As the code above sầu, I"ve used loop khổng lồ vì the job in his name 10 time, with 1000 simple times when I also replaced some 10 a number 1000 (you can try). Looking at the code you can also visualize the basic things we bởi.

2. Loop structure

*

Syntax, flowcharts, & the behavior of the loop

Looking at the picture above sầu we see the loop has a syntax and how it works is quite clear và easy lớn understand. However you should note:

After the comm& for no semicolon.3 Expressions of loops separated by semicolons (;), expressions may be absent 1,2 or both 3 but this must be raised semicolon.Expressions 1 always be calculated only once when dialed for.Expressions 2, 3 & for possible relatives repeatedly.Expressions 1, 2, 3 must be separated by a semicolon ( ; ), expressions may be absent 1,2 or both 3 but this must be raised semicolon.If the expression 2 no, loop is considered to be always right. Want lớn escape the loop in order lớn take a break, golớn hoặc return.For each expression can be written as a series of human expressions separated by commas. Then the child is determined expression from left to right. As of right và wrong in the expression of the sequence first expression 2 is determined by the final expression.In the body for (bloông xã command) may contain one or more other control structures, another loop.When having a break statement, deepest loop structure will exit.In the toàn thân there for thedung goto lớn exit the loop to lớn the desired location.In the body toàn thân can use for return to return khổng lồ a certain function.In the body can use for the command continue to lớn move sầu lớn the top of the loop (skip the remaining statements in the body).

Return code examples:

#include int main(){ int i; for (i = 1; i The process works as follows:

Step 1: Running expressions 1 ie assign i = 1.Step 2: Running expressions 2 ie kiểm tra i Run command blochồng that is printed name.Step 3: Running expressions 3 ie an increase of i up 1 unit. Now i = 2.Step 4: Baông xã expression 2. Check i Run commvà block, ie in the name.Step 5: Running expressions 3 ie increase i up 1. Now i = 3.Step 6. Bachồng expression 2,…Step….step x: Running expressions 3, i increase up 1. Now i = 10.Step x + 1: Back expression 2 ie kiểm tra i Run comm& blochồng print name.Step x + 2: Running expressions 3, i increase up 1. Now i = 11.Step x + 3: Baông xã expression 2, kiểm tra i Exit loop. Next persize peer comm& loop (return statement 0 underneath).

Ok. Now you have the basic understanding of loop. Continue to lớn vị a few more examples.

Example 1:


Write a program that prints out 15 times your name, together with the following order:

Nguyen Van QuanNguyen Van QuanNguyen Van Quan ….

Hum… how to derive the number ascending? You can look at the process of running the steps of the for loop? We have sầu 1 i turn on ascending… So we will take advantage of it.

#include int main(){ int i; for (i = 1; i

Example 2:

Please print out positive sầu numbers even from 1 to lớn trăng tròn on top of 1 current, separated by spaces.

Just for i running from 1 lớn 20 and we will check the printed number is even,. To kiểm tra the parity, we ređiện thoại tư vấn the division took balances. If i divide 2 residual 0 shall be even.

#include int main(){ int i; for (i = 1; i Ok. But I want you to lớn think a little more before looking down and code hints. How could we not use demo if even numbers và still vì chưng khổng lồ this post?

Remember the expression of for not fixed, we can change it. And change the way increase i. We give sầu i start 2 và each increase, the rise 2 unit.

#include int main(){ int i; for (i = 2; i This code clearly better than the previous time code. Above sầu we increase i up 2 command unit i = i + 2.This is an assignment i + 2 for i, not compare i & i command + 2.

Example 3:

Summed even integer from 1 lớn trăng tròn.

#include int main(){ int i; int s = 0; for (i = 2; i Notice in the above sầu code, we declare 1 variable s (stands sum) lớn save total. Every time i increases, we accrued lớn the s comm& s = s + in.

Example 4:


Sum of odd numbers from 1 to lớn 20 but until the total exceeds 15 then stop.

Here we are summing the odd numbers, like the even numbers only, however when the total is greater than 15 then we will stop, plus no more.

#include int main(){ int i; int s = 0; for (i = 1; i Simply kiểm tra when s still less than or equal 15 then we added. We will have s = 1 + 3 + 5 + 7 = 16.

However if you notice, when s> 15, we are not added to the loop"s but still running until i> 20. That is going khổng lồ run lớn the iteration i = 9, 11, 13, 15, 17, 19. Therefore it makes waste, superfluous & make programs run longer. So we should break the loop at the time the commvà is done when i = 7. To bởi so can be used break, or goto, but you should use break recommover for simplicity và safety.

#include int main(){ int i; int s = 0; for (i = 1; i

Example 5

Enter integer n, sum of even numbers entered.

#include int main(){ int i, n, x; int s = 0; printf("Enter n = "); scanf("%d", &n); for (i = 1; i In this example, I instruct you how to use the comm& continue. This commvà does not escape capital effects loop but is returning to lớn the expression 3 without orders behind it. Here if x does not even enter the command skip Incremental commvà s which runs to increase i right.

Xem thêm: Lmht: 'Thời Khắc Cuối Cùng' Của Mùa Xếp Hạng 2016 Sắp Tới

Exercise:

Write a program to print out the number n và the number n is estimated that.Write test program 1 there is prime number not? Prime number is a positive integer only 2 Convention is 1 and itself. Example No. 2, 3, 5, …Write test program 1 there is the perfect number does not? Perfect number is a positive integer equal the sum of the estimated 2 times it. Examples of 6 the Convention 1, 2, 3, 6 & total 1 + 2 + 3 + 6 = 12 (equal 2 time 6).Write a program lớn calculate S = 1 + 1/2 + 1/3 + … + 1/NWrite a program that calculates the squares of the odd numbers from 1 to n.Write a program khổng lồ calculate n! know n! = 1.2.3.4…nWrite a program that counts the n th Fibonacci sequence known sequence f(n) = f(A-1) + f(n-2), n > 2 & f(1) = 1, f(2) = 1.Write a program in length, the width of the rectangle. Draw a rectangle asterisk (*) kích cỡ entered.