"21 days Learn C + +" study notes 7th Chapter control procedure

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.