C++ knowledge expansion: infinite loop, compound statement scope, multi-file scope issues

knowledge development

Infinite loop

Since the loop will always execute when the condition is met, we need to keep changing some variables in the loop body so that the loop can eventually end. If the conditions of the loop are permanently satisfied, the program will always execute the code in the loop, resulting in a waste of computing resources. This kind of loop is also called an infinite loop.

Infinite loop

#include<iostream>
using namespace std;
//死循环
int main()
{
    
    
	for (int i = 0 ; i >= 0 ; i++)
	{
    
    
	cout << "输出数字:" << i << endl;
	}
	return 0;
}
	

This example is an infinite loop. After the program is executed, since the condition of i>=0 is always satisfied, the loop will print increasing numbers until the computer's resources are exhausted.

There is often a need to decrement the counter in the recirculation. This is a problem that needs attention:
the trap of decrementing the for statement

#include<iostream>
using namespace std;
//递减for语句的陷阱
int main()
{
    
    
	for unsigned i = 10 ; i >= 0 ; i--)
	{
    
    
	cout << " 输出数字:" << i <<endl;
	}
	return 0;
}

When writing an incrementing for loop, unsigned int is often used as a counter. This is because the counter generally starts from 0, and the semantics of using unsigned int is clear; and if we use it in the decrementing for loop, the program will fall into infinite Loop, this is because after i is decremented to 0, if it continues to decrement by 1, it will overflow and become the upper limit of the unsigned type.

Scope of Compound Statements

In the head of the compound statement, we can not only use external variables, but also directly declare new variables. The scope of such local variables will be the entire compound statement. For some variables that can only be used inside the compound statement, we can use this method to prevent its scope from spreading to the outside, which is beneficial to structured programming.

Scope of Compound Statements

#include<iostream>
using namespace std;
//复合语句的作用域
int main()
{
    
    
//复合语句
if ( int a = 0 )
	cout << "a的值为:" << a <<endl;
else
 	cout << "a的值为:" << a << endl;
 	//a在复合语句外不可见
 	//cout <<"a的值为:" << a << endl;
 	return 0 ;
}
 	

The example shows the case where variables are defined at the head of a conditional statement. a is visible in the conditional statement, and is automatically destroyed after the statement ends. If we delete the comment of the last print statement, the compiler will report an error because it cannot find the definition of a.
like this

#include<iostream>
using namespace std;
//复合语句的作用域
int main()
{
    
    
//复合语句
if ( int a = 0 )
	cout << "a的值为:" << a <<endl;
else
 	cout << "a的值为:" << a << endl;
 	//a在复合语句外不可见
 	cout <<"a的值为:" << a << endl;
 	return 0 ;
}
 	

The error is reported as:
insert image description here
Note When defining variables in the if statement, we also implicitly use the initial value of the variable as the condition of the if statement. The more common example of declaring variables at the head of a compound statement is the declaration of the first part of the for loop head that has appeared in the previous example, such as "for (int i= 0; i>=0; i++)" "int i= 0;", there is no hidden semantic problem here, even if it is an empty statement, the loop can still be executed as usual.

Multiple file scope issues

Global variables can be accessed anywhere in the same source code file where the variable is defined. So when we have multiple files, can this variable also be accessed by code in another file?
multiple file scope

1.cpp
#include<iostream>
using namespace std;
//多文件的作用域
//extern int staticNum;
extern int globalNum;
int main()
{
    
    
	//cout << " a的值为:" << staticNum << endl;
	cout << "globalNum的值为:" << globalNum << endl;
	return 0 ;
}

2.cpp
//多文件的作用域
static int staticNum = 2int globalNum = 3 ;

The example demonstrates the problem of scoping in multiple files. If ordinary global variables want to be accessed in another file, an additional explicit declaration is required, and the extern keyword must be added before the declaration to indicate to the compiler that the variable has been defined in another file. This is different from ordinary declarations, which are just to solve the problem that variable names cannot be recognized before they are defined (often appearing in class definitions), while extern declarations allow the compiler to find variable definitions from other files . Of course, you can also use #include to directly include the entire file, so that all global variables in the file can be accessed.
In addition to general global variables, static global variables can also be defined. The static variables of functions and classes have different semantics from the static global variables here. A static global variable means that the scope of the variable is limited to this file and cannot be accessed in other files. Therefore, if we remove the comment about staticNum here, the compiler will report an error, because staticNum cannot be accessed in other files even if the extern declaration is used. In addition, const constants also imply static semantics, so they can be defined repeatedly in multiple files.

If it is helpful to you, please like and support~ *

Guess you like

Origin blog.csdn.net/m0_62870588/article/details/123703692