※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Mechanism of loop operation   

  1. ..clause 1..: Initialization and condition
  2. ..clause 2..: Indefinitely ・・・
  3. ..clause 3..: Compulsion escape

[1] Initialization and condition

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.

 
for (Initialization;Conditional expression;
Update)  Repeated sentence;
}
Initialization is a sentence to initialize the count variable.
The expression written here is executed only once first.

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.

 
#include <stdio.h> 

int main(void)  
{
	int i;  
	 Printf (" message \n");
	}
	return 0;
}
Here, the expression of initialization : It is i=1.
I becomes one at the time of beginning of the loop because this expression is executed only once first.

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.

It returns to contents.


[2] Indefinitely ・・・

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.

 
#include <stdio.h> 

int main(void) 
{
	For (;;) Printf (message ..".. 
		\n ....");
	}
	return 0; 
}
The execution result of this program becomes as follows.

Message
Message
Message
Message
Message
    ・
    ・
    ・
This program keeps infinitely displaying the message.
Never end as long as the program is not canceled.

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.


[   Infinite loop   ]
Program that repeats the same operation infinitely
In a broad sense, all the applications are composed of an infinite loop.
Actually, this infinite loop is a technique used very widely.
It understands if it thinks. For instance, in the application such as note pads
Processing of displaying it when the key is input
It repeats until the user ends it pushing × button through all eternity.

It returns to contents.


[3] Compulsion escape

Though it is usual that the for sentence ends when the conditional expression becomes an imitation
Actually, the for sentence can be ended without permission on the way.
Use the break sentence for it.

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.

 
#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;
}
The execution result of this program becomes as follows.

1
2
3
Though it is sure not to end until displaying to see the conditional expression ten times
When displaying it three times, it ends.
This is because the value of i becomes three, and the following break sentence of the if sentence was executed the third times.

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.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.