1. 程式人生 > 其它 >c++ - break and continue用法

c++ - break and continue用法

技術標籤:C++

22 break and continue

#include <iostream>
#include <string>

int main()
{
  std::string sentence = "Hello my name is Jim";
  for(int i = 0; i < sentence.size(); i++)
    {
      std::cout << sentence[i] << std::endl;
      if(sentence[i] == 'o')
	{
	  std::cout << "found o!\n";
	  break; // break the closest loop
	}
    }
  
  return 0;
}

#include <iostream>
#include <string>

int main()
{
  std::string sentence = "Hello my name is Jim";
  for(int i = 0; i < sentence.size(); i++)
    {
      if(sentence[i] == 'o')
	{
	  continue; // jump this one continue the loop
	}
      std::cout << sentence[i] << std::endl;
    }
  
  return 0;
}