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.Though it is also possible to request this calculation mathematically
..".. 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?
In short, though it understands when this is seenRepeated sentence; }
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.
The execution result of this program becomes as follows.#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; }
First of all, when you say why to calculate by the double type
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.
The point of this program is in the part of the condition.
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.
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.
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.
The loop of times of the constant can be achieved even by the while sentence if it does in this manner.Initialization; Repeated FONT>sentence; 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.