C++ study notes (2)--program flow structure, array, function

Table of contents

1. Select structure:

1. The if statement:

2. Ternary operator:

 3. Switch statement:

Second, the cycle structure:

1. while loop statement

2. do...while loop statement

3. for loop statement

 3. Array:

1. One-dimensional array:

2. Bubble sort:

2. Two-dimensional array:

4. Function:

1. Definition of function:

2. Function call

3. Value transfer and address transfer:

5. Function declaration

6. File writing function:


1. Select structure:

1. The if statement:

Function: Execute the statement that satisfies the condition. Divided into the following three forms:

  • Single-line format if statement

  • Multi-line format if statement

  • multi-conditional if statement

int main() {

	//选择结构-单行if语句
	int score = 200;

	//if语句
	//注意事项,在if判断语句后面,不要加分号
	if (score > 600)
	{
		cout << "我考上了一本大学!!!" << endl;
	}
    
    //多行格式if语句
	if (score > 600)
	{
		cout << "我考上了一本大学" << endl;
	}
	else
	{
		cout << "我未考上一本大学" << endl;
	}

    //多条件if语句:
    if (score > 600)
	{
		cout << "我考上了一本大学" << endl;
	}
	else if (score > 500)
	{
		cout << "我考上了二本大学" << endl;
	}
	else if (score > 400)
	{
		cout << "我考上了三本大学" << endl;
	}
	else
	{
		cout << "我未考上本科" << endl;
	}

	system("pause");

	return 0;
}

At the same time, C++ also supports the empty if body, but a semicolon is required;

int main()
{
	if (1 > 0);

	system("pause");
	return 0;
}

2. Ternary operator:

Function: Realize simple judgment through the ternary operator

grammar:表达式1 ? 表达式2 :表达式3

int main() {

	int a = 10;
	int b = 20;
	int c = 0;

	c = a > b ? a : b;
	cout << "c = " << c << endl;  //20

	//C++中三目运算符返回的是变量,可以继续赋值

	(a > b ? a : b) = 100;

	cout << "a = " << a << endl; //10
	cout << "b = " << b << endl; //100
	cout << "c = " << c << endl; //20

	system("pause");

	return 0;
}

 3. Switch statement:

Function: Execute multi-conditional branch statements

grammar:

switch(表达式)

{

	case 结果1:执行语句;break;

	case 结果2:执行语句;break;

	...

	default:执行语句;break;

}

Note 1: The expression type in the switch statement can only be integer or character

Note 2: If there is no break in the case, the program will continue to execute downwards

Summary: Compared with the if statement, when judging multiple conditions, the structure of the switch is clear and the execution efficiency is high. The disadvantage is that the switch cannot judge the interval

nt main() {

	//请给电影评分 
	//10 ~ 9   经典   
	// 8 ~ 7   非常好
	// 6 ~ 5   一般
	// 5分以下 烂片

	int score = 0;
	cout << "请给电影打分" << endl;
	cin >> score;

	switch (score)
	{
	case 10:
	case 9:
		cout << "经典" << endl;
		break;
	case 8:
		cout << "非常好" << endl;
		break;
	case 7:
	case 6:
		cout << "一般" << endl;
		break;
	default:
		cout << "烂片" << endl;
		break;
	}

	system("pause");

	return 0;
}

Second, the cycle structure:

1. while loop statement

Function: satisfy the loop condition, execute the loop statement

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

Explanation: == As long as the result of the loop condition is true, execute the loop statement ==

int main() {

	int num = 0;
	while (num < 10)
	{
		cout << "num = " << num << endl;
		num++;
	}
	
	system("pause");

	return 0;
}

2. do...while loop statement

Function: satisfy the loop condition, execute the loop statement

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

Note: The difference with while is that do...while will first execute the statement in do once, then judge whether the condition of the loop body is satisfied, and then decide whether to enter the loop body again, and the do statement will only be executed once.

int main() {

	int num = 0;

	do
	{
		cout << num << endl;
		num++;

	} while (num < 10);
	
	
	system("pause");

	return 0;
}

3. for loop statement

Function: satisfy the loop condition, execute the loop statement

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

Note: The expressions in the for loop must be separated by semicolons  

int main() {

	for (int i = 0; i < 10; i++)
	{
		cout << i << endl;
	}
	
	system("pause");

	return 0;
}

 3. Array:

The so-called array is a collection that stores data elements of the same type

Feature 1: Each data element in the array is of the same data type

Feature 2: Arrays are composed of contiguous memory locations

1. One-dimensional array:

Three ways to define a one-dimensional array:

  1. 数据类型 数组名[ 数组长度 ];

  2. 数据类型 数组名[ 数组长度 ] = { 值1,值2 ...};

  3. 数据类型 数组名[ ] = { 值1,值2 ...};

