C++入门基础06:简单语句与顺序结构、选择结构if与switch、循环语句、跳转语句、异常处理

C++入门基础06:简单语句与顺序结构、选择结构if与switch、循环语句、跳转语句、异常处理

1、简单语句与顺序结构:

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

int main()
{
    
    
    //1、表达式加上分号就是语句
    //a + 5 
    int a = 10;
    a = a + 5;

    //2、空语句
    //while (a > 5); a--; //空语句多余的也不一定是没有害处的
    
    //3、复合语句——语句块
    //{ }
    {
    
    
        int b = 6;
        b = b + 1;
    }

    //4、顺序语句
    a = a + 5;
    a = a * 5;
    //....顺序执行

    //5、语句作用域
    //b是定义在语句块中的,作用域就是该语句块内,外部是没有定义的。
    //因此下面的b输出会报错,显示为未定义标识符"b"
    //cout << b << endl;
    for (int i = 0; i < 10; i++) {
    
    
        cout << "i:" << i << endl;
    }
    //cout << i << endl; //这里i的作用域仅限于for循环。


    return 0;
}

在这里插入图片描述

2、选择结构if与switch

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

int main()
{
    
    
    //程序流程结构
    //顺序结构、条件结构(选择结果)、循环结构

    //选择结构:if——else switch——case
    //一、 if——else
    //if语句
    int a = 1;
    if (a>6) //bool:true false
    {
    
    
        //不执行
        cout << "a 大于 6 !" << endl;
        a = a + 5;
    }

    //if..else
    if (a > 6) //bool:true false
    {
    
    
        //不执行
        cout << "a的值大于6!" << endl;
    }
    else {
    
    
        cout << "a的值小于6:" << endl;
    }

    //嵌套if语句
    if (a > 6) {
    
    
        cout << "a 的值大于6" << endl;
    }
    else if (a > 0) {
    
    
        cout << "a 的值大于0小于6" << endl;
    }
    else {
    
    
        cout << "a 的值小于0" << endl;
    }

    //嵌套的if判断
    if (a > 0) 
    {
    
    
        if (a > 6)
        {
    
    
            cout << "a大于6" << endl;
        }
        else
        {
    
    
            cout << "a小于6" << endl;
        }
    }

    //二、switch——case
    /*
    switch(表达式) //表达式内容只能是字符型或者整型
    {
        case结果1: 执行语句1; break;
        case结果2: 执行语句2; break;
        ...
        case结果n: 执行语句n; break;
        default: 执行语句: break;
    }
    */
    cout << "\n" << endl;
    string str = "i like study cplusplus!";
    int num = 0;
    for (char c : str)
    {
    
    
        switch (c) //表达式内容只能是字符或则整型
        {
    
    
            case 'a': num++; break; //break关键字很重要,不可以遗漏
            case 'e': num++; break;
            case 'i' : num++; break;
            case 'o': num++; break;
            case 'u': num++; break;
            default: cout << "不是元音字母:" <<c<< endl; break;
        }
    }
    cout << "元音字母个数为:" << num << endl;

    return 0;
}

在这里插入图片描述

3、循环语句

循环的实现可以使用 for、while 和 do…while 三种形式:

for(初始值;循环结束条件;循环量变更)
{
 //代码块
}

范围 for 循环:

for(元素:集合)
{
 //代码块
}

范围 for 循环是从集合中取其中的元素,进行循环操作。

while(循环结束条件)
{
 // 语句块
}
do
{
 // 语句块
}
while(循环结束条件)

注意:do…while 会先执行一次语句块的功能,然后再判断循环条件是否满足。
循环结构要非常注意循环变更是否能满足循环结束条件,不满足则出现死循环。

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

