c++ 跳转语句块

p170~p172:
跳转语句:
1、break:对while for switch有效!
2、continue:中断当前迭代,但是循环还要继续。因此while for有效,对switch无效!
3、goto:不要使用!

 

练习:

5.20

#include <iostream>
#include <string>
using namespace std;
int main()
{
    /*
            输入一组字符串,例如a b c d e e输出为e
        若输入为a b c d e f输出为“没有单词重复出现~”
     */
    string word, lastw;
    cin >> lastw; // 初始化
    bool has_sameword = false;
    while (cin >> word) {
        if (word == lastw) {
            cout << word << endl;
            has_sameword = true;
            break;
        } else {
       lastw = word;
     } }
if (!has_sameword) { cout << "没有单词重复出现~”" << endl; } return 0; }

 

5.21

#include <iostream>
#include <string>
using namespace std;
bool capital_begin(string word) {
    if (word[0] >= 'A' && word[0] <= 'Z') {
        return true;
    }
    return false;
}
int main()
{
    /*
            输入一组字符串,例如a b c d e e输出为“没有以大写字母开头的单词重复出现~”
        若输入为a b c d e F F j k则输出为F,这样的话增加一个条件就好了。
     */
    string word, lastw;
    cin >> lastw; // 初始化
    bool has_sameword = false;
    bool lastW = false;
    while (cin >> word) {
        if (word == lastw && capital_begin(word)) {
            cout << word << endl;
            has_sameword = true;
            break;
        } else {
            lastw = word;
        }
    }
    if (!has_sameword) {
        cout << "没有以大写字母开头的单词重复出现~" << endl;
    }
    return 0;
}

 

5.22

    int sz = -1;
    while (sz <= 0) {
        sz = get_size();
    }

 

posted @ 2017-03-15 16:15  xkfx  阅读(850)  评论(0编辑  收藏  举报