程序流程结构
选择结构
/*单行if语句*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//选择结构 单行if语句
//用户输入分数,若分数大于600,视为考上一本大学
//注意事项 if条件语句后不要加分号
int score = 0;
cout << "请输入你的分数: " << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{
cout << "恭喜您考上了一本大学" << endl;
}
system("pause");
return 0;
}
/*多行if语句*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//选择结构 多行if语句
//用户输入分数,若分数大于600,视为考上一本大学
//若没考上一本大学,则打印未考上一本大学
//注意事项 if条件语句后不要加分号
int score = 0;
cout << "请输入你的分数: " << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{
cout << "恭喜您考上了一本大学" << endl;
}
else
cout << "很遗憾您未考上一本大学" << endl;
system("pause");
return 0;
}
/*练习题:三只小猪称体重*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//三只小猪ABC,请分别输入体重,并判断哪只小猪最重
int weight_A = 0;
cout << "请输入小猪A的体重:" << endl;
cin >> weight_A;
cout << "小猪A的体重为:" << weight_A << endl;
int weight_B = 0;
cout << "请输入小猪B的体重:" << endl;
cin >> weight_B;
cout << "小猪B的体重为:" << weight_B << endl;
int weight_C = 0;
cout << "请输入小猪C的体重:" << endl;
cin >> weight_C;
cout << "小猪C的体重为:" << weight_C << endl;
if (weight_A > weight_B)
{
if (weight_A > weight_C)
cout << "小猪A最重" << endl;
else
cout << "小猪C最重" << endl;
}
else
{
if (weight_B > weight_C)
cout << "小猪B最重" << endl;
else
cout << "小猪C最重" << endl;
}
system("pause");
return 0;
}
三目运算符
表达式1 ?表达式2:表达式3
如果1为真,则执行2,返回2的结果
如果1为假,则执行3,返回3的结果
/*三目运算符*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//创建三个变量a b c
//将a和b做比较,大的赋值给c
int a = 20;
int b = 10;
int c = 0;
c = (a > b ? a : b);
cout << "c=" << c << endl;
//三目运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << "a=" << a << endl;//a=100
cout << "b=" << b << endl;//b=10
system("pause");
return 0;
}
switch语句
/*switch语句*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//给电影打分
cout << "请给电影进行打分:" << endl;
int score = 0;
cin >> score;
cout << "您打的分数为:" << score << endl;
switch (score)
{
case 10:
cout << "您认为是经典电影" << endl;
break;//退出当前分支
case 9:
cout << "您认为是经典电影" << endl;
break;
case 8:
cout << "您认为是好电影" << endl;
break;
default:
cout << "您认为是烂片" << endl;
break;
}
//switch缺点 判断时候只能是整型或字符型
//switch优点 结构清晰,执行效率高
system("pause");
return 0;
}
循环结构
/*while循环*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//打印0~9
int num = 0;
//在循环中避免出现死循环
while (num < 10)
{
cout << num << " ";
num++;
}
system("pause");
return 0;
}
/*while循环练习案例:猜数字*/
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
//计算机生成1~100的数字,玩家猜,提示过大或过小,猜对恭喜玩家胜利并退出游戏
//添加随机数种子,防止生成伪随机数
srand((unsigned int)time(NULL));
int num = rand() % 100 + 1;//rand() % 100生成0~99的随机数
int val = 0;
while (1)
{
cin >> val;
if (val > num)
cout << "过大" << endl;
else if (val < num)
cout << "过小" << endl;
else
{
cout << "猜对了" << endl;
break;
}
}
system("pause");
return 0;
}
/*do while语句*/
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
//在屏幕中输出0~9的数字
int num = 0;
do
{
cout << num << " ";
num++;
} while (num < 10);
//do while 会先执行一次循环语句
system("pause");
return 0;
}
/*水仙花数*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
//三位数,每个位上的数字的三次幂的和为它本身
//用do...while语句求出所有三位数中的水仙花数
int num = 100;
do
{
int a = 0;
a = num / 100;
int b = 0;
b = (num / 10) % 10;
int c = 0;
c = num % 10;
int d = 0;
d = a * a*a + b * b*b + c * c*c;
if(d == num)
cout << num << " ";
num++;
} while (num < 1000);
//do while 会先执行一次循环语句
system("pause");
return 0;
}
/*for循环练习案例:敲桌子*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
//1~100,含7或是7的倍数,输出敲桌子,其余数字直接输出
for (int num = 1; num <= 100; num++)
{
int a = num % 7;//7的倍数
int b = num % 10;//个位有7
int c = num / 10;//十位有7
if (a == 0 || b == 7 || c == 7)
{
cout << "敲桌子" << " ";
}
else
cout << num << " ";
}
system("pause");
return 0;
}
/*嵌套循环*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
//打印10*10星图
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
/*嵌套循环案例:乘法口诀表*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
//打印10*10星图
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << i << "*" << j << "=" << i * j << " ";
}
cout << endl;
}
system("pause");
return 0;
}
跳转语句
break语句
在switch条件语句中,终止case并跳出switch
在循环语句中,跳出当前的循环语句
在嵌套循环中,跳出最近的内层循环
continue语句
在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环
goto语句
不推荐使用,影响代码结构