栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++基础入门4——程序流程结构

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++基础入门4——程序流程结构

int main()
{
	//打印10*10正方形点阵
	for (int i = 0; i < 10; i++)
	{
		for (int i = 0; i < 10; i++)
		{
			cout << "*";
		}
		cout << endl;
	}
	return 0;
}

 

1. 选择结构 1)if语句

 实例:

//多条if语句形式
int main()
{
	int pig1 = 0;
	int pig2 = 0;
	int pig3 = 0;
	int max = 0;
	cout << "请输入三只小猪的体重:"  << endl;
	cout << "pig1:" << endl;
	cin >> pig1;
	cout << "pig2:" << endl;
	cin >> pig2;
	cout << "pig3:" << endl;
	cin >> pig3;
	if (pig1 >= pig2 && pig1 >= pig3)
	{
		cout << "体重最重的小猪是小猪1"  << endl;
	}
	else if (pig1 >= pig2 && pig1<= pig3)
	{
		cout << "体重最重的小猪是小猪3" << endl;
	}
	else if (pig1 <= pig2 && pig2 <= pig3)
	{
		cout << "体重最重的小猪是小猪3" << endl;
	}
	else if (pig1 <= pig2 && pig2 >= pig3)
	{
		cout << "体重最重的小猪是小猪2" << endl;
	}
	return 0;
}
//嵌套if语句形式
int main()
{
	int pig1 = 0;
	int pig2 = 0;
	int pig3 = 0;
	int max = 0;
	cout << "请输入三只小猪的体重:"  << endl;
	cout << "pig1:" << endl;
	cin >> pig1;
	cout << "pig2:" << endl;
	cin >> pig2;
	cout << "pig3:" << endl;
	cin >> pig3;
	if (pig1 > pig2)
	{
		if (pig1 > pig3)
		{
			cout << "小猪1最重" << endl;
		}
		else
		{
			cout << "小猪3最重" << endl;
		}
	}
	else
	{
		if (pig2 > pig3)
		{
			cout << "小猪2最重" << endl;
		}
		else
		{
			cout << "小猪3最重" << endl;
		}
	}
	return 0;
}

2)三目运算符

3)switch语句 

 

//switch语句只能判断整型或者字符型,不能对区间进行判定
int main()
{
	int score = 0;
	cout << "请输入您的评分:" << endl;
	cin >> score;
	switch (score)
	{
	case 5: 
		cout << "您对电影的评级为优秀" << endl;
		break;
	case 4:
		cout << "您对电影的评级为良好" << endl;
		break;
	case 3:
		cout << "您对电影的评级为不错" << endl;
		break;
	case 2:
		cout << "您对电影的评级为还行" << endl;
		break;
	case 1:
		cout << "您对电影的评级为烂片" << endl;
		break;
	}
	return 0;
}
2. 循环结构 1)while循环语句

 

int main()
{
	int RandNumber = rand() % 100 + 1;//生成一个1到100之间的随机数
	cout << "系统给出的随机数字是:" << RandNumber << endl;
	int GuessNumber = 0;
	cout << "请输入您猜想的数字:" << endl;
	cin >> GuessNumber;
	while (GuessNumber != RandNumber)
	{
		if (GuessNumber > RandNumber)
		{
			cout << "抱歉,您猜测的数字过大。请继续!" << endl;
			cout << "请输入您猜想的数字:" << endl;
		}
		else
		{
			cout << "抱歉,您猜测的数字过小。请继续!" << endl;
			cout << "请输入您猜想的数字:" << endl;
		}
		cin >> GuessNumber;
	}
	cout << "恭喜,您猜对了。" << endl;
	return 0;
}

2)do...while循环语句

int main()
{
	//输出0-9之间的数字
	int num = 0;
	do
	{
		cout << num << endl;
		num++;
	} while (num < 10);
	return 0;
}

 

int main()
{
	//输出所有的水仙花数
	int num = 100;
	int num100 = 0;
	int num10 = 0;
	int num1 = 0;
	do
	{
		num100 = num / 100;
		num10 = (num - num100*100)/10;
		num1 = num % 10;
		if (num == pow(num100,3) + pow(num10, 3) + pow(num1, 3))
		{
			cout << num << endl;
		}
		num++;
	} while (num < 1000);
	return 0;
}

 

3)for循环语句

 

 

int main()
{
	//敲桌子问题
	for (int num = 1; num <= 100; num++)
	{
		if (num % 10 == 7)
		{
			cout << "敲桌子" << endl;
		}
		else if (num / 10 == 7)
		{
			cout << "敲桌子" << endl;
		}
		else if (num % 7 == 0)
		{
			cout << "敲桌子" << endl;
		}
		else
		{
			cout << num << endl;
		}
	}
	return 0;
}

4)嵌套循环

 

 

 

int main()
{
	//打印九九乘法口诀表
	for (int i = 1; i < 10; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << j << "x" << i << "=" << i * j << " ";
		}
		cout << endl;
	}
	return 0;
}

 

3. 跳转语句 1)break语句

 2)continue语句

在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环

 

 

3)goto语句

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/384389.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号