加减乘除
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
肥肠容易忘记。。。
A:B ?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");
}