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


  Frequency uncertainty loop   

  1. ..clause 1..: Loop from which frequency is requested
  2. ..clause 2..: Exchangeability with for sentence

[1] Loop from which frequency is requested

Though the for sentence explains in the preceding chapter was a sentence that repeats only a decided frequency
On the contrary, I want occasionally will to know how many times only have to repeat.
The calculation of the geometric progression is its interesting example.

A certain grade-schooler asked parents.
..".. like one yen as for this month, two yen it as for next month, and four yen as for the month after next
Give the pocket money to me. "
Well, is it at month that the amount of money that parents pay exceeds one million yen how many?
Though it is also possible to request this calculation mathematically
This time, will calculate by using the loop.
It is possible to request by using the for sentence, and in such a case
While ([howairu]) sentence is more suitable than the for sentence.
In general, use the while sentence by the following writing.
 
Repeated  sentence;
}
In short, though it understands when this is seen
It is the same thing in case of the case to specify only the conditional expression by the for sentence.
Though the time of the constant loop can be easily written because initialization and the update can be used in the for sentence
When the frequency of the loop is not understood, the while sentence only of the conditional expression is convenient.

To calculate by the while sentence ahead
Specify to continue the repetition in fewer than one million yen to the condition of the while sentence.
Only have to double money by twice in the for statement.
The following program is an example of calculating this.

 
#include <stdio.h> 

int main(void)  
{
	double money = 1;  
	int month = 1;  
	
	 Printf ("%02d moon eyes: %7.0f yen \n" month and money);
		money *= 2;  
		month++;  
	}
	Become %7.0f yen, and ..%02d moon ..(.. ..".. eye of printf.. exceed one million 
	yen.  \n" month and money. );  
	
	return 0;
}
The execution result of this program becomes as follows.

Month 01:       One yen
Month 02:       Two yen
Month 03:       Four yen
Month 04:       Eight yen
Month 05:       16 yen
Month 06:       32 yen
Month 07:       64 yen
Month 08:     128 yen
Month 09:     256 yen
Month tenth:     512 yen
Month 11:     1024 yen
Month 12:     2048 yen
Month 13:     4096 yen
Month 14:     8192 yen
Month 15:   16384 yen
Month 16:   32768 yen
Month 17:   65536 yen
Month 18:   131072 yen
Month 19:   262144 yen
Month 20:   524288 yen
Month 21 Become 1048576 yen, and exceed one million yen.
First of all, when you say why to calculate by the double type
16 bit compiler The purpose is not to be able to store one million with LSI-C86 by the int type.
It is unquestionable in other compilers, and ..int type.. mend by each one.

Though it is possible to treat if long (length) type whose size is larger than the int type is used
Because in the program extent of the exercise, there are not so many things that need the long type
I will assume the explanation of the long type to be a deferment.
The point of this program is in the part of the condition.
As a condition Money < one million is specified.
The while sentence is between in a word smaller than one million yen money between the truth these conditions.
The for statement Keep executing the sentence of money*=2.
And, end the repetition when money exceeds one million yen.
And, display the excess of one million yen by the last printf sentence.

Even the for sentence and the while sentence : in this manner.
It is common in the point repeated while changing the variable specified for the condition.
Otherwise, it becomes an infinite loop like being the same the condition.

It returns to contents.


[2] Exchangeability with for sentence

The while sentence is the same only as the conditional expression of the for sentence as explained in the preceding clause.
It will be able to be said that the for sentence is a sentence to which the while sentence is enhanced if it says oppositely.

Actually, this two can do a similar usage.
Do as follows when you use the while sentence like the for sentence.


Initialization;
      Repeated sentence;
    Update;
}
The loop of times of the constant can be achieved even by the while sentence if it does in this manner.
Oppositely, when you use the for sentence like the while sentence
Only omit the sentence of initialization and the update.

However, use the for sentence for times of the constant to loop as a custom.
In the while sentence, using it to loop no so is most.
Use it properly well though they are two sentences that look like well.

It returns to contents.


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