C++入门笔记之(最基本的操作2)(黑马)

本文介绍了C++中的基本编程概念,包括算术运算符、逻辑运算、条件语句(if-else,elseif)、三目运算符、switch-case语句以及循环(while)。实例演示了如何在程序中使用这些元素进行逻辑判断和数值计算。
摘要由CSDN通过智能技术生成

加减乘除

int main()
{
    int a = 10;
    cout<< a<< endl;
    cout<< a/2<< endl;
    cout<< a%2<< endl;
    cout<< a++<< endl;
    cout<< ++a<< endl;
    cout<< --a<< endl;
    cout<< a++<< endl;
    system("pause");
}

比较运算符

进行逻辑计算的方式,真为1,假为0;

逻辑运算符

C++中除了0都为真。


#include <iostream>
#include <string.h>
using namespace std;


int main()
{
    int a=1;
    int b=1;
    cout<<!a<<endl;
    a=0;
    b=1;
    cout<<( b||a )<<endl;
    a=0;
    b=0;
    cout<<( a&&b )<<endl;
    system("pause");
}

if条件语句使用

#include <iostream>
#include <string.h>
using namespace std;


int main()
{
    int score=0;
    cout<<"分数:"<<endl;
    cin>>score;
    cout<<"分数:"<<score<<endl;
    if(score>600)
    {
        cout<<"恭喜"<<endl;
    }
    else if(score>550)
{
    cout<<"550"<<endl;
}
    else
    {
        cout<<"可惜"<<endl;
    }
    system("pause");
}

多行if   用elseif

else if即可。

三者比较谁最大代码

#include <iostream>
#include <string.h>
using namespace std;


int main()
{
    int a,b,c;
    cout<<"请输入a的大小";
    cin>>a;
    cout<<"请输入b的大小";
    cin>>b;
    cout<<"请输入c的大小";
    cin>>c;
    if(a>b)
    {
        if(a>c) cout<<"a最大"<<endl;
        else cout<<"c最大"<<endl;
    }
    else
    {
        if(b>c) cout<<"b最大"<<endl;
        else cout<<"c最大"<<endl;
    }
    system("pause");
}

三目运算符 A:B?C

肥肠容易忘记。。。

 AB C

A是判断表达式,B是A为true时的结果,C是A为false的结果

#include <iostream>
#include <string.h>
using namespace std;


int main()
{
    int a,b;
    cout<<"请输入a的大小";
    cin>>a;
    cout<<"请输入b的大小";
    cin>>b;
    cout<<(a>b?a:b)<<endl;
    system("pause");
}

switch case跳转语句

#include <iostream>
#include <string.h>
using namespace std;


int main()
{
    int score=0;
    cout<<"请输入"<<endl;
    cin>>score;

    switch (score)
    {
    case 1/* constant-expression */:
        /* code */
        cout<<"1"<<endl;
        break;
    
    default:
        cout<<"0"<<endl;
        break;
    }
    
    system("pause");
}

循环语句


#include <iostream>
#include <string.h>
using namespace std;


int main()
{
    int n=0,sum=0;
    //从0到9的加法
    while(n<10)
    {
        sum+=n;
        n++;
    }
    cout<<sum<<endl;
    system("pause");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值