int main()
{
    
    
    //1、for 循环
    for (int i = 0; i < 10; i++) {
    
    
        cout << "i:" << i << endl;
    }

    //2、范围for循环(C++11)
    cout << '\n' << endl;
    string str = "cplusplus";
    for (char c : str)  //从str中一个个取字符
    {
    
    
        cout << c << " " ;
    }
    cout << endl;

    //3、while循环
    cout << '\n' << endl;
    int i = 10;
    while (i > 0)
    {
    
    
        cout<<"i的值:"<< i << endl;
        i--; //循环变更方法,去掉了就成了死循环。
    }

    //4、do...while
    cout << '\n' << endl;
    //先执行一次do,后到while判断
    int j = 10;
    do {
    
    
        j--;
        cout << "j的值:" << j << endl;
    } while (j > 0);

    return 0;
}

在这里插入图片描述

4、跳转语句

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

int main()
{
    
    
    //1、break
    // 乘法表
    // 终止最近的循环,彻底的终止。
    //用于switch 和 循环 ( for while do..while)
    for (int i = 1; i < 10; i++) 
    {
    
    
        for (int j = 1; j <10; j++)
        {
    
    
            if(j>i)
                {
    
    
                    cout << endl;
                    break;
                }
            cout << i << "*" << j << " = " << i * j << " ";
        }
    }


    //2、continue
    // 去除元音字母
    //终止当前迭代的最近循环
    cout<<'\n' << endl;
    string str = "i love study cplusplus very much!";
    for (char c : str)
    {
    
    
        if (c == 'a' || c =='e' || c =='i' || c == 'o' || c =='u')
        {
    
    
            continue;
            //本次循环结束,不在往下进行了,将
        }
        cout << c;

    }
    cout << endl;

    //3、return
    //返回值
    //函数里面使用,在函数章节会做讲解

    //4、goto
    //无条件跳转,但是不建议大家使用
    int a = 0;
label:
    int c = 9;
    a = a + 1;
    goto label; //没有任何条件与意义的进行跳转,不建议使用
    int b = 9;
    b = b + 1;

    return 0;
}

在这里插入图片描述

5、异常处理

异常是程序在执行期间产生的问题或者特殊错误,如除以零的操作。

C++提供了异常处理机制,异常处理涉及到三个关键字:try、catch、throw。
try: try 块中是尝试执行的代码,该代码有可能有异常抛出,它后面通常跟着一个或多个 catch 块。
catch: 用于处理程序捕获异常。
throw: 当代码执行出现问题时,可以使用 throw 关键字抛出一个异常。

try
{
 // 可能有异常抛出的代码
}catch( ExceptionName e1 )
{
 // catch 块
}catch( ExceptionName e2 )
{
 // catch 块
}

C++ 提供了一系列标准的异常,定义在 中,我们可以在程序中使用这些标准的异常。
也可以通过继承和重载 exception 类来自己定义异常。
在这里插入图片描述
尽量使用标准库的异常

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
#include<exception> //标准库的头文件,标准库的异常
using namespace std;

//异常处理

//自定义了一个MyException异常
struct MyException : public exception //继承exception这个类,也就是标准库的异常。
{
    
       
    //重写了一下what()函数,在exception中有定义了一个what()函数,重写了一下what()函数的返回信息:
    const char* what() const throw()
    {
    
    
        return "C++ Exception";
    }
};

int main()
{
    
    
    int price;
    try
    {
    
    
        cout << "请输入价格:" << endl;
        cin >> price;
        if (price <= 0) 
        {
    
    
            //抛出异常
            throw runtime_error("price cannot be negative!");
        }
    }
    //catch只捕获运行时异常,运行时异常变量名名称叫e
    catch (runtime_error e) 
    {
    
    
        //输出一下运行时错误的信息。
        cout<<"运行时错误的信息:"<< endl;
        cout << e.what() << endl;
    }


    cout << "\n" << endl;
    try
    {
    
    
        throw MyException();
    }
    catch (MyException e) 
    {
    
    
        cout << "catch some exception! " << endl;
        cout << e.what() << endl;
    }


    cout << "\n" << endl;
    try
    {
    
    
        throw MyException();
    }
    //括号中是三个点表示捕获所有异常。
    //如果不适用...,那么有可能异常没有捕获到,程序就有可能崩溃,所以为了能够让程序正常运行,使用...去捕获所有异常。
    catch (...)
    {
    
    
        cout << "catch all exception! " << endl;
    }


    return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_37755995/article/details/128064496