- ..clause 1..: Initialization and condition
- ..clause 2..: Indefinitely ・・・
- ..clause 3..: Compulsion escape
Though it explained the repetition (loop) can be achieved by the for sentence in the foregoing paragraph
Here, it explains the mechanism of the operation of the for sentence in detail.
The usage of a more concrete for sentence is as follows.
Initialization is a sentence to initialize the count variable.for (Initialization;Conditional expression; Update) Repeated sentence; }
The conditional expression is a sentence to set the end condition of the loop.
Keep executing the sentence that the value of the expression written here repeats during the truth.
The update is a sentence to update the count variable.
After the repeated sentence is executed, the expression written here is executed.
The operation of the program made in the foregoing paragraph is examined based on this.
Here, the expression of initialization : It is i=1.#include <stdio.h> int main(void) { int i; Printf (" message \n"); } return 0; }
Next, compare conditional expressions.
The result of i < =10 becomes true as the value of i is one at this stage.
As a result, will keep still executing the loop.
Next, execute the repeated sentence. Here The printf sentence is executed.
Next, the expression of the update is executed. Though the value of i was one till then
One increase of the value of i because the expression of this update is i++ and it becomes two.
++ It must see and the person who has forgotten the operator must recall clause 6 of Chapter 5 paragraph 1.
Repeat this execution of sentence → update that repeats conditional expression → many times.
When i becomes 11 The condition of i < =10 becomes an imitation and it will slip out the loop.
In repeating until the condition becomes an imitation while changing the value of the count variable in this manner
The loop processing of a decided frequency has been achieved.
In the for sentence, you may put what expression on the part of initialization, the conditional expression, and the update.
Do not care about an expression that decreases one every times or is irrelevant either.
However, it is easy to use the first expression like the foregoing paragraph for the achievement of the loop of a decided frequency.
In the for sentence, you may not put the expression far from your may being to put what expression.
The following program is for sentence where the expression is not put at all.
However, we will recommend this program not to be executed.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { For (;;) Printf (message ..".. \n ...."); } return 0; }
This program keeps infinitely displaying the message.
Message
Message
Message
Message
Message
・
・
・
Because this omitted the conditional expression, the part is always judged that the truth.
It is because of falling into the state to execute the repetition for a long, long time.
The loop infinitely executed is called an infinite loop in this manner.
Actually, this infinite loop is a technique used very widely.
[ Infinite loop ]
Program that repeats the same operation infinitely
In a broad sense, all the applications are composed of an infinite loop.
Though it is usual that the for sentence ends when the conditional expression becomes an imitation
If the break sentence is executed in the for sentence, the for sentence is compulsorily ended.
The count variable becomes like the value at that time.
The following program is an example of ending the loop by the break sentence.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { int i; for (i = 1;i <= 10;i++) { printf("%d\n",i); if (i == 3) break; /* End the loop */. } return 0; }
Though it is sure not to end until displaying to see the conditional expression ten times
1
2
3
The break sentence : simultaneously with the condition of the for sentence because it can use it.
Use it to end when the error occurs while repeating.