4-5. Do ... while loop

  1. do ... while loop and while most obvious difference
  • do ... while: After performing the first judgment condition
  • while: the first determination condition is performed
  1. #include<iostream>
    
    using namespace std;
    int main(int argc, char* argv[])
    {
    	//while(test_condition) body
    	//do body while(test_condition)
    	/*************************举例1*************************/
    	string s = "hellow world";
    	unsigned long size = s.size();
    	int i = 0;
    	do {
    		if (s[i] != ' ')
    		{
    			cout << "<" << s[i] << ">";
    		}
    		else
    		{
    			cout << s[i];
    		}
    		//i++;
    	}
    	while ((++i) < size);
    	cout << endl;
    	/*************************举例2*************************/
    	string ss = "I love you ";
    	auto p = begin(ss) ;
    	auto endp = end(ss);
    	do 
    	{
    		if (*p != ' ')
    		{
    			cout << "<" << *p << ">";
    		}
    		else
    		{
    			cout << *p;
    		}
    		//p++;
    	} while ((++p) != endp);
    	cout << endl;
    	return 0;
    }
    
  2. Use for loops, while loops or do ... while loop used in accordance with the actual situation is like, have advantages and disadvantages, be selected according to the actual situation, may be interchanged.

  3. In the above various cycle conditions moiety may be various types of written expression, the intermediate can be separated by commas.

Published 45 original articles · won praise 24 · views 3918

Guess you like

Origin blog.csdn.net/qq_43367829/article/details/105342509