c++基础知识02

1.前置与后置区别

#include
using namespace std;

int main()
 {
    //前置和后置区别
    //前置递增或递减 先让变量加减1 然后进行表达式运算
    int a1 = 10;
    int b1 = ++a1 * 10;
    cout << "a1=" << a1 << endl;
    cout << "b1=" << b1 << endl;
    //后置递增或递减 先进行表达式运算,后让变量加减1
    int a2 = 10;
    int b2 = a2++ * 10;
    cout << "a2=" << a2 << endl;
    cout << "b2=" << b2 << endl;

    system("pause");

    return 0;
}

2.if选择结构

#include
using namespace std;
int main() {
    //选择结构 
    int score = 0;
    cout << "请输入一个分数" << endl;
    cin >>score;
    if (score > 600) //if条件后不要加分号
    {
        cout << "恭喜您考上一本大学" << endl;
    }
    else
    {
        cout << "很遗憾您未能考上一本" << endl;
    };
    //多条件选择结构
    int grade = 0;
    cout << "请输入一个分数"  << endl;
    cin >> grade;
    cout << "您输入的分数为:" << grade << endl;

    if (grade > 600)
    {
        cout << "恭喜您考上一本大学" << endl;
        if (grade>700)
        {
            cout << "您能考入985" << endl;
        }
        else if (grade>650)
        {
            cout << "您能考入211" << endl;
        }
        else
        {
            cout << "您能考入普通一本" << endl;
        }
    }
    else if (grade > 500)
    {
        cout << "恭喜您考上二本大学" << endl;
    }
    else if (grade > 400)
    {
        cout << "恭喜您考上三本大学" << endl;
    }
    else
    {
        cout << "很遗憾您未能考上本科" << endl;
    }

    system("pause");

    return 0;
}

3.while与do..while循环结构

#include
using namespace std;

int main()                            
{
    //while循环语句
    //输出数字0~9
    int num = 0;
    
    while (num < 10) 
    {
        cout << num << endl;
        num++;
        
    }
    
    //do...while循环语句: do{循环语句}while(循环条件);
    // 与while的区别在于会先执行一次循环语句,再判断循环条件

    do
    {
        cout << num << endl;
        num++;
    } while (num < 10);


    system("pause");

    return 0;

 

你可能感兴趣的:(c++基础知识02)