12

C++第七讲:for循环

递增

#include 
using namespace std;
 
int main ()
{
   for( int a = 1; a < 10; a = a + 1 )  //相当于python的 for i in range(1,10):
   {
       cout << "a 的值:" << a << endl;
   }
 
   return 0;
}

a = a +1 可以写成 a ++

递减

#include 
using namespace std;
 
int main ()
{
   for( int a = 10; a >=1; a = a-1 )  //10,9,8,7,6,5,4,3,2,1
   {
       cout << "a 的值:" << a << endl;
   }
 
   return 0;
}

设定循环的次数:

#include 
using namespace std;
 
int main ()
{
   for( int a = 1; a < 10; a = a + 1 )  //循环9次
   {
       cout << "*"<< endl;
   }
 
   return 0;
}


遍历指定数组中的元素并进行相应处理

#include
using namespace std;
int main()
{
	int nums[] = {1,2,3,4};
	for(int t : nums){    //相当于 for t in nums:
		cout<
13
发表评论
留言与评论(共有 0 条评论) “”
昵称:
匿名发表 登录账号
         
   
验证码:

相关文章

推荐文章

10
11