For
//Loops
#include<iostream>
/*
int main() {
using namespace std;
int i;
for (i = 0; i < 5; i++) {
cout << "C++ knows loops.\n";
}
cout << "C++ knows when to stop.\n";
return 0;
}
*/
//for循环包含了初始值,测试,循环操作,循环更新
//i=0 初始值,只执行一次
//i<5 测试条件
//i++ 更新操作
//cout... 循环操作
/*
int main() {
using namespace std;
cout << "Enter the starting countdown value:";
int limit;
cin >> limit;
int i;
for (i = limit; i; i--)
cout << "i = " << i << endl;
cout << "Now that i = " << i << endl;
return 0;
}
*/
//这段代码揭示了C++不限制测试条件的值为bool
//但会把测试结果强制转化为bool
//因此在i--至0时为false,循环结束
//for循环是入口条件循环,因此当输入0时
//测试条件为0被转化为false,不执行循环
/*
int main() {
const int ArSize=16;
long long factorials[ArSize];
factorials[1] = factorials[0] = 1LL;
for (int i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i - 1];
for (int i = 0; i < ArSize; i++)
std::cout << i << "! = " << factorials[i] << std::endl;
return 0;
}
*/
//修改步长
/*
int main() {
using namespace std;
cout << "Enter an integer: ";
int by;
cin >> by;
cout << "Counting by " << by << "s:\n";
for (int i = 0; i < 100; i = i + by)
cout << i << endl;
return 0;
}
*/
//倒序打印
/*
#include<string>
int main() {
using namespace std;
string word;
cin >> word;
for (int i = word.size(); i >= 0; i--)
cout << word[i];
cout << "\nThat's it.\n";
return 0;
}
*/
//递增递减
/*
int main() {
using namespace std;
int a = 20;
int b = 20;
cout << "a++ = " << a++ << endl;
cout << "a = " << a << endl;
cout << "++b = " << ++b << endl;
cout << "b = " << b << endl;
return 0;
}
*/
//a++的结果并不会参与当前运算,而是参加下次
//++b的结果会参与当前运算
//将(a和b)+1被定义为递增符的副作用
//在之前的代码中i++或++i从逻辑上没有区别
//因为在此之前i并未参与运算,只有副作用生效
While
#include<iostream>
/*
const int ArSize = 20;
int main() {
using namespace std;
char name[ArSize];
cout << "Your first name, please:";
cin >> name;
cout << "Here is your name, verticalized and ASCIIized:\n";
int i = 1;
while (name[i]!='\n') {
cout << name[i] << ": " << int(name[i]) << endl;
i=i+1;
}
for(int i=0;name[i]!='\0';i++)
cout << name[i] << ": " << int(name[i]) << endl;
return 0;
}
*/
//while测试条件默认返回0/false
//for测试条件默认返回1/true
/*
#include<ctime>
int main() {
using namespace std;
cout << "Enter the delay time, in seconds: ";
float secs;
cin >> secs;
clock_t delay = secs * CLOCKS_PER_SEC;
cout << "Starting\a\n";
clock_t start = clock();
while (clock() - start < delay) {
;
}
cout << "done \a\n";
return 0;
}
*/
dowhile
#include<iostream>
/*
int main() {
using namespace std;
int n;
cout << "Enter number in the range 1-10 to find ";
cout << "my favorite number\n";
do {
cin >> n;
} while (n != 7);
cout << "Yes! 7 is my lucky number!";
return 0;
}
*/
//for,while是入口条件循环
//dowhile是出口条件循环
//即在运行主体之后在进行判定
//如只有在输入账户后系统才会判定是否正确
循环输入与二维数组
#include<iostream>
/*
int main() {
using namespace std;
char ch;
int count = 0;
cout << "Enter characters; enter # to quit:\n";
//cin >> ch;
cin.get(ch);
//while (ch != '#')
//输入字符以#结尾
while (cin.fail() == false)
//cin.fail在键盘输入中模拟eof文件尾条件
//属于事后报告,所以放在读取后
//默认返回true
//ctrl+z为结束
//while(cin)
//while(!cin.fail())等效
{
cout << ch;
++count;
//cin >> ch;
cin.get(ch);
}
cout << endl << count << " characters read\n";
return 0;
}
*/
//cin.get()会读取输入的每一个字符,包括空符换行符等
//在windows中ctrl+z为仿真EOF
/*
int main() {
using namespace std;
int ch;
int count = 0;
while ((ch = cin.get()) != EOF)
//ch=cin.get()返回的是int
{
cout.put(char(ch));
++count;
}
cout << endl << count << " characters read\n";
return 0;
}
*/
const int Cities = 5;
const int Years = 4;
int main() {
using namespace std;
const char *cities[Cities] = { "A","B","C","D","E" };
int maxtemp[Years][Cities]{
{78,54,95,71,80},
{34,54,84,96,88},
{81,86,53,12,37},
{10,97,60,26,86},
};
cout << "Maximum temperatures for 2009-2012\n\n";
for (int city = 0; city < Cities; ++city) {
cout << cities[city] << ":\t";
for (int year = 0; year < Years; ++year) {
cout << maxtemp[year][city] << "\t";
}
cout << "\n";
}
return 0;
}