Saturday, June 20, 2015

[Programming book Chapter Four] Loop (Loop).

In the meantime, many different types of programs that (condition) have learned to use. These conditions may also be made with a variety of programs have started. Very well. But now we have to find another way to solve the problem and. No need to write a program, which is an integer from 1 up to 10 monitors show all (one per line number). The problem is very simple and very easy to solve. Here are all the numbers from 1 to 10 we have to print. However, on a printf () could be done without having to use: printf ("1 \ n2 \ n3 \ n4 \ n5 \ n6 \ n7 \ n8 \ n9 \ n10 \ n"); It was written in the program. n is an integer variable, the value of which we will set at 1. We will then print the value of n-. We will enhance the value of one of the n- (n = n + 1, or in short, n ++ type). int n = 1; printf ("% d \ n", n); n = n + 1; printf ("% d \ n", n); n = n + 1; printf ("% d \ n", n); n = n + 1; / * In this way, a total of ten times * / But one sentence it once again to increase the value of n 1 can be cured. printf ("% d \ n", n); n = n + 1; Instead, we can write: printf ("% d \ n", n ++); However, he went on to print from 1 to 10. But if 1 in 100, or 1000, or 10000 if we would have been called up to print? The method has been adopted over the course would be. But I know that one of you will not agree to so much trouble. To solve the problem at all programming lyanguyejei loop (loop) as a method. The same can be done with it repeatedly. To sit in the loop is a condition, in which the inner loop until the program can be repeated. Two popular loop while being coded in C and for. We are now using while I write the program. What's great! Now we want to put as much as you can instead of 10, so as to be printed from 1 to enter. while loop is to write the condition in parentheses. The program checks the condition. Until then loop until the condition is true, continues to work on the inside. The inner loop will be done within the brackets. We've done two things here, such as the inside of the loop. n- n-, then print the value of've increased the value of 1. If the rise in the value of n- 1 to 11, and then once it n <= 10 This condition is false (because 11> 10). The program will come out of the loop. In other words, the condition will be false when he will be out of the loop. Indentesanera concerning a note. Four of code inside of the loop to the right start. What it will print all the numbers from 1 to 10? Let's see. The program is to run. What is the output? 1 monitors the line and the program is being printed. Too bad. Let's see what is the reason behind the grief. int n = 1; N- of the program will take the value of 1. Then the while loop will check the condition. We have the condition n <= 10 means n- smaller or equal to the value of 10. This condition is true because the value of n- 1. The program will then print the value of n- printf ("% d \ n", n) ;. What n- 1, then increase the value? Not increase, because we have to brace '}' symbol (meaning the end of the loop). That means the program will check conditions, the n- n- because of the quality of the print ... this will continue, since the value is not increasing, n <= 10 condition has remained true all the time - do not ever lie. Now you can continue to study a different type of while loop. If all the conditions are true, you can use your computer to keep busy. while (1) {...} 1 has been used as a condition here. 1 computer that understands true. The inner loop will continue to do so all the time, will not stop. while (1 == 1) {...} and behaves the same. However, an important thing now, I would say, with which you will be forced out of the loop. The statement of the break. What will this program? 1 to 10 will be printed. We have said that out of the while- condition n <= 100, but have to say that again in the loop if n> 10, then break; This means to get out, or give lupati broke. break out of the loop if the program comes out of it. So n- increase the value of its value after 10 prints (n ++;) n- of the standard 11. And when n> 10 is true, if the conditions of the program can be entered. While there, he was asked to break him, so he will be out of the loop. break- works versa, is a statement that continue ;. If you use a loop in place, continue to the next part of continue- are no longer working. This program is only the odd numbers between 1 and 10 will print. Even at the numbers continue to use the program printf ("% d \ n", n); This statement is not executed, the loop will begin the next phase. Then we will write another program. Namatagulo that you learned as a child that we are now entering the program will look at computer monitors. Let's start with the multiplication table of 5. Our program will output like this: 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 5 X 6 = 30 5 X 7 = 35 5 X 8 = 40 5 X 9 = 45 5 X 10 = 50 Indeed, for some of you will start writing printf function. The program will be written in a loop. So far, we have used while loop. Let's learn to use the for loop. 5-in table to write the program using for loop, then we will look at it. The first of three parts of the brackets for a loop. Each of the semi-colon (;) to separate. The first semicolon before the loop is entered into the program, we will be able to do that, they will once. Here i- 1 set up as the standard. Then the second part will work. The second part is commonly used terms (while loop the first thing we do inside the brackets originally). The second part of the above program, we i <= 10've used condition. If the condition is false, the program will come out of the loop. And if it is true that they may do the inner loop and the loop for the third part of the first bracket, which has been asked to do that. Then came the second part of the test conditions. The first part, but not anymore. As noted in our program again. i <= 10 is true, because the value of i- 1. Then printf () function will work. Then i = i + 1 statement will be executed (i- of its value can be increased). Then again i <= 10 true or false, it will be examined (i-'s value is now 2). Then again, the inner loop will work (printf ()). Thus, until i <= 10 the inner loop will continue to operate until the condition is false. i-'s value increased by one to 11, then increased when the condition is false, and the program will come out of the loop. The first three parts of the inner loop for the brackets that do not have to be used. If you do not use any part we can leave it blank, but must pay the semicolon. Before we set the value of the i- do not, however, even though it is in the loop. Now we'll look for another job. 5 times that of the for loop namataya've (n * i) it does not add to the program only to write. You wonder what the quality of the program, except that the table will this happen? But we can not write 5 x 3-to 5 + 5 + 5. Surely you understand what I'm going to do. We have added to the quality of the program. Continue to compile and run. Will work well. Gunitakagulo any number of times out, as can be, so that you can add. If we see that the program does not add to the quality of work is being done, then it is better to add processors to a computer to add to that because it takes time, it takes a lot more time than a multiplication. Although you may not understand it at the time to run the program. Detailed studies of the computer's processor will be able to learn about it. Janalei it will now add a quality than a good, the less time it takes the computer to add. We did learn that for loop. Now we'll just try to do the multiplication table by entering a number from 1 to 20 to enter all the numbers at the table. N- of the value of 5 to 20 to 1 is not specified. This is a simple method (or methods back?) To write the table to be part of the repeated copying. But we'll do it in a loop using the loop. N- a loop of the phasing out of value from 1 to 20. And for a certain quality of its inner n- type namatata. Now for the program. If you do not then simply add quality to use the program. We are here for another loop inside a for loop, which nested lupao (nested loop) say, I used it. So we asked for or while loop or while loop inside a for loop more than once in a while for or can use. However, four or five times more than it really does not need to be. We are now going to write another program with nested loop. 1, 2, 3 - all of these three formats (permutation) to the program. From small to large order will show the layout of the program will output like this: 1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1 This program can be written in many ways, but we have now learned the extent of the programming, and the best solution to the use of nested loop. Here we have a loop for the first number, the second number in the first loop, the second loop of a loop and another loop will use the third number. Now run the program, we get the same output: 1, 1, 1 1, 1, 2 1, 1, 3 1, 2, 1 1, 2, 2 1, 2, 3 1, 3, 1 1, 3, 2 1, 3, 3 2, 1, 1 2, 1, 2 2, 1, 3 2, 2, 1 2, 2, 2 2, 2, 3 2, 3, 1 2, 3, 2 2, 3, 3 3, 1, 1 3, 1, 2 3, 1, 3 3, 2, 1 3, 2, 2 3, 2, 3 3, 3, 1 3, 3, 2 3, 3, 3 But we really do not want this kind of thing. When the value of a- and b and c- of the value to be 1, not 1, the standard will not be equal to b and c-. This means that a, b and c will be different. We will then loop through the conditions changed a bit. The second loop condition b <= 3-of'll come across another condition, b! = A. b <= 3 && b! = a means b- value of less than or equal to 3, the value of the a- and b- values ​​will not be equal. We now have the conditions of the third loop, c <= 3 && c! = A && c! = B, c- the mean value of 3 will be smaller or equal to the value of the a- and c- and c's value should not be equal Although the quality of the work is equal to the value of b- natahale our program will look like this: When you run, we'll see output? 3, 2, 1 Just a line! The program seems to have the ball, we just go to the trouble. Think you can out of trouble? A- 1, so the value of a <= 3 true. The program first went into the midst of the loop. B- At the beginning of the second loop, the value of 1. b <= 3 true. But b! = A lie. It is equal to the value of a and b-, both values ​​1. So the program does not go through the second loop. 1, the reduction in the value of a- (a ++). a <= 3 true (a-'s standard 2). Now the second loop will start. b- value of 1. Here b <= 3 and b! = A two conditions are true. The program will be entered into the second loop. At the beginning of the third loop of c- 1. c <= 3 true, c! = a fact, but c! ​​= b false (both values ​​1). So the program is out of the loop when the condition is true, only three yabe program and passing the loop through the a, b and c- of the standard print. Thus, if a little research you will see that the value of the a- 3, b- and c- of the value of the standard 2 to 1, then the condition is true, and we only get output: 3, 2, 1. A- b- value of the second loop we are equal to the values ​​that are going to be out of the loop. That is just not done. If the two values ​​are equal, we need to try to make the next quality. And only when the two are not equal to the task. So we can write: for (b = 1; b <= 3; b ++) {if (b! = A) {/ * b- if the value is not equal to the quality of a- into the inner part of the program. * / For (c = 1; c <= 3; c ++) {if (c! = A && c! = B) {/ * c- if not equal to the value of the a- and c- of the quality of the value of b- into the inner part of the program only when the quality is not equal. * / Printf ("% d,% d,% d \ n", a, b, c); }}}} If you stand in our whole program looks like this: program running, we'll see the following output, which we wanted. 1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1 Well, finally the solution to our problems. We are not here to think. Only when printing a, b, c of the three values ​​are not equal to each other, it is sure to recommend it! Good idea, but it is less than our previous work with the computer program, so it takes less time to move, or in the words of computer run time (run time) low. How long does it take for a program actually depends largely on the number of program assignments operations (assignment operation) the operation of the many comparisons (comparison operation) took over.

No comments:

Post a Comment