zoukankan      html  css  js  c++  java
  • (5)C++ 循环和判断

      循环

    一、for循环

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

        for (int i = 5; i; i--)
        {
            cout << "abc" << endl;
        }

    二、while循环

        int a = 0;
        while (a<10)
        {
            a++;
            cout << a << endl;
        }

    三、do while循环

        int a = 0;
        do
        {
            a++;
            cout << a << endl;
        } while (a<5);
        int n = 6;
        while (--n>0)
        {
            cout << n<<endl;
        } 

    四、基于范围 for循环

     对数组或容器类 vector array

        int a[] = { 3,6,8,7 };
        for (int i:a)
        {
            cout << i << endl;
        }

    能够修改数组内容

        int a[] = { 3,6,8,7 };
        for (int &i:a)
        {
            cout << i << endl;
        }

    循环内初始化

        for (int i: { 3,6,8,7 })
        {
            cout << i << endl;
        }

     多表达式

        for (int i = 6,j=5; i > 0; --i,--j)
        {
    
        }

    五、循环和输入

     1.使用原始cin输入

        char ch;
        int count = 0;
        cout << "输入字符,按#结束";
        cin >> ch;
        while (ch != '#') {
            cout << ch;
            ++count;
            cin >> ch;
        }
        cout << count << endl;

    忽略了空格等字符

    2.cin.get

        char ch;
        int count = 0;
        cout << "输入字符,按#结束";
        cin.get(ch);
        while (ch != '#') {
            cout << ch;
            ++count;
            cin.get(ch);
        }
        cout << count << endl;

    包含空格等

    3.EOF

    六、嵌套循环和二维数组

        int arr[3][3] = { {1,2,3},{4,5,6},{7,8,9} };
        int c = 0;
        for (int i=0; i<3;i++)
        {
            for (int j = 0; j < 3;j++) {
                c = arr[i][j];
                cout << c << endl;
            }
        }

     判断---------

    一、if

        if () {
            //
        }
        else if ()
        {
            //
        }
        else
        {
            //
        }

    二、逻辑表达式

    || &&

    if (a> 0 && a<5) {
        //
    }
    if (a< 0 || a>5) {
        //
    }

    三、cctype库

    #include<cctype>
    isalnum() //判断是否数字或字母
    isalpha() //是否字母
    iscntrl() //是否控制字符
    isdigit() //是否0-9
    isgraph() //是否除空格之外的打印字符
    islower() //是否小写字母
    isprint() //是否打印字符
    ispunct() //是否标点符号
    isspace() //是否标准空白字符(空格,进纸,换行,回车,制表)
    isupper() //是否大写字母
    isxdigit() //是否十六进制
    tolower() //如果是大写,返回小写
    toupper() //如果是小写,返回大写

    四、三目运算符

        char ch = 1 > 3 ? 'a' : 'b';
        cout << ch;

    五、switch 

        char ch = 'c';
        switch (ch)
        {
        case 'a':
        case 'A':
            cout << ch;
            break;
        default:
            cout << "off";
        }

    六、break continue

    break 跳出该循环

    continue 跳出本次循环

    七、读取数字循环

     

    八、简单文件输入输出

  • 相关阅读:
    EVIOCGNAME:Get Device Name
    Andriod Sensor HAL 分析
    通知内核你的设备 不支持llseek, 通过在你的 open 方法中调用nonseekable_open
    linux Gsensor驱动(bma250为例子)
    Linux输入子系统:输入设备编程指南 -- input-programming.txt
    Android UEventObserver 使用
    android switch模块
    linux里的驱动接口
    input subsystem 函数解释
    2.6 内核中的计时器 小结
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/11521087.html
Copyright © 2011-2022 走看看