1. Iteration means repeating the same work. The main method of iteration is looping.
2, while () loop, Do...while () loop, for Loop,
3, note that the Do...while () loop do is really going to appear, not just a sign:
1#include <iostream>2 intMain ()3 {4 using namespacestd;5 intcounter;6cout<<"How many hellows";7Cin>>counter;8 Do //Note here do must not be less, is the basic format problem9 {Tencout<<"hello\n"; Onecounter--; A } - while(counter>0); -cout<<"Counter is:"<<counter<<Endl; the return 0; -}
4. Two advanced usages of the For loop: less common:
①for (;;) Loop, which is equivalent to a while (true) loop, where you need to set the loop initialization,test,action elsewhere.
Use empty statement in ②for statement: for (;counter<5;) is equivalent to a while loop while (COUNTER<5)
To use an empty loop body in a ③for loop:
#include <iostream>
int main ()
{
for (int i=0;i<5;std::cout<< "I:" <<i++<<std::endl)
, or//empty loop body, as long as the sub-good and the circulation head corresponding to the good
return 0;
}
5, using the loop instead of the recursive implementation of the Fibonacci sequence, the algorithm complexity is much less than the recursive method:
1#include <iostream>2#include <iomanip>3 using namespacestd;4 intFibonacci (intposition);5 intMain ()6 {7 intposition;8cout<<"Please input the position:";9Cin>>position;Tencout<<"The Fibonacci number in position"<<position<<"is :"<<fibonacci (position) <<Endl; OneSystem"PAUSE"); A return 0; - } - the intFibonacci (intposition) - { - intminustwo=1, minusone=1, answer=2; - if(position<3) + { -Answer=1; + } A if(position==3) at { -Answer=1; - } - for(;p osition>3;p osition--) - { -minustwo=Minusone; inMinusone=answer; -Answer=minusone+Minustwo; to } + returnanswer; -}
View Code
"21 days Learn C + +" study notes 7th Chapter control procedure