// for if wh
#include <iostream>
#include <string>
#include <ctime>
int main()
{
using namespace std;
//5.1for
string word = "fdsfsdf";// physically modify string object
char temp;
int i, j;
for (j = 0, i = word.size() - 1; j < i; --i, ++j)//for (int i = word.size() - 1; i >= 0; i--)
{ // start block
temp = word[i];
word[i] = word[j]; //cout << word[i];
word[j] = temp; //swap
} // end block
cout << word << "\nDone\n";
int quizscores[10] =
{ 20, 20, 20, 20, 20, 19, 20, 18, 20, 20 };
cout << "Doing it right:\n";
int ii;
for (ii = 0; quizscores[ii] == 20; ii++)
cout << "quiz " << ii << " is a 20\n";//显示前5个值为20
cout << "Doing it dangerously wrong:\n";
for (ii = 0; quizscores[ii] = 20; ii++) //已知循环,崩了
cout << "quiz " << ii << " is a 20\n";
string word = "?ate";// char word[5] = "?ate";
for (char ch = 'a'; word != "mate"; ch++)//strcmp(word, "mate")
{
cout << word << endl;
word[0] = ch;
}
cout << "After loop ends, word is " << word << endl;
//5.2while
cout << "Enter the delay time, in seconds: ";//delay time
float secs = 5.0;
clock_t delay = secs * CLOCKS_PER_SEC; // convert to clock ticks
cout << "starting\a\n";
clock_t start = clock();
while (clock() - start < delay) // wait until time elapses
; // note the semicolon
cout << "done \a\n";
//5.3do while
do
{
cin >> n; // execute body
} while (n != 7); // then test
cout << "Yes, 7 is my favorite.\n";
5.4 for C++11
5.5循环与文本输入
5.6 嵌套循环和二维array
const char * cities[Cities] = // == char cities[Cities][25]==const string cities[Cities]
{ // to 5 strings
"Gribble City",
"Gribbletown",
"New Gribble",
"San Gribble",
"Gribble Vista"
};
int maxtemps[Years][Cities] = // 2-D array
{
{96, 100, 87, 101, 105}, // values for maxtemps[0]
{96, 98, 91, 107, 104}, // values for maxtemps[1]
{97, 101, 93, 108, 107}, // values for maxtemps[2]
{98, 103, 95, 109, 108} // values for maxtemps[3]
};
cout << "Maximum temperatures for 2008 - 2011\n\n";
for (int city = 0; city < Cities; ++city)
{
cout << cities[city] << ":\t";
for (int year = 0; year < Years; ++year)
cout << maxtemps[year][city] << "\t";
cout << endl;
}
cin.get();
return 0;
}
chapter5循环
最新推荐文章于 2023-12-19 18:43:45 发布