program flow structure

C/C++ supports the most basic three kinds of program running structures: sequence structure, selection structure, loop structure

  • Sequential structure: programs are executed sequentially without jumps
  • Selection structure: according to whether the conditions are met, the corresponding functions are selectively executed
  • Loop structure: cyclically execute a certain piece of code according to whether the condition is satisfied

select structure

if statement

Three forms of if statement:

  • single line if statement
  • multi-line if statement
  • Multiple conditional if statement

Single-line if statement: if (judgment condition) {execute code}

#include <iostream>
using namespace std;

int main() {
    
    
	int score;

	cout << "请输入一个分数:" << endl;

	cin >> score;

	if (score >= 600)  // if条件后面不能加分号 
	{
    
    
		cout << "该分数已超过重点线,恭喜!" << endl;
	}

	system("pause");

	return 0;
}

Multi-line if statement: if (judgment condition) {code to be executed if the condition is met} else {code to be executed if the condition is not met}

#include <iostream>
using namespace std;

int main() {
    
    
	int score;

	cout << "请输入一个分数:" << endl;

	cin >> score;

	if (score >= 600)  // if条件后面不能加分号 
	{
    
    
		cout << "该分数已超过重点线,恭喜!" << endl;
	}
	else
	{
    
    
		cout << "该分数未超过重点线,还需继续努力!" << endl;
	}

	system("pause");

	return 0;
}

Multi-conditional if statement: if (condition 1) {condition 1 satisfies the executed code} else if (condition 2) {condition 2 satisfies the executed code}...else {neither condition satisfies the executed code}

#include <iostream>
using namespace std;

int main() {
    
    
	int score;

	cout << "请输入一个分数:" << endl;

	cin >> score;

	if (score >= 600)  // if条件后面不能加分号 
	{
    
    
		cout << "该分数已超过重点线,可喜可贺!" << endl;
	}
	else if (score >=500) 
	{
    
    
		cout << "该分数已超过一本线,恭喜!" << endl;
	}
	else
	{
    
    
		cout << "该分数未上线,还需继续努力!" << endl;
	}

	system("pause");

	return 0;
}

Ternary operator

Simple judgment can be realized through the ternary operator.

Syntax: expression1 ? expression2 : expression3

If the value of expression 1 is true, execute expression 2 and return the result of expression 2;
if the value of expression 1 is false, execute expression 3 and return the result of expression 3.

#include <iostream>
using namespace std;

int main() {
    
    
	int a = 10;
	int b = 20;
	int c;

	c = a > b ? a : b;  

	cout << c << endl;  // 结果为20

	a > b ? a : b = 100;  // 三目运算符返回的是变量,可以继续赋值

	cout << "a = " << a << endl;  // 结果为10
	cout << "b = " << b << endl;  // 结果为100

	system("pause");

	return 0;
}

switch statement

The switch statement is used to execute multi-conditional branch statements; the expression can only be integer or character type when judging, and cannot be written as a numerical range.
Compared with the if statement, the switch statement has a clear structure and higher execution efficiency.
If there is no break statement in the case, the program will continue to execute downwards.

grammar:

switch(表达式)  
{
    
    
case 结果1: 
    执行代码;
	break;
	
case 结果2: 
    执行代码;
	break;
	
	...
	
default: 
    执行代码;
	break;
}
#include <iostream>
using namespace std;

int main() {
    
    
	int score;

	cout << "请输入电影评分:" << endl;
	cin >> score;

	switch (score)  // 表达式须为整型或字符型
	{
    
    
	case 10:
		cout << "非常经典的一部电影!" << endl;
		break;  // 如果没有break语句及时跳出,则程序会一直向下执行
	case 9:
		cout << "很不错的一部电影!" << endl;
		break;
	case 8:
		cout << "值得一看的一部电影!" << endl;
		break;
	default:
		cout << "这部电影不太行!" << endl;
		break;
	}

	system("pause");

	return 0;
}

loop structure

while loop statement

Syntax: while (loop condition) {execute code}

The code is executed as long as the result of the loop condition is true.

#include <iostream>
using namespace std;

int main() {
    
    
	int num = 0;

	while (num < 10)  // 不能加分号
	{
    
    
		cout << num << endl;
		num++;
	}

	system("pause");

	return 0;
}

Number of daffodils:

#include <iostream>
using namespace std;

int main() {
    
    
	int num = 100;
	int a;
	int b;
	int c;

	cout << "三位数的水仙花数有如下几个:" << endl;

	while (num >= 100 && num <= 999) {
    
    
		a = num % 10;  // 个位
		b = num / 10 % 10;  // 十位
		c = num / 100;  // 百位

		if (a * a * a + b * b * b + c * c * c == num) {
    
    
			cout << num << endl;
		}

		num++;
	}

	system("pause");

	return 0;
}

do...while loop statement

Syntax: do{execute code}while(loop condition);

do...while will execute the loop code once, and then judge the loop condition.

#include <iostream>
using namespace std;

int main() {
    
    
	int num = 0;

	do
	{
    
    
		cout << num << endl;
		num++;
	} while (num < 10);  // 需要加分号

	system("pause");

	return 0;
}

for loop statement

Syntax: for (start expression; condition expression; end loop body) {execute code}

for(①起始表达式; ②条件表达式; ③末尾循环体){
    
    
	④执行代码
}
这段循环代码的执行顺序为:① -->-->-->-->-->-->-->...
for循环中的①②③可以放在for后面的括号内,也可以放在括号外,但括号内的两个分号不能少。
#include <iostream>
using namespace std;

int main() {
    
    
	for (int i = 0; i < 10; i++) {
    
    
		cout << i << endl;
	}

	system("pause");

	return 0;
}

The following code has the same output as the above code:

#include <iostream>
using namespace std;

int main() {
    
    
	int i = 0;

	for (; ; ) {
    
    
		if (i >= 10) {
    
    
			break;
		}

		cout << i << endl;
		
		i++;
	}

	system("pause");

	return 0;
}

Print the multiplication table:

#include <iostream>
using namespace std;

int main() {
    
    
	for (int i = 1; i < 10; i++) {
    
    
		for (int j = 1; j <= i; j++) {
    
    
			cout << j << " * " << i << " = " << j * i << "\t";
		}

		cout << endl;
	}

	system("pause");

	return 0;
}

jump statement

break

The break statement is used to jump out of a selection structure or a loop structure.

When to use break:

  • Appears in the switch conditional statement, the function is to terminate the case and jump out of the switch
  • Appears in a loop statement, the function is to jump out of the current loop statement
  • Appears in nested loops, the function is to jump out of the nearest inner loop statement

continue

The continue statement acts on the loop, the purpose is to skip this loop and continue to execute the next loop.

#include <iostream>
using namespace std;

int main() {
    
    
	for (int i = 1; i < 100; i++) {
    
    
		if (i % 2 == 0) {
    
    
			continue;  // 可用于依据某些条件进行筛选
		}

		cout << i << endl;
	}

	system("pause");

	return 0;
}

goto

The goto statement can unconditionally jump to the statement.

Syntax: goto tag;

If the marked name exists, when the goto statement is executed, it will jump to the marked position.

It is not recommended to use the goto statement in the program, so as not to cause confusion in the program flow.

#include <iostream>
using namespace std;

int main() {
    
    
	for (int i = 1; i < 10; i++) {
    
    
		if (i == 5) {
    
    
			goto FLAG;
		}

		cout << i << endl;
	}

	FLAG:
	cout << "程序结束!" << endl;

	system("pause");

	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_48158964/article/details/131481407
Recommended