int main() {

	//定义方式1
	//数据类型 数组名[元素个数];
	int score[10];

	//利用下标赋值
	score[0] = 100;
	score[1] = 99;
	score[2] = 85;

	//利用下标输出
	cout << score[0] << endl;
	cout << score[1] << endl;
	cout << score[2] << endl;


	//第二种定义方式
	//数据类型 数组名[元素个数] =  {值1,值2 ,值3 ...};
	//如果{}内不足10个数据,剩余数据用0补全
	int score2[10] = { 100, 90,80,70,60,50,40,30,20,10 };
	
	//逐个输出
	//cout << score2[0] << endl;
	//cout << score2[1] << endl;

	//一个一个输出太麻烦,因此可以利用循环进行输出
	for (int i = 0; i < 10; i++)
	{
		cout << score2[i] << endl;
	}

	//定义方式3
	//数据类型 数组名[] =  {值1,值2 ,值3 ...};
	int score3[] = { 100,90,80,70,60,50,40,30,20,10 };

	for (int i = 0; i < 10; i++)
	{
		cout << score3[i] << endl;
	}

	system("pause");

	return 0;
}

Use of 1D array names:

  1. Can count the length of the entire array in memory

  2. You can get the first address of the array in memory

  3. The array name is a constant and cannot be assigned

  4. Print the array name directly, you can view the first address of the memory occupied by the array

  5. Perform sizeof on the array name to get the size of the memory space occupied by the entire array

int main() {

	//数组名用途
	//1、可以获取整个数组占用内存空间大小
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };

	cout << "整个数组所占内存空间为: " << sizeof(arr) << endl;
	cout << "每个元素所占内存空间为: " << sizeof(arr[0]) << endl;
	cout << "数组的元素个数为: " << sizeof(arr) / sizeof(arr[0]) << endl;

	//2、可以通过数组名获取到数组首地址
	cout << "数组首地址为: " << (int)arr << endl; //也就是arr[0]的地址
	cout << "数组中第一个元素地址为: " << (int)&arr[0] << endl;
	cout << "数组中第二个元素地址为: " << (int)&arr[1] << endl;

	//arr = 100; 错误,数组名是常量,因此不可以赋值


	system("pause");

	return 0;
}

2. Bubble sort:

Function: The most commonly used sorting algorithm, sorting the elements in the array

  1. Compare adjacent elements. If the first is bigger than the second, swap them both.

  2. Do the same for each pair of adjacent elements, and when done, find the first maximum value.

  3. Repeat the above steps, each time the number of comparisons -1, until no comparison is required

int main() {

	int arr[9] = { 4,2,8,0,5,7,1,3,9 };
	int len = sizeof(arr) / sizeof(arr[0]);

	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - 1 - i; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}

//打印数据
	for (int i = 0; i < 9; i++)
	{
		cout << arr[i] << endl;
	}

	system("pause");

	return 0;
}

2. Two-dimensional array:

Four ways to define a two-dimensional array:

  1. 数据类型 数组名[ 行数 ][ 列数 ];

  2. 数据类型 数组名[ 行数 ][ 列数 ] = { {数据1,数据2 } ,{数据3,数据4 } };

  3. 数据类型 数组名[ 行数 ][ 列数 ] = { 数据1,数据2,数据3,数据4};

  4. 数据类型 数组名[ ][ 列数 ] = { 数据1,数据2,数据3,数据4};

When defining a two-dimensional array, if the data is initialized, the number of rows can be omitted, and the columns must be cleared regardless of the definition method

int main() {

	//方式1  
	//数组类型 数组名 [行数][列数]
	int arr[2][3];
	arr[0][0] = 1;
	arr[0][1] = 2;
	arr[0][2] = 3;
	arr[1][0] = 4;
	arr[1][1] = 5;
	arr[1][2] = 6;

	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << " ";
		}
		cout << endl;
	}

	//方式2 
	//数据类型 数组名[行数][列数] = { {数据1,数据2 } ,{数据3,数据4 } };
	int arr2[2][3] =
	{
		{1,2,3},
		{4,5,6}
	};

	//方式3
	//数据类型 数组名[行数][列数] = { 数据1,数据2 ,数据3,数据4  };
	int arr3[2][3] = { 1,2,3,4,5,6 }; 

	//方式4 
	//数据类型 数组名[][列数] = { 数据1,数据2 ,数据3,数据4  };
	int arr4[][3] = { 1,2,3,4,5,6 };
	
	system("pause");

	return 0;
}

2D array array name

  • View the memory space occupied by a two-dimensional array

  • Get the first address of the two-dimensional array

