+一个完成某种特定任务的过程可分解成一组操作步骤,这组操作步骤即构成一个算法。
+算法结构
顺序结构
选择结构
循环结构
+条件
布尔类型及相关运算符
+布尔类型bool
true false 1个字节
#include <iostream>
using namespace std;
int main()
{
bool x=true;
cout<<x<<endl;//true被显示为1
}
int y;
y=x;//将bool型变量x赋值给int型变量y,c++将自动转换类型,true被转换成1
cout<<y<<endl;
x=5;//将int型常量5赋值给bool型变量x,5被转换为true,即非0值转为true
// 此时编译系统会提示warning
cout<<x<<endl;//显示变量x的值,true被显示为1
return 0;
+关系运算符
比较结果为bool类型
+逻辑运算符
一、选择语句
if (表达式)
{语句}
else
{语句}
switch - case
//求倒数
#include <iostream>
using namespace std;
int main()
{
double x;
cin>>x;
if(x!=0)
{
double y;
y=1/x;//求x的倒数,结果赋值给y
cout<<y;//显示y的值,即x的倒数
}
else
cout<<"0的倒数没有意义";
return 0;
}
//判断闰年
#include <iostream>
using namespace std;
int main()
{
int year;
cin>>year;
if((year%4==0&&year%100!=0)||year%400==0)//闰年判断条件
cout<<year<<"是闰年"<<endl;
else
cout<<year<<"不是闰年"<<endl;
return 0;
}