程序流程结构
顺序结构
选择结构
循环结构
1选择结构
1.1 单行if语句
#include<iostream>
using namespace std;
//比较运算符
int main() {
//单行if语句
//输入分数 分数大于600,考上一本,恭喜
int score = 0;
cout << "请输入一个分数" << endl;
cin >> score;
//打印您的分数
cout << "您的分数为:" << score << endl;
//判断您的分数>600
if (score > 600) //这条语句后面不能加;
{
cout << "恭喜您考上了一本大学" << endl;
}
//
if (score < 200)
{
cout << "请再来一年" << endl;
}
system("pause");
return 0;
}
1.2多行if语句
if(条件){条件满足执行的语句}else{条件不满足执行的语句}
#include<iostream>
using namespace std;
int main()
{
//单行if语句
//输入分数 分数大于600,考上一本,恭喜
int score = 0;//默认i情况下0分
cout << "请输入一个分数" << endl;
cin >> score;
//打印您的分数
cout << "您的分数为:" << score << endl;
//判断您的分数>600
if (score > 600) //这条语句后面不能加;
{
cout << "恭喜您考上了一本大学" << endl;
}
else
{
cout << "再来一年" << endl;
}
system("pause");
return 0;
}
1.2多条件if语句
#include<iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入一个分数" << endl;
cin >> score;
//打印您的分数
cout << "您的分数为:" << score << endl;
//判断您的分数>600
//大于500,二本
//大于400,三本
//否则,未考上
if (score > 600)
{
cout << "恭喜您考上了一本大学" << endl;
}
else if(score > 500 )
{
cout << "考上二本大学" << endl;
}
else if (score > 400 )//else if 理论上可以写无数个,看自己的需求
{
cout << "未考上大学" << endl;
}
else
{
cout << "未考上大学,再来一次" << endl;
}
system("pause");
return 0;
}
1.3嵌套if语句
#include<iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入一个分数" << endl;
cin >> score;
//打印您的分数
cout << "您的分数为:" << score << endl;
//判断您的分数>600
//大于500,二本
//大于400,三本
//否则,未考上
if (score > 600) //嵌套语句,在大于680里可以上985大学否则211大学
{
if (score > 680)
{
cout << "恭喜您考上了985大学" << endl;
}
else if(score>610)
{
cout <<"恭喜你考上了211大学 "<<endl;
}
else
{
cout << "恭喜您考上了一本大学" << endl;
}
}
else if(score > 500 )
{
cout << "考上二本大学" << endl;
}
else if (score > 400 )//else if 理论上可以写无数个,看自己的需求
{
cout << "未考上大学" << endl;
}
else
{
cout << "未考上大学,再来一次" << endl;
}
system("pause");
return 0;
}
1.4案例三只小猪称体重
#include<iostream>
using namespace std;
int main()
{
int weight1 = 0;
int weight2 = 0;
int weight3 = 0;
cout << "请输入三只小猪的体重" << endl;
cin >> weight1;
cin >> weight2;
cin >> weight3;
cout<< "第一只小猪的体重为:" << weight1 << endl;
cout << "第二只小猪的体重为:" << weight2 << endl;
cout << "第三只小猪的体重为:" << weight3 << endl;
if (weight1 > weight2) //1比2重
{
if (weight1 > weight3)
{
cout << "第一只小猪最重" << endl;
}
else//3>1
{
cout << "第三只小猪最重" << endl;
}
}
else //2 比1重
{
if (weight2 > weight3)
{
cout << "第二只小猪最重" << endl;
}
else
{
cout << "第三只小猪最重" << endl;
}
}
system("pause");
return 0;
}
1.5三目运算符
#include<iostream>
using namespace std;
int main()
{
//三目运算符
//A B C
//AB相比较,将大的赋值给 C a > b ? a : b
int a = 10;
int b = 21;
int c = 43;
/*c=(a > b ? a : b);
cout << "c=" << c << endl;*/
(a < b ? a : b )= 100;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
return 0;
}
1.6 switch语句
#include<iostream>
using namespace std;
//switch语句执行多条件分支语句
int main()
{
//switch语句
//给电影进行打分
cout << "输入一个分数" << endl;
int score = 0;
cin >> score;
cout << "你打的分数为:" << score << endl;
switch(score)//switch() 只能放一个 整数或 字符型,不可以是个区间(缺点)
//结构很清晰,执行效率高 比if语句效率要高。
{
case 10:
cout << "您认为这是个经典电影" << endl; break;//break 中止不再往下进行
case 9:
cout << "您认为这是个经典电影" << endl; break;
case 8:
cout << "您认为好" << endl; break;
case 7:
cout << "您认为这是个经典电影" << endl; break;
case 5:
cout << "您认为这是个yiban电影" << endl; break;
default:
cout << "您认为这是个lan电影" << endl; break;
}
system("pause");
return 0;
}
2循环结构
2.1 while循环语句
#include<iostream>
using namespace std;
//switch语句执行多条件分支语句
int main()
{
//while()填入 循环条件
//写循环的时候,避免死循环
int i = 0;
while(i<1000)
{
cout << i << endl;
i++;
}
system("pause");
return 0;
}
2.2猜数字游戏 (随机数种子)
#include<iostream>
using namespace std;
#include<ctime> //加随机数种子的时候,必须含有这个ctime头文件
//switch语句执行多条件分支语句
int main()
{
//添加随机数种子,作用利用当前时间生成随机数,防止每次随机数都一样
srand((unsigned int)time(NULL));
//系统随机生成随机数
int sjs=rand() % 100 + 1;//(0-100)
//2.玩家进行猜测
int val = 0;
while (1)
{
cin >> val;//在循环中可以无限输入
//猜对,赢
if (val > sjs)
{
cout << "猜测大了" << endl;
}
else if (val < sjs)
{
cout << "猜测小了" << endl;
}
else
{
cout << "猜对了,恭喜!!!!" << endl;
break;
}
}
system("pause");
return 0;
}
2.3 do while循环语句
#include<iostream>
using namespace std;
//#include<ctime> //加随机数种子的时候,必须含有这个ctime头文件
//switch语句执行多条件分支语句
int main()
{//do while 先执行循环语句 do{}while()
int num = 0;
//do {
// cout << num << endl;
// num++;
//}
//while (num<10 );//循环条件
do while 先执行一次循环语句, while 是先判断语句再执行语句
while (num<10)
{
cout << num << endl;
num++;
}
system("pause");
return 0;
}
2.4 案例——水仙花数
3位数,每个位上的数字3次幂之和等于它本身
比如 153 =1^3+ 5^3+ 3^3=153
个位153%10=3
十位 153/10=15.3 15.3%10=5
百位153/100=1
#include<iostream>
using namespace std;
//#include<ctime> //加随机数种子的时候,必须含有这个ctime头文件
//switch语句执行多条件分支语句
int main()
{
int num = 100;
do {
int a;//个位数
int b;//十位数
int c;//百位数
a = num % 10;
b = num / 10 % 10;
c = num / 100;
if ( a*a*a + b*b*b + c*c*c==num )//=只是赋值,==等于
{
cout << num << endl;//如果是水仙花才打印
}
num++;
}
while (num < 1000);
system("pause");
return 0;
}
for循环结构
2.5 案例——敲桌子
#include<iostream>
using namespace std;
//#include<ctime> //加随机数种子的时候,必须含有这个ctime头文件
int main()
{
//敲桌子1.输出为1~100
//个位为7 ,i%10=7
//7的倍数 i%7=0
//十位为7 i/10=7
for (int i = 1; i <= 100; i++)
{
//100里面找到特殊的数字 打印敲桌子
if(i%10==7||i%7==0||i/10==7)//或||的关系,表达式必须为可修改的左值
{
cout << "今天敲了"<<i<<"桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
星图
#include<iostream>
using namespace std;
//#include<ctime> //加随机数种子的时候,必须含有这个ctime头文件
int main()
{
//利用嵌套循环
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
2.6 乘法口诀表
#include<iostream>
using namespace std;
//乘法口诀表
int main()
{
//利用嵌套循环
for (int i = 1; i < 9; i++)
{
for (int j = 1; j <=i; j++)//列数小于行数
{
//for (i < j)
cout <<j<<"*"<<i<<"="<<i*j<<" ";
}
cout << endl;
}
system("pause");
return 0;
}
#include<iostream>
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;//break 中止条件
default:
break;
}
//出现在循环语句中
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
cout << i << endl;
}
//出现在嵌套语句
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
2.7 案例 0-100只输出偶数,不输出奇数
#include<iostream>
using namespace std;
//乘法口诀表
//continue 语句 在循环语句中, 执行到本行就不再执行后面的代码
int main()
{
for (int i = 0; i < 100; i++)
{
if (i % 2 == 1)//奇数输出
{
continue;//执行到这句,就不会执行后面的代码;
}//
cout << i << endl;
}
//
system("pause");
return 0;
}
goto语句不太推荐使用
#include<iostream>
using namespace std;
//乘法口诀表
//continue 语句 在循环语句中, 执行到本行就不再执行后面的代码
int main()
{
//goto语句可以无条件跳转语句
cout << "1.uijianshiy1 " << endl;
cout << "2,tuijianshiy1 " << endl;
goto FLAG;
cout << "3,tuijianshiy1 " << endl;
cout << "4,tuijianshiy1 " << endl;
FLAG:
cout << "tuijianshiy1 " << endl;
system("pause");
return 0;
}