[C++ Entry Guide] (08) Loop

insert image description here

Sometimes, it may be necessary to execute the same block of code multiple times. In general, statements are executed sequentially: the first statement in a function is executed first, followed by the second statement, and so on.

Programming languages ​​provide various control structures that allow more complex execution paths.

1. while loop

Function : If the loop condition is met, execute the loop statement.

Grammar :while(循环条件){循环语句块}

Flowchart :
insert image description here
Example :

#include <iostream>

using namespace std;

int main()
{
    
    
	int sum = 0, val = 50;
	// 只要 val 的值小于等于10,while 循环就会持续执行
	while (val <= 100)
	{
    
    
		sum += val;  // 将 sum + val 赋值给 sum
		++val;       // 将 val + 1
	}
	
	cout << "Sum of 1 to 10 inclusive is " 
		<< sum << endl;

	return 0;
}

2. The do...while loop

Function : If the loop condition is met, execute the loop statement.

Grammar :do{循环语句}while(循环条件)

Flowchart :
insert image description here

Note : The difference with while is that do ... while will first execute the loop statement once, and then judge the loop condition.

Example : Find the number of daffodils in all three-digit numbers.

#include <iostream>

using namespace std;

int main()
{
    
    
	// 求出三位数中,所有的水仙花数
	int num = 100;
	int ge = 0;
	int shi = 0;
	int bai = 0;
	
	do{
    
    
		ge = num % 10;
		shi = num / 10 % 10;
		bai = num / 100;
		if (ge * ge * ge + shi * shi * shi + bai * bai * bai == num) 
		{
    
    
			cout << num << endl;
		}	
		num++;
	} while (num <= 999);

	return 0;
}

Three, for loop

Function : If the loop condition is met, execute the loop statement.

Grammar :for(起始表达式; 条件表达式;末尾循环体){循环语句;}

Example : Calculate the sum of all odd numbers between 1 and N.

#include <iostream>

using namespace std;

int main()
{
    
    
	int num = 0;
	int sum = 0;

	cin >> num;

	for (int i = 0; i <= num; i++)
	{
    
    
		if (i % 2 != 0) 
		{
    
    
			sum += i;
		}
	}
	cout << sum << endl;

	return 0;
}

4. Nested loops

Function : Nest another layer of loop in the loop to solve some practical problems.

Example : Realize the nine-nine multiplication table

#include <iostream>

using namespace std;

int main()
{
    
    

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

	return 0;
}

Five, jump statement

5.1 break statement

The break statement in C++ has the following two usages:

  • When a break statement appears inside a loop, the loop terminates immediately and program flow continues with the next statement immediately following the loop.
  • It can be used to terminate a case in a switch statement.

5.2 continue statement

The continue statement in C++ is a bit like the break statement. But it is not forced to terminate, continue will skip the code in the current loop and force the next loop to start.

For a for loop, the continue statement causes the conditional test and loop increment portion to be executed. For while and do…while loops, the continue statement causes program control to return to the conditional test.

5.3 goto statement

The goto statement allows an unconditional transfer of control to a marked statement within the same function.

Note : The goto statement is not recommended in any programming language. Because it makes the control flow of the program difficult to follow, making the program difficult to understand and difficult to modify. Any program that uses a goto statement can be rewritten to not use a goto statement.

Guess you like

Origin blog.csdn.net/duoduo_11011/article/details/130642278