八、循环(一)

1、for循环

1)for循环语法

//for循环语法
for(初始化表达式;运行条件表达式;变动表达式)
{
    循环内容;
}

//示例
for(int i=0;i<10;i++)
{
    std::cout<<i<<std::endl;
}
std::cout<<"循环结束";

①打印9-1

//打印9-1
#include <iostream>

int main()
{
    for (int i = 9; i > 0; i--)
    {
        std::cout << i << std::endl;
    }
}

②打印所有大写字母

//打印所有大写字母
#include <iostream>

int main()
{
    for (char i = 65; i <=90; i++)  //A的ascii码为65
    {
        std::cout << i << std::endl;
    }
}

③打印所有大写字母,但是每行只有五个字母

//打印所有大写字母
#include <iostream>

int main()
{
    int count{ 0 };
    for (char i = 65; i <=90; i++)  //A的ascii码为65
    {
       
        count++;
        if (count > 5)
        {
            std::cout << std::endl;
            count = 1;
        }
        std::cout << i;
    }
}

法二:

//打印所有大写字母
#include <iostream>

int main()
{
	for (char a{ 65 }; a < 91; a++)
	{
		if ((!((a - 65) % 5)) && (a > 65)) //取余数,如果余数=0,则进行换行
		{
			std::cout << std::endl;
		}
		std::cout << a;
	}
}

④要求用户输入一个大写字母,打印从这个字母后的所有大写字母,比如用户输入C,那么屏蔽上打印DEFG..Z

//输入一个大写字母,打印从这个字母后的所有大写字母
#include <iostream>

int main()
{
	char userIn;
	std::cout << "请输入一个大写字母:";
	std::cin >> userIn;
	for (char a = userIn + 1; a < 91; a++)
	{
		if (!((a - userIn - 1) % 5) && (a > (userIn + 1)))
		{
			std::cout << std::endl;
		}
		std::cout << a;
	}
}

2、for循环之网银证书密码攻击

需求:设计一个系统来模拟网银密码被攻击的场景,用户输入一个6位数字密码,然后我们破解它的密码并显示出来

#include <iostream>
#include <conio.h>
int main()
{
	int password, repassword;
	std::cout << "请设置一个6位数字的密码:";
	std::cin >> password;
	std::cout << "请再次输入你的密码:";
	std::cin >> repassword;
lset:
	if (password == repassword)
	{
		system("cls");
		//破解程序
		std::cout << "破解程序启动";
		for (int hackpass = 0; hackpass < 1000000; hackpass++)
		{
			if (hackpass == password)
			{
				std::cout << "你的密码为:" << hackpass << std::endl;
				goto lexit;
			}
		}
	}
	else
	{
		goto lset;
	}
lexit:
	std::cout << "密码破解成功,程序退出!";
}

3、for循环补充知识

1)循环的嵌套

①打印九九乘法表,输出结果如下

#include <iostream>
#include <iomanip>

int main()
{
	std::cout << std::setfill('0');
	for (int y{}; y < 10; y++)
	{
		for (int x{}; x < 10; x++)
		{
			std::cout << std::setw(3)<<x * y << " ";
		}
		std::cout << std::endl;
		
	}
}

2)跳出for循环的三种方式

能力 语句 说明
1 continue 跳出本次循环,进入下一次循环
2 break 跳出循环
3 goto 跳出嵌套循环,即跳出所有循环

①continue:跳出本次循环

//continue:跳出本次循环
#include <iostream>
#include <iomanip>

int main()
{
	std::cout << std::setfill('0');
	for (int y{}; y < 10; y++)
	{
		if (y == 0) continue;
		for (int x{}; x < 10; x++)
		{
			if (x == 0) continue;
			std::cout << std::setw(3) << x * y << " ";
		}
		std::cout << std::endl;

	}
}

②break:跳出循环

//break:跳出循环
#include <iostream>
#include <iomanip>

int main()
{
	std::cout << std::setfill('0');
	for (int y{}; y < 10; y++)
	{
		for (int x{}; x < 10; x++)
		{
			if (x == 5) break;
			std::cout << std::setw(3) << x * y << " ";
		}
		std::cout << std::endl;

	}
}

注:break只能跳出本层循环,但是无法跳出上层循环

③goto:可以跳出所有循环

3)for循环的变体

//正常for循环
for(初始化;条件;变动表达式)
{
    
}

//变体一:无初始化,只要前面声明过,for中就不需要初始化
for(;条件;变动表达式)
{
    
}

//变体二:无变动表达式
for(;条件;)       //死循环
{
    
}

//变体三:
for(;;)       //无条件执行,中间是一个条件表达式,即bool值
{
    
}