c++入门编程学习记录2

20.7.19,学习c++的第二天

学习内容

程序流程结构

选择结构 的 三目运算符、switch语句
循环结构:while、do…while、for、嵌套
跳转语句:break、continue、goto

数组

一维数组的定义
冒泡排序
二维数组的定义

练习

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 20;

    // 三目运算符
    cout << (a > b ? a : b) <<endl;
    (a > b ? a : b) = 100;//C++中三目运算符返回的是变量,可以继续赋值(b=100)

    //switch语句
    switch (a)
    {
    case 10:
        cout << 10 <<endl;
        break;
    case 20:
        cout << 20 <<endl;
        break;
    default:
        cout << "烂片" << endl;
        break;      
    }
    
    //while循环语句
    int c = 0;
    while (c < a)
    {
        cout << c <<endl;
        c++;
    }
    
    //do...while
    do
    {
        cout << c <<endl;
        c++;
    } while (c < a);
    
    //for语句
    for (int i = 0; i < a; i++)
    {
        cout << i <<endl;
    }
    
    //continue

    //goto

    //数组
    int  arr1[5];
    int  arr2[5] = {1, 2, 3, 4, 5};
    int  arr[] = {1, 2, 3, 4, 5};

    cout << "数组的元素个数为: " << sizeof(arr2) / sizeof(arr2[0]) << endl;
    cout << "数组中第一个元素地址为: " << (int)&arr[0] << endl;

    //二维数组
    

    system("pause");

    return 0;
}

冒泡

#include <iostream>
using namespace std;

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

    for (int i = 0; i < 9-1; i++)
    {
        for (int j = 0; j < 9-1-i; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                int tem = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = tem;
            }
            
        }
        
    }
    

    for (int i = 0; i < 9; i++)
	{
		cout << arr[i] << endl;
	}

    system("pause");

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值