C++,顺序结构,选择结构,循环结构,随机数

目录

1.选择结构

1.1if语句

1.1.1单行if语句

1.1.2多行格式的if语句

1.1.3多条件if语句

1.1.4嵌套if语句

1.2三目运算符

1.3switch语句

 2.循环结构

2.1while循环

 2.2do..while语句

2.3for循环语句

 2.4嵌套循环

3.跳转语句

3.1.1break

3.1.2continue

 3.1.3goto语句


1.选择结构

1.1if语句

1.1.1单行if语句

#include<iostream>
using namespace std;

int main()
{
	//选择结构 单行if语句

	//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出

	int score;
	cout << "请输入一个分数" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	if (score > 600)
	{
		cout << "恭喜您考上了一本大学" << endl;
	}
	system("pause");

	return 0;

}

1.1.2多行格式的if语句

#include<iostream>
using namespace std;

int main()
{
	//选择结构 多行if语句

	//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
	//否则打印未考上一本大学
	int score;
	cout << "请输入一个分数" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	if (score > 600)
	{
		cout << "恭喜您考上了一本大学" << endl;
	}
	else
	{
		cout << "未考上一本大学" << endl;
	}
	system("pause");

	return 0;

}

1.1.3多条件if语句

#include<iostream>
using namespace std;

int main()
{
	//选择结构 多条件if语句

	//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
	//大于500分,视为考上二本大学
	//大于400分,视为考上三本大学
	//小于等于400分,视为未考上本科
	int score;
	cout << "请输入一个分数" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	if (score > 600)
	{
		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<iostream>
using namespace std;

int main()
{
	//选择结构 多条件if语句

	//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
	//大于500分,视为考上二本大学
	//大于400分,视为考上三本大学
	//小于等于400分,视为未考上本科
	int score;
	cout << "请输入一个分数" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	if (score > 600)
	{
		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;

}

练习,比较三个数的大小

#include<iostream>
using namespace std;

int main()
{
	int a, b, c;
	cout << "输入三只小猪的体重" << endl;
	cin >> a >> b >> c;
	int ans;
	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");

	return 0;

}

1.2三目运算符

#include<iostream>
using namespace std;

int main()
{
	//三目运算符
	//将a和b作比较,将变量大的值赋值给变量c
	int a = 10;
	int b = 20;
	int c;
	c = (a > b ? a : b);
	cout << c << endl;

	//在C++中三目运算符返回的是变量,可以继续赋值
	(a > b ? a : b) = 100;
	cout << "a=" << a << "b=" <<b<< endl;//a=10b=100

	system("pause");

	return 0;

}

1.3switch语句

#include<iostream>
using namespace std;

int main()
{
	//switch语句

	//给电影打分
	//10~9 经典
	//8~7 非常好
	//6~5 一般
	//5以下 烂片
	cout << "请给电影打分" << endl;
	int score;
	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;

}


 2.循环结构

2.1while循环,随机数,rand,srand

#include<iostream>
using namespace std;

int main()
{
	//while循环
	//在屏幕中打印0~9这10个数字
	int a = 0;
	while (a < 10)
	{
		cout << a << endl;
		a++;
	}

	system("pause");

	return 0;

}

猜数字

#include<iostream>
using namespace std;

int main()
{
	//while循环
	
	//添加随机数种子 作用利用当前系统时间生成随机数,防止每次随机数都一样
	srand((unsigned int)time(NULL));
	//rand() % 100;//生成0-99的随机数
	int num=rand() % 100 + 1;//生成1-100之间的随机数
	int val;
	while (1)
	{
		cin >> val;
		if (val > num)
		{
			cout << "猜大了" << endl;
		}
		else if(val < num)
		{
			cout << "猜小了" << endl;
		}
		else
		{
			cout << "猜对了" << endl;
			break;//利用break,跳出当前的循环
		}
	}
	system("pause");

	return 0;

}

 2.2do..while语句

#include<iostream>
using namespace std;

int main()
{
	//do...while语句
	//在屏幕中输出0-9
	int a = 0;
	do
	{
		cout << a << endl;
		a++;
	} while (a < 10);
	system("pause");

	return 0;

}

#include<iostream>
using namespace std;

int main()
{
	//do...while语句
	//在屏幕中输出0-9
	int num = 100;
	int ge, shi, bai,tmp;
	do
	{
		tmp = num;
		ge = tmp % 10;
		tmp = tmp / 10;
		shi = tmp % 10;
		bai = tmp / 10;
		if (num == ge * ge * ge + shi * shi * shi + bai * bai * bai)
		{
			cout << num << endl;
		}
		num++;
	} while (num < 1000);
	system("pause");

	return 0;

}

2.3for循环语句

 2.4嵌套循环

#include<iostream>
using namespace std;

int main()
{
	//for循环
	//从数字0打印到数字9

	for (int i = 1; i <= 10; i++)
	{
		for (int j = 0; j <= 10; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
	system("pause");

	return 0;

}
#include<iostream>
using namespace std;

int main()
{
	//for循环
	//打印9 9乘法表

	for (int i = 1; i <= 9; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << j << "*" << i<<"="<<i*j<<" ";
		}
		cout << endl;
	}
	system("pause");

	return 0;

}

3.跳转语句

3.1.1break

#include<iostream>
using namespace std;

int main()
{
	//break使用
	//1.出现在switch语句中
	cout << "请选择副本难度" << endl;
	cout << "1:普通 ,2:中等,3:困难" << endl;
	int select;
	cin >> select;
	switch (select)
	{
	case 1:
		cout << "普通" << endl;
		break;
	case 2:
		cout << "中等" << endl;
		break;
	default:
		cout << "困难" << endl;
		break;
	}
	//2.出现在循环语句中
	for (int i = 0; i <= 10; i++)
	{
		if (i == 5)
		{
			break;
		}
		cout << i << endl;
	}
	//3.出现在嵌套循环语句中
	for (int i = 0; i <10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (j == 5)
			{
				break;//退出内层循环
			}
			cout << "*";
		}
		cout << endl;
	}
	system("pause");

	return 0;

}

3.1.2continue

 

#include<iostream>
using namespace std;

int main()
{
	//continue语句
	for (int i = 0; i <10; i++)
	{
		if (i % 2 == 0)
		{
			continue;//执行到此就不再向下执行,执行下一次循环
		}
		cout << i << endl;
	}
	system("pause");

	return 0;

}

 3.1.3goto语句

#include<iostream>
using namespace std;

int main()
{
	//goto语句
	cout << "1.XXX" << endl;
	cout << "2.XXX" << endl;
	goto FLAG;
	//标记一般大写
	cout << "3.XXX" << endl;
	cout << "4.XXX" << endl;
	FLAG:
	cout << "5.XXX" << endl;
	system("pause");

	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值