C++编程基础一 26-do while循环

 1 // 26-do while循环.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <climits>
 7 #include <array>
 8 #include <string>
 9 using namespace std;
10 
11 int main()
12 {
13     //do {
14     //    循环体
15     //} while (判断);
16  
17     int i = 100;
18     do {
19         cout << "创建敌人" << endl;  //先运行后判断,只执行了一次。
20     } while (i>100);
21 
22     while (i>100)
23     {
24         cout << "创建敌人" << endl;  //先判断后执行,没有执行代码。
25     }
26     
27     //while 执行次数大于等于0,do while执行次数大于1。
28 
29     //利用for循环简单的遍历数组
30     int scores[] = { 34,245,6,72,742,3,763,12,};
31     for (int i=0;i<8;i++) 
32     {
33         cout << scores[i] << endl;
34     }
35 
36     //C++11简单的方式遍历数组
37     for (int temp : scores)  //只能取temp对应的值,不能设置值,如果需要设置temp对应的值,写成(int& temp:scores)
38     {
39         //cout << temp * 2 << endl;//没有效果。数组中所有的值没有变化。 
40         cout << temp << endl; 
41     }
42 
43     for (int& temp : scores) //加了&后表示temp是每个数组中的引用,有了这个引用后就可以对temp中对应的值进行修改。
44     {
45         cout << temp * 2 << endl; //数组中所有的值都扩大2倍了!
46     }
47     
48 
49     int t;
50     cin >> t;
51     return 0;
52 }

猜你喜欢

转载自www.cnblogs.com/uimodel/p/9346592.html
今日推荐