int main() {

	//二维数组数组名
	int arr[2][3] =
	{
		{1,2,3},
		{4,5,6}
	};

	cout << "二维数组大小: " << sizeof(arr) << endl;
	cout << "二维数组一行大小: " << sizeof(arr[0]) << endl;
	cout << "二维数组元素大小: " << sizeof(arr[0][0]) << endl;

	cout << "二维数组行数: " << sizeof(arr) / sizeof(arr[0]) << endl;
	cout << "二维数组列数: " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;

	//地址
	cout << "二维数组首地址:" << arr << endl;
	cout << "二维数组第一行地址:" << arr[0] << endl;
	cout << "二维数组第二行地址:" << arr[1] << endl;

	cout << "二维数组第一个元素地址:" << &arr[0][0] << endl;
	cout << "二维数组第二个元素地址:" << &arr[0][1] << endl;

	system("pause");

	return 0;
}

4. Function:

1. Definition of function:

1. Return value type: A function can return a value. in the function definition

2. Function name: give the function a name

3. Parameter list: When using this function, the data passed in

4. Function body statement: the code in curly braces, the statement to be executed in the function

5. Return expression: linked to the return value type, after the function is executed, the corresponding data will be returned

grammar:

返回值类型 函数名 (参数列表)
{
       函数体语句
       return表达式

}
//函数定义
int add(int num1, int num2)
{
	int sum = num1 + num2;
	return sum;
}

2. Function call

Function: use the defined function

grammar:函数名(参数)

Example:

//函数定义
int add(int num1, int num2) //定义中的num1,num2称为形式参数,简称形参
{
	int sum = num1 + num2;
	return sum;
}

int main() {

	int a = 10;
	int b = 10;
	//调用add函数
	int sum = add(a, b);//调用时的a,b称为实际参数,简称实参
	cout << "sum = " << sum << endl;

	system("pause");

	return 0;
}

  3. Value transfer and address transfer:

1. Value transfer:

  • The so-called passing by value means that when the function is called, the actual parameter passes the value to the formal parameter.

  • When passing by value, if the formal parameter occurs, it will not affect the actual parameter

2. Address delivery:

  • The so-called address passing means that the actual parameter is passed to the formal parameter when the function is called.

  • When the address is passed, if the formal parameter occurs, the actual parameter will also change

#include<iostream>
using namespace std;
#pragma warning(disable:4996)
void swap(int num1, int num2)
{
	num1 = 20;
	num2 = 10;
}
void swap_address(int &num1, int &num2) {
	num1 = 20;
	num2 = 10;
}
int main() {

	int a = 10;
	int b = 20;

	swap(a, b);

	cout << "mian中的 a = " << a << endl; //10
	cout << "mian中的 b = " << b << endl; //20

	swap_address(a, b);

	cout << "mian中的 a = " << a << endl; //20
	cout << "mian中的 b = " << b << endl; //10

	system("pause");

	return 0;
}

4. Common styles of functions

1. No return without participation 2. No return with participation

3. Return without participation 3. Return with participation

//函数常见样式
//1、 无参无返
void test01()
{
	//void a = 10; //无类型不可以创建变量,原因无法分配内存
	cout << "this is test01" << endl;
	//test01(); 函数调用
}

//2、 有参无返
void test02(int a)
{
	cout << "this is test02" << endl;
	cout << "a = " << a << endl;
}

//3、无参有返
int test03()
{
	cout << "this is test03 " << endl;
	return 10;
}

//4、有参有返
int test04(int a, int b)
{
	cout << "this is test04 " << endl;
	int sum = a + b;
	return sum;
}

5. Function declaration

Function: Tell the compiler the name of the function and how to call the function. The actual body of the function can be defined separately.

  • A function can be declared multiple times , but a function can only be defined once

Example:

//声明可以多次,定义只能一次
//声明
int max(int a, int b);
int max(int a, int b);
//定义
int max(int a, int b)
{
	return a > b ? a : b;
}

int main() {

	int a = 100;
	int b = 200;

	cout << max(a, b) << endl;

	system("pause");

	return 0;
}

6. File writing function:

Function: To make the code structure clearer, there are generally 4 steps in writing:

  1. Create a header file with the suffix .h

  2. Create a source file with a suffix of .cpp

  3. Write function declarations in header files

  4. Write the definition of the function in the source file

Three files, the first .h file is used to declare the function. The second .cpp file is used to define the function body. The third main function file is used.

Example:

//swap.h文件
#include<iostream>
using namespace std;

//实现两个数字交换的函数声明
void swap(int a, int b);
//swap.cpp文件
#include "swap.h"

void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}
//main函数文件
#include "swap.h"
int main() {

	int a = 100;
	int b = 200;
	swap(a, b);

	system("pause");

	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_60414376/article/details/126823191