1.1.1 单行if语句
#include
using namespace std;
int main()
{
//选择结构 单行if语句
//用户输入分数,大于600视为考上一本大学,在屏幕输出
//1.用户输入分数
int score=0;
cout << "请输入你的分数" << endl;
cin >> score;
//2.打印用户输入分数
cout << "你的分数为" << score << endl;
//3.判断是否大于600,大于输出
if (score > 600) //注意if后不能加分号;
{
cout << "恭喜考入一本大学" << endl;
}
system("pause");
return 0;
}
1.1.2 多行if语句
#include
using namespace std;
int main()
{
//选择结构 多行if语句
//用户输入分数,大于600视为考上一本大学,在屏幕输出,否则输出未考上
//1.用户输入分数
int score=0;
cout << "请输入你的分数" << endl;
cin >> score;
//2.打印用户输入分数
cout << "你的分数为" << score << endl;
//3.判断是否大于600,大于输出,否则输出未考上
if (score > 600) //注意if后不能加分号;
{
cout << "恭喜考入一本大学" << endl;
}
else
{
cout << "未考上一本" << endl;
}
system("pause");
return 0;
}
1.1.3 多条件if语句
#include
using namespace std;
int main()
{
//选择结构 多条件if语句
//用户输入分数,大于600视为考上一本大学,在屏幕输出;大于500,视为考上二本大学;大于400视为考上三本大学,否则输出未考上本科
//1.用户输入分数
int score=0;
cout << "请输入你的分数" << endl;
cin >> score;
//2.打印用户输入分数
cout << "你的分数为" << score << endl;
//3.判断是否大于600视为考上一本大学,在屏幕输出;大于500,视为考上二本大学;大于400视为考上三本大学,否则输出未考上本科
if (score > 600) //注意if后不能加分号;
{
cout << "恭喜考入一本大学" << endl;
}
else if(score>500)
{
cout << "恭喜考入二本大学" << endl;
}
else if (score > 400)
{
cout << "恭喜考上三本大学" << endl;
}
else
{
cout << "未考上本科" << endl;
}
system("pause");
return 0;
}
1.1.4 嵌入if语句
#include
using namespace std;
int main()
{
//选择结构 多条件if语句
//用户输入分数,大于600视为考上一本大学,在屏幕输出;大于500,视为考上二本大学;大于400视为考上三本大学,否则输出未考上本科
//1.用户输入分数
int score=0;
cout << "请输入你的分数" << endl;
cin >> score;
//2.打印用户输入分数
cout << "你的分数为" << score << endl;
//3.判断是否大于600视为考上一本大学,在屏幕输出;大于500,视为考上二本大学;大于400视为考上三本大学,否则输出未考上本科
if (score > 600) //注意if后不能加分号;
{
cout << "恭喜考入一本大学" << endl;
if (score > 700)
{
cout << "恭喜您考入北京大学" << endl;
}
else if(score>650)
{
cout << "恭喜考入清华大学" << endl;
}
else
{
cout << "恭喜您考入人大" << endl;
}
}
else if(score>500)
{
cout << "恭喜考入二本大学" << endl;
}
else if (score > 400)
{
cout << "恭喜考上三本大学" << endl;
}
else
{
cout << "未考上本科" << endl;
}
system("pause");
return 0;
}
1.1.4 练习案例
#include
using namespace std;
int main()
{
int weight1 = 0;
int weight2 = 0;
int weight3 = 0;
cout << "请输入一号小猪体重" << endl;
cin >> weight1;
cout << "请输入二号小猪体重" << endl;
cin >> weight2;
cout << "请输入三号小猪体重" << endl;
cin >> weight3;
if (weight1 > weight2)
{
if (weight1 > weight3)
{
cout << "一号最重" << endl;
}
}
else if (weight2 > weight3)
{
cout << "二号最重" << endl;
}
else
{
cout << "三号最重" << endl;
}
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//三目运算符
//创建三个变量abc
//将a与b作比较,将大值给c
int a = 10;
int b = 20;
int c = 0;
c = (a > b ? a : b);
cout << "C=" << c << endl;
//在c++中运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//switch语句
//给电影打分
//10~9 经典
//8~7 非常好
//6~5 一般
//5以下 烂片
int score = 0;
cout << "请输入你想打的分" << endl;
cin >> score;
cout << "您打的分数:" << score << endl;
switch (score)
{
case 10:
cout << "您认为是经典" << endl;
break;
case 9:
cout << "您认为是经典" << endl;
break;
case 8:
cout << "您认为是非常好" << endl;
break;
case 7:
cout << "您认为是非常好" << endl;
break;
case 6:
cout << "您认为是一般" << endl;
break;
case 5:
cout << "您认为是一般" << endl;
break;
default:
cout << "烂片" << endl;
break;
}
//if与switch区别
//switch 的缺点:判断的时候只能是整型或者是字符型,不可以是个区间
//switch 的优点:结构清晰,执行效率高
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//while 循环
//在屏幕中打印0~9这十个数字
int num = 0;
while (num < 10) //注意避免死循环
{
cout << num << endl;
num++;
}
system("pause");
return 0;
}
2.1.1 案例练习//随机数
#include
using namespace std;
//time系统时间头文件
#include
int main()
{
//添加随机数的种子,利用当前时间来生成随机数,防止每次都一样
srand((unsigned int)time(NULL));
int num1 = rand() % 100 + 1;//rand()%100表示随机生成0~99之间一个数 //伪随机数
int num2 = 0;
cout << "请输入您猜的数" << endl;
cin >> num2;
while (num1 != num2)
{
if (num2 > num1)
{
cout << "您猜的数比他大,请重新输入" << endl;
cin >> num2;
}
else
{
cout << "您猜的比他小,请重新输入" << endl;
cin >> num2;
}
}
cout << "恭喜您猜对了" << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//do while 语句
//在屏幕中输出0~9这十个数字
int num = 0;
do
{
cout << num << endl;
num++;
} while (num < 10);
//do...while 与while的区别
//do...while会先执行一次再判断 验证:把num<10改为num
system("pause");
return 0;
}
2.2.1 案例练习
#include
using namespace std;
int main()
{
int num1 = 100;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
do{
num3 = num1 / 100;
num4 = num1 % 10;
num5 = num1 /10 % 10;
num2 = num3 *num3*num3 + num4 *num4*num4 + num5 *num5*num5;
if (num1 == num2)
{
cout << num1 << endl;
}
num1++;
} while (num1 < 1000);
system ("pause");
return 0;
}
#include
using namespace std;
int main()
{
//for循环
//从数字1~9打印出来
int i;
for (i = 0; i < 10; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
2.3.1 案例练习
#include
using namespace std;
int main()
{
for (int num = 1; num <= 100; num++)
{
if(num %7==0||num%10==7||num/10==7)
{
cout << "敲桌子" << endl;
}
else
{
cout << num << endl;
}
}
system("pause");
return 0;
}
打印10*10的号
#include
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++)
{
for (int j=1; j <= 10; j++)
{
cout << "*";
}
cout << "\n";
}
system("pause");
return 0;
}
2.4.1 案例练习
#include
using namespace std;
int main()
{
for (int i = 1; i < 10; i++)
{
for (int j=1; j <= i; j++)
{
int sum = i * j;
cout << j << "*" << i << "=" << sum<<" ";
}
cout << "\n";
}
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//break 的使用时机
//出现在switch语句
cout << "请选择难度" << endl;
cout << "1.困难" << endl;
cout << "2.普通" << endl;
cout << "3.简单" << endl;
int select = 0;
cin >> select;
switch (select)
{
case 1:
cout << "你选择的是困难" << endl;
break;
case 2:
cout << "你选择的是普通难度" << endl;
break;
case 3:
cout << "你选择的是简单" << endl;
break;
}
//出现在循环中
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
cout << i << endl;
}
//出现在嵌套语句中
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
if (j == 5)
{
break;
}
cout << "* ";
}
cout << "\n";
}
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//continue 语句
for (int i = 0; i <= 100; i++)
{
//奇数输出,偶数不输出
if (i % 2 == 0)
{
continue;//可以筛选条件,跳过这次循环剩余代码,进行下次循环
}
cout << i << endl;
}
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//goto语句
cout << "1...." << endl;
cout << "2...." << endl;
goto Flag;
cout << "3...." << endl;
cout << "4...." << endl;
Flag:
cout << "5...." << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//数组 三种类型
//1.数据类型 数组名[数据长度];
int arr[5];
//数组名是从0开始的
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
cout << arr[0] << endl;
cout << arr[1] << endl;
cout << arr[2] << endl;
cout << arr[3] << endl;
cout << arr[4] << endl;
//2.数据类型 数组名[数据长度]={值1,值2,。。。};
int arr2[5] = { 1,2,3,4,5 };//如果初始化没有全部填补数据,系统会自动用0来填补
/*
cout << arr2[0] << endl;
cout << arr2[1] << endl;
cout << arr2[2] << endl;
cout << arr2[3] << endl;
cout << arr2[4] << endl;
*/
for (int i = 0; i < 5; i++)//用循环输出
{
cout << arr2[i] << endl;
}
//3.数据类型 数组名[]={值1,值2,。。。};
int arr3[] = { 11,12,13,14,15 };//定义数组时候,必须要有初始长度
for (int i = 0; i < 5; i++)//用循环输出
{
cout << arr3[i] << endl;
}
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//一维数组名称的用途
//1.可以统计整个数组在内存中的长度
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
cout << "整个数组所占空间" << sizeof(arr) << endl;
cout << "每个元素所占空间" << sizeof(arr[0]) << endl;
cout << "有几个数" << sizeof(arr) / sizeof(arr[0]) << endl;
// 2.可以获取数组在内存中的首地址
cout <<"数组首地址:"<< arr << endl;//可以在arr前加(int),可以把十六进制转换为十进制
cout << "第一个元素的地址" << &arr[0] << endl;//看元素地址要加&
cout << "第二个元素的地址" << &arr[1] << endl;
//数组名是个常量,不能进行赋值操作
//arr=100;错误,已经是常量,不能赋值
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
int arr[5] = { 300,350,200,400,250 };
int max = arr[0];
int k = 0;
for (int i = 1; i < 5; i++)
{
if (arr[i] > arr[i - 1])
{
max = arr[i];
k = i;
}
}
cout << "第" << k << "只小猪最重," << "体重为" << max << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
int L = sizeof(arr) / sizeof(arr[0]);
int temp = 0;
for (int i = 0; i < L / 2; i++)
{
temp = arr[i];
arr[i] = arr[L - 1 - i];
arr[L - 1 - i] = temp;
}
for (int j = 0; j < L; j++)
{
cout << arr[j] << endl;
}
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//冒泡排序
int arr[9] = { 4,2,8,0,5,7,1,3,9 };
int temp = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 1; j < 9 - i; j++)
{
if (arr[j] < arr[j - 1])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
cout << "排序后 : " << endl;
for (int k = 0; k < 9; k++)
{
cout << arr[k] ;
}
cout << endl;
system ("pause");
return 0;
}
多加了一个维度
#include
using namespace std;
int main()
{
//二维数组定义
//1.数据类型 数组名[行数][列数];
int arr[2][3];
arr[0][0] = 0;
arr[0][1] = 1;
arr[0][2] = 2;
arr[1][0] = 0;
arr[1][1] = 1;
arr[1][2] = 2;
cout << arr[0][0] << endl;
cout << arr[0][1] << endl;
cout << arr[0][2] << endl;
cout << arr[1][0] << endl;
cout << arr[1][1] << endl;
cout << arr[1][2] << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << endl;
}
}
//2.数据类型 数组名[行数][列数]={ {数据1,数据2},{数据3,数据4}};
int arr2[2][3] =
{
{1,2,3},
{4,5,6}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr2[i][j] ;
}
cout << endl;
}
//3.数据类型 数组名[行数][列数]={ 数据1,数据2,数据3,数据4};
int arr3[2][3] = { 1,2,3,4,5,6 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr2[i][j];
}
cout << endl;
}
//4.数据类型 数组名[][列数]={ 数据1,数据2,数据3,数据4};
int arr4[][3] = { 1,2,3,4,5,6 };//可以省去行数,列不能省
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr2[i][j];
}
cout << endl;
}
system ("pause");
return 0;
}
#include
using namespace std;
int main()
{
//二维数组组名
//查看二维数组所占空间
int arr[2][3] =
{
{1,2,3},
{4,5,6}
};
cout << "二维数组所占空间" << sizeof(arr) << endl;
cout << "二维数组一行所占空间" << sizeof(arr[0]) << endl;
cout << "二维数组一个元素所占空间" << sizeof(arr[0][0]) << endl;
//获取二维数组的首地址
cout << "二维数组的首地址" << arr << endl;
system ("pause");
return 0;
}
#include
using namespace std;
int main()
{
int score[3][3] =
{
{100,100,100},
{90,50,100},
{60,70,80}
};
int sum[3] = { 0,0,0 };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
sum[i] = score[i][j] + sum[i];
}
}
cout << "张三成绩为" << sum[0] << endl;
cout << "李四成绩为" << sum[1] << endl;
cout << "王五成绩为" << sum[2] << endl;
system ("pause");
return 0;
}