C++分支和循环语句怎么使用

发布时间:2022-10-17 16:44:55 作者:iii
来源:亿速云 阅读:213

C++分支和循环语句怎么使用

目录

  1. 引言
  2. 分支语句
  3. 循环语句
  4. 控制语句
  5. 综合示例
  6. 总结

引言

在C++编程中,分支和循环语句是控制程序流程的基本工具。通过使用这些语句,程序可以根据不同的条件执行不同的代码块,或者重复执行某些代码块。本文将详细介绍C++中的分支和循环语句的使用方法,并通过示例代码帮助读者更好地理解和掌握这些概念。

分支语句

if语句

if语句是最基本的分支语句,用于根据条件决定是否执行某段代码。其基本语法如下:

if (condition) {
    // 当条件为真时执行的代码
}

示例:

#include <iostream>
using namespace std;

int main() {
    int num = 10;

    if (num > 0) {
        cout << "数字是正数" << endl;
    }

    return 0;
}

if-else语句

if-else语句在if语句的基础上增加了一个else分支,用于在条件为假时执行另一段代码。其基本语法如下:

if (condition) {
    // 当条件为真时执行的代码
} else {
    // 当条件为假时执行的代码
}

示例:

#include <iostream>
using namespace std;

int main() {
    int num = -5;

    if (num > 0) {
        cout << "数字是正数" << endl;
    } else {
        cout << "数字是非正数" << endl;
    }

    return 0;
}

嵌套if语句

if语句可以嵌套使用,即在ifelse分支中再使用if语句。其基本语法如下:

if (condition1) {
    // 当条件1为真时执行的代码
    if (condition2) {
        // 当条件2为真时执行的代码
    }
} else {
    // 当条件1为假时执行的代码
}

示例:

#include <iostream>
using namespace std;

int main() {
    int num = 15;

    if (num > 0) {
        cout << "数字是正数" << endl;
        if (num % 2 == 0) {
            cout << "数字是偶数" << endl;
        } else {
            cout << "数字是奇数" << endl;
        }
    } else {
        cout << "数字是非正数" << endl;
    }

    return 0;
}

switch语句

switch语句用于根据变量的值执行不同的代码块。其基本语法如下:

switch (expression) {
    case value1:
        // 当expression等于value1时执行的代码
        break;
    case value2:
        // 当expression等于value2时执行的代码
        break;
    // 可以有任意数量的case语句
    default:
        // 当expression不等于任何case值时执行的代码
}

示例:

#include <iostream>
using namespace std;

int main() {
    int day = 3;

    switch (day) {
        case 1:
            cout << "星期一" << endl;
            break;
        case 2:
            cout << "星期二" << endl;
            break;
        case 3:
            cout << "星期三" << endl;
            break;
        case 4:
            cout << "星期四" << endl;
            break;
        case 5:
            cout << "星期五" << endl;
            break;
        case 6:
            cout << "星期六" << endl;
            break;
        case 7:
            cout << "星期日" << endl;
            break;
        default:
            cout << "无效的日期" << endl;
    }

    return 0;
}

循环语句

for循环

for循环用于重复执行某段代码,通常用于已知循环次数的情况。其基本语法如下:

for (initialization; condition; increment) {
    // 循环体
}

示例:

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        cout << "i = " << i << endl;
    }

    return 0;
}

while循环

while循环用于在条件为真时重复执行某段代码。其基本语法如下:

while (condition) {
    // 循环体
}

示例:

#include <iostream>
using namespace std;

int main() {
    int i = 0;

    while (i < 5) {
        cout << "i = " << i << endl;
        i++;
    }

    return 0;
}

do-while循环

do-while循环与while循环类似,但do-while循环至少会执行一次循环体,然后再检查条件。其基本语法如下:

do {
    // 循环体
} while (condition);

示例:

#include <iostream>
using namespace std;

int main() {
    int i = 0;

    do {
        cout << "i = " << i << endl;
        i++;
    } while (i < 5);

    return 0;
}

嵌套循环

循环语句可以嵌套使用,即在循环体中再使用循环语句。其基本语法如下:

for (initialization1; condition1; increment1) {
    for (initialization2; condition2; increment2) {
        // 内层循环体
    }
    // 外层循环体
}

示例:

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "i = " << i << ", j = " << j << endl;
        }
    }

    return 0;
}

控制语句

break语句

break语句用于立即终止循环或switch语句的执行。其基本语法如下:

break;

示例:

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break;
        }
        cout << "i = " << i << endl;
    }

    return 0;
}

continue语句

continue语句用于跳过当前循环的剩余部分,直接进入下一次循环。其基本语法如下:

continue;

示例:

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            continue;
        }
        cout << "i = " << i << endl;
    }

    return 0;
}

goto语句

goto语句用于无条件跳转到程序中的某个标签处。其基本语法如下:

goto label;
// ...
label:
// 代码

示例:

#include <iostream>
using namespace std;

int main() {
    int i = 0;

    loop:
    cout << "i = " << i << endl;
    i++;
    if (i < 5) {
        goto loop;
    }

    return 0;
}

综合示例

以下是一个综合使用分支和循环语句的示例,该程序模拟了一个简单的猜数字游戏:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(0)); // 初始化随机数种子
    int secretNumber = rand() % 100 + 1; // 生成1到100之间的随机数
    int guess;
    int attempts = 0;

    cout << "欢迎来到猜数字游戏!" << endl;

    do {
        cout << "请输入你猜的数字(1-100):";
        cin >> guess;
        attempts++;

        if (guess < secretNumber) {
            cout << "太小了!再试一次。" << endl;
        } else if (guess > secretNumber) {
            cout << "太大了!再试一次。" << endl;
        } else {
            cout << "恭喜你,猜对了!你用了 " << attempts << " 次尝试。" << endl;
        }
    } while (guess != secretNumber);

    return 0;
}

总结

C++中的分支和循环语句是控制程序流程的重要工具。通过合理使用这些语句,可以实现复杂的逻辑控制和重复操作。本文详细介绍了ifif-elseswitchforwhiledo-while等语句的使用方法,并通过示例代码帮助读者理解和掌握这些概念。希望本文能对读者在C++编程中有所帮助。

推荐阅读:
  1. git 本地创建分支和远程分支关联
  2. Gitlab使用和分支管理(三)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:switch分支语句怎么使用

下一篇:怎么用imageIO图像流实现验证码效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》