①The order of for statements ②Knock on the table ③Nested loop ④Multiplication formula table ⑤Break use ⑥Continue use ⑦Goto statement use [Dark horse programmer video]

The order of for statements

for (initial expression ; conditional expression ; end loop )
{ // ①②③④ representing a sequence program running } initial expression ① data structure can be created directly, i.e.: int i = 0;


Knock on the table

Rule : Among 1-100 numbers, the ones digit has 7, the tens digit has 7, and the multiples of 7 are printed and knocked on the table, and the rest are printed directly

#include <iostream>
using namespace std;

int main()
{
    
    
/*for(起始表达式①;条件表达式②;末尾循环体④)
{
	③   //①②③④代表程序运行的顺序
}
*起始表达式①可以直接创建数据结构,即:int i = 0;
*/

//敲桌子程序
//1-100个数里面,个位有7,十位有7,7的倍数就打印敲桌子,其余的直接打印

	for (int i = 1; i <= 100; i++)
	{
    
    
		if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
		{
    
    
			cout << "敲桌子" << endl;
		}
		else
			cout << i << endl;
	}
	return 0;
}
/* 嵌套循环:
*在一个循环里面不能重复使用一个字母     
* 程序选择具有就近原则
* 内层运行完后,外层一次
* 外层一次,内层一周,内部全部完成才可以运行外层
*/

Nested loop :

  • One letter cannot be reused in a cycle
  • Procedure selection has the principle of proximity
  • After the inner layer runs, the outer layer once
  • The outer layer is once, the inner layer is one week, and the inner layer is completely completed before the outer layer can be operated.

Multiplication formula table

#include <iostream>
using namespace std;

int main()
{
    
    
	/*乘法口诀表
	分析:
	* i为行,j为列
	* 当 列j > 行i 时候,不继续打印
	* i * j = i*j;
	*/
	for (int i = 1; i <= 9; i++)
	{
    
    
		for (int j = 1; j <= i; j++)
		{
    
    
			cout << i << " * " << j <<" = " << i * j << "  ";
		}
		cout << endl;
	}
	return 0;
}

break use

Used to select or loop structure

  • ① switch, terminate the case statement, jump out of switch
  • ② Cycle: Jump out of the current cycle
  • ③ Nesting: Jump out of the nearest inner loop
#include <iostream>
using namespace std;

int main()
{
    
    
	/*break用于选择或者循环结构
	* ① switch,终止 case 语句,跳出 switch
	* ② 循环 : 跳出当前循环
	* ③ 嵌套 : 跳出最近的内层循环
	*/

    //①.[switch :终止 case 语句,跳出 switch]链接:
    //(https://blog.csdn.net/weixin_42198265/article/details/113408616)

	//②. 循环 : 跳出当前循环
	int score = 0;
	while (score <= 10)
	{
    
    
		score++;
		cout << score << endl;
		if (score >= 5)
			break;
	}
	//③ 嵌套 : 跳出最近的内层循环
	for (int i = 1; i <= 5; i++)
	{
    
    
		for (int j = 1; j <= i; j++)
		{
    
    
			cout << j;
			if (j >= 3)
				break;
		}
		cout << endl;
	}
	return 0;
}

continue : In the loop, only skip the unfinished part of the loop

#include <iostream>
using namespace std;

int main()
{
    
    
	//continue : 在循环中,仅仅跳本次循环未循环完的部分
	//例子 : 1-100中只输出偶数,不输出奇数
	for (int i = 1; i <= 100; i++)
	{
    
    
		if (i % 2 == 0)
			cout << i << endl;
		else
			continue;
	}
	return 0;
}

The goto statement uses:

goto mark;

mark:

#include <iostream>
using namespace std;

int main()
{
    
    
	/*goto语句
	* 使用方法:
	* 
	* goto标记;
	* 标记:
    */
	cout << "1" << endl;
	cout << "2" << endl;
	goto end;
	cout << "3" << endl;
	cout << "4" << endl;
	end: //3和4不被=
	cout << "5" << endl;
	cout << "6" << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_42198265/article/details/113620544