一、概述
二、内容
5.1、简单语句
5.2、语句作用域
5.3条件语句
else if嵌套
花括号
#include <iostream>
using std::cin; using std::cout; using std::endl;
int main()
{
// initialize counters for each vowel
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
while (cin >> ch) {
// if ch is a vowel, increment the appropriate counter
switch (ch) {
case 'a':
++aCnt;
break;
case 'e':
++eCnt;
break;
case 'i':
++iCnt;
break;
case 'o':
++oCnt;
break;
case 'u':
++uCnt;
break;
}
}
// print results
cout << "Number of vowel a: \t" << aCnt << '\n'
<< "Number of vowel e: \t" << eCnt << '\n'
<< "Number of vowel i: \t" << iCnt << '\n'
<< "Number of vowel o: \t" << oCnt << '\n'
<< "Number of vowel u: \t" << uCnt << endl;
return 0;
}
#include <iostream>
using std::cin; using std::cout; using std::endl;
int main()
{
char ch;
// initialize counters for each vowel
unsigned vowelCnt = 0;
unsigned otherCnt = 0; // count anything that isn't a vowel
while (cin >> ch) {
// if ch is a vowel, increment the appropriate counter
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
++vowelCnt;
break;
default:
++otherCnt;
break;
}
}
// print results
cout << "Number of vowels: \t" << vowelCnt << '\n'
<< "Total non-vowels : \t" << otherCnt << '\n';
return 0;
}
多行独立变量需要加大括号;
5.4、迭代语句
#include "Version_test.h"
#include <iostream>
using std::cout; using std::endl;
#include <vector>
using std::vector;
#include <string>
using std::string;
#ifndef LIST_INIT
#include <iterator>
using std::begin; using std::end;
#endif
int main()
{
vector<int> ivec;
#ifdef LIST_INIT
vector<int> v = {0,1,2,3,4,5,6,7,8,9};
#else
int temp[] = {0,1,2,3,4,5,6,7,8,9};
vector<int> v(begin(temp), end(temp));
#endif
// range variable must be a reference so we can write to the elements
for (auto &r : v) // for each element in v
r *= 2; // double the value of each element in v
// print every element in v
for (int r : v)
cout << r << " "; // print the elements in v
cout << endl;
return 0;
}
#include "Version_test.h"
#include <iostream>
using std::endl; using std::cin; using std::cout;
#include <vector>
using std::vector;
#include <string>
using std::string;
#ifndef LIST_INIT
#include <iterator>
using std::begin; using std::end;
#endif
#ifdef LIST_INIT
const vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
#else
// in this program just use an array instead of a vector,
// we only use scores to fetch values so don't need the flexibility of a vector
const string scores[] = {"F", "D", "C", "B", "A", "A++"};
#endif
vector<unsigned> grades;
// these functions demonstrate alternative ways to handle the if tests
// function that takes an unsigned value and a string
// and returns a string
string goodVers(string lettergrade, unsigned grade)
{
// add a plus for grades the end in 8 or 9 and a minus for those ending in 0, 1, or 2
if (grade % 10 > 7)
lettergrade += '+'; // grades ending in 8 or 9 get a '+'
else
if (grade % 10 < 3)
lettergrade += '-'; // those ending in 0, 1, or 2 get a '-'
return lettergrade;
}
// incorrect version of the function to add a plus or minus to a grade
string badVers(string lettergrade, unsigned grade)
{
// add a plus for grades the end in 8 or 9 and a minus for those ending in 0, 1, or 2
// WRONG: execution does NOT match indentation; the else goes with the inner if
if (grade % 10 >= 3)
if (grade % 10 > 7)
lettergrade += '+'; // grades ending in 8 or 9 get a +
else
lettergrade += '-'; // grades ending in 3, 4, 5, 6 will get a minus!
return lettergrade;
}
// corrected version using the same logic path as badVers
string rightVers(string lettergrade, unsigned grade)
{
// add a plus for grades that end in 8 or 9 and a minus for those ending in 0, 1, or 2
if (grade % 10 >= 3) {
if (grade % 10 > 7)
lettergrade += '+'; // grades ending in 8 or 9 get a +
} else // curlies force the else to go with the outer if
lettergrade += '-'; // grades ending in 0, 1, or 2 will get a minus
return lettergrade;
}
int main()
{
// read a set of scores from the input
unsigned grade;
while (cin >> grade)
grades.push_back(grade);
// now process those grades
for (auto it : grades) { // for each grade we read
cout << it << " " ; // print the grade
string lettergrade; // hold coresponding letter grade
// if failing grade, no need to check for a plus or minus
if (it < 60)
lettergrade = scores[0];
else {
lettergrade = scores[(it - 50)/10]; // fetch the letter grade
if (it != 100) // add plus or minus only if not already an A++
if (it % 10 > 7)
lettergrade += '+'; // grades ending in 8 or 9 get a +
else if (it % 10 < 3)
lettergrade += '-'; // grades ending in 0, 1, or 2 get a -
}
cout << lettergrade << endl;
if (it > 59 && it !=100) {
cout << "alternative versions: " << it << " ";
// start over with just the basic grade, no + or -
lettergrade = scores[(it - 50)/10];
cout << goodVers(lettergrade, it) << " ";
cout << badVers(lettergrade, it) << " ";
cout << rightVers(lettergrade, it) << " ";
cout << endl;
}
}
return 0;
}
#include <iostream>
using std::cin; using std::cout; using std::endl;
#include <string>
using std::string;
int main()
{
// repeatedly ask the user for a pair of numbers to sum
string rsp; // used in the condition; can't be defined inside the do
do {
cout << "please enter two values: ";
int val1 = 0, val2 = 0;
cin >> val1 >> val2;
cout << "The sum of " << val1 << " and " << val2
<< " = " << val1 + val2 << "\n\n"
<< "More? Enter yes or no: ";
cin >> rsp;
} while (!rsp.empty() && rsp[0] != 'n');
cout << endl;
return 0;
}
5.5、跳转语句
#include <iostream>
using std::cin; using std::cout; using std::endl;
int main()
{
do {
cout << "Guess a number between 0 and 9" << endl;
unsigned i, mynum = 7;
cin >> i;
if (i == mynum) {
cout << "Congrats you guessed right!" << endl;
break;
} else
if (i < mynum)
cout << "too low" << endl;
else
cout << "too high" << endl;
if (i != mynum)
if (i < mynum)
cout << "too low" << endl;
else
cout << "too high" << endl;
else {
cout << "Congrats you guessed right!" << endl;
break;
}
} while (true);
return 0;
}
5.6 Try语句块和异常处理
#include <stdexcept>
using std::runtime_error;
#include <iostream>
using std::cin; using std::cout; using std::endl;
#include "Sales_item.h"
int main()
{
Sales_item item1, item2;
while (cin >> item1 >> item2) {
try {
// execute code that will add the two Sales_items
// if the addition fails, the code throws a runtime_error exception
// first check that the data are for the same item
if (item1.isbn() != item2.isbn())
throw runtime_error("Data must refer to same ISBN");
// if we're still here, the ISBNs are the same
cout << item1 + item2 << endl;
} catch (runtime_error err) {
// remind the user that the ISBNs must match
// and prompt for another pair
cout << err.what()
<< "\nTry Again? Enter y or n" << endl;
char c;
cin >> c;
if (!cin || c == 'n')
break; // break out of the while loop
} // ends the catch clause
} // ends the while loop
return 0; // indicate success
}