多线程循环输出abcc++
Program 1:
程序1:
#include <iostream>
using namespace std;
int A = 5;
int fun()
{
return A--;
}
int main()
{
int A = 5;
while (fun()) {
cout << A + ::A << " ";
}
return 0;
}
Output:
输出:
9 8 7 6 5
Explanation:
说明:
In the above program, we defined a function in which we are accessing the value of the global variable A and decreases it using the post-decrement operator.
在上面的程序中,我们定义了一个函数,在该函数中,我们访问全局变量A的值,并使用减后运算符减小它。
In the main() function, we also declared a local variable A. Both local and global variables contain the same value that is 5.
在main()函数中,我们还声明了局部变量A。 局部变量和全局变量都包含相同的值5。
Now look to the while loop, here we used the return value of fun() as a condition in the loop, as we know that condition is true till the returned value is non-zero.
现在来看while循环,在这里我们将fun()的返回值用作循环中的条件,因为我们知道条件是正确的,直到返回的值非零为止。
Note: fun() can't access the local variable A while in the main() global variable A is accessing using the Scope resolution operator (::).
注意:当在main()全局变量A中使用范围解析运算符(::)访问时, fun()无法访问局部变量A。
Interation1:
The function fun() returns 5, condition is true, and global 'A' become 4. Then
cout<<A+::A<<" ";
Here the above statement will print 5+4 that is 9.
Interation2:
The function fun() returns 4, condition is true, and global 'A' become 3. Then
cout<<A+::A<<" ";
Here the above statement will print 5+3 that is 8.
Interation3:
The function fun() returns 3, condition is true, and global 'A' become 2. Then
cout<<A+::A<<" ";
Here the above statement will print 5+2 that is 7.
Interation4:
The function fun() returns 2, condition is true, and global 'A' become 1. Then
cout<<A+::A<<" ";
Here the above statement will print 5+1 that is 6.
Interation5:
The function fun() returns 1, condition is true, and global 'A' become 0. Then
cout<<A+::A<<" ";
Here the above statement will print 5+0 that is 5.
Interation6:
The function fun() returns 0, and then condition will be false.
Then the final output will be "9 8 7 6 5".
Program 2:
程式2:
#include <iostream>
using namespace std;
int A = 5;
int& fun()
{
return A--;
}
int main()
{
int A = 5;
while (fun()) {
cout << A + ::A << " ";
}
return 0;
}
Output:
输出:
main.cpp:8:13: error: invalid initialization of non-const
reference of type ‘int&’ from an rvalue of type ‘int’
return A--;
~^~
Explanation:
说明:
Here, we defined a function that returning the reference.
在这里,我们定义了一个返回引用的函数。
In C++, A function that returns reference will be used as an LVALUE, but in the above program, we are using fun() as an RVALUE.
在C ++中,一个返回引用的函数将用作LVALUE ,但是在上述程序中,我们将fun()用作RVALUE 。
Read: What do 'lvalue' and 'rvalue' mean in C/C++?
阅读: 在C / C ++中,“左值”和“右值”是什么意思?
Program 3:
程式3:
#include <iostream>
#define MACRO(VAL) (char*)(&VAL + 1) - (char*)(&VAL)
using namespace std;
int main()
{
int i = 1;
int num = 0;
num = MACRO(num);
while (i <= num) {
cout << "India ";
i++;
}
return 0;
}
Output:
输出:
India India India India
Explanation:
说明:
In the above program, we defined a macro MACRO that will calculate the size in bytes of a specified variable just like sizeof() operator, here we calculate the displacement of variable, subtract the current address by next address, then we got different that denotes the size of the variable.
在上面的程序中,我们定义了一个宏MACRO ,它将像sizeof()运算符一样计算指定变量的字节大小,在这里我们计算变量的位移,将当前地址减去下一个地址,然后得到不同的值,表示变量的大小。
Here we used a 32-bit system, then MACRO will return 4 because here we passed an integer variable to it.
在这里我们使用的是32位系统,因此MACRO将返回4,因为在这里我们将整数变量传递给它。
num = MACRO(num);
Then the value of num will be 4. Then the loop will execute 4 times and print "India " 4 times.
然后num的值为4。然后循环将执行4次并打印“ India” 4次。
Recommended posts
推荐的帖子
C++ Looping | Find output programs | Set 1
C ++循环| 查找输出程序| 套装1
C++ Looping | Find output programs | Set 2
C ++循环| 查找输出程序| 套装2
C++ Looping | Find output programs | Set 3
C ++循环| 查找输出程序| 套装3
C++ Looping | Find output programs | Set 5
C ++循环| 查找输出程序| 套装5
C++ Operators | Find output programs | Set 1
C ++运算符| 查找输出程序| 套装1
C++ Operators | Find output programs | Set 2
C ++运算符| 查找输出程序| 套装2
C++ const Keyword | Find output programs | Set 1
C ++ const关键字| 查找输出程序| 套装1
C++ const Keyword | Find output programs | Set 2
C ++ const关键字| 查找输出程序| 套装2
C++ Reference Variable| Find output programs | Set 1
C ++参考变量| 查找输出程序| 套装1
C++ Reference Variable| Find output programs | Set 2
C ++参考变量| 查找输出程序| 套装2
C++ Conditional Statements | Find output programs | Set 1
C ++条件语句| 查找输出程序| 套装1
C++ Conditional Statements | Find output programs | Set 2
C ++条件语句| 查找输出程序| 套装2
C++ Switch Statement | Find output programs | Set 1
C ++转换语句| 查找输出程序| 套装1
C++ Switch Statement | Find output programs | Set 2
C ++转换语句| 查找输出程序| 套装2
C++ goto Statement | Find output programs | Set 1
C ++ goto语句| 查找输出程序| 套装1
C++ goto Statement | Find output programs | Set 2
C ++ goto语句| 查找输出程序| 套装2
翻译自: https://www.includehelp.com/cpp-tutorial/looping-find-output-programs-set-4.aspx
多线程循环输出abcc++