多线程循环输出abcc++_C ++循环| 查找输出程序| 套装4
多線程循環(huán)輸出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 5Explanation:
說明:
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.
在上面的程序中,我們定義了一個函數(shù),在該函數(shù)中,我們訪問全局變量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()函數(shù)中,我們還聲明了局部變量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.
現(xiàn)在來看while循環(huán),在這里我們將fun()的返回值用作循環(huán)中的條件,因為我們知道條件是正確的,直到返回的值非零為止。
Note: fun() can't access the local variable A while in the main() global variable A is accessing using the Scope resolution operator (::).
注意:當(dāng)在main()全局變量A中使用范圍解析運算符(::)訪問時, fun()無法訪問局部變量A。
Interation1: The function fun() returns 5, condition is true, and global 'A' become 4. Thencout<<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. Thencout<<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. Thencout<<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. Thencout<<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. Thencout<<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.
在這里,我們定義了一個返回引用的函數(shù)。
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 ++中,一個返回引用的函數(shù)將用作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 IndiaExplanation:
說明:
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()運算符一樣計算指定變量的字節(jié)大小,在這里我們計算變量的位移,將當(dāng)前地址減去下一個地址,然后得到不同的值,表示變量的大小。
Here we used a 32-bit system, then MACRO will return 4 because here we passed an integer variable to it.
在這里我們使用的是32位系統(tǒng),因此MACRO將返回4,因為在這里我們將整數(shù)變量傳遞給它。
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。然后循環(huán)將執(zhí)行4次并打印“ India” 4次。
Recommended posts
推薦的帖子
- C++ Looping | Find output programs | Set 1 - C ++循環(huán)| 查找輸出程序| 套裝1 
- C++ Looping | Find output programs | Set 2 - C ++循環(huán)| 查找輸出程序| 套裝2 
- C++ Looping | Find output programs | Set 3 - C ++循環(huán)| 查找輸出程序| 套裝3 
- C++ Looping | Find output programs | Set 5 - C ++循環(huán)| 查找輸出程序| 套裝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關(guān)鍵字| 查找輸出程序| 套裝1 
- C++ const Keyword | Find output programs | Set 2 - C ++ const關(guān)鍵字| 查找輸出程序| 套裝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 ++轉(zhuǎn)換語句| 查找輸出程序| 套裝1 
- C++ Switch Statement | Find output programs | Set 2 - C ++轉(zhuǎn)換語句| 查找輸出程序| 套裝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
多線程循環(huán)輸出abcc++
總結(jié)
以上是生活随笔為你收集整理的多线程循环输出abcc++_C ++循环| 查找输出程序| 套装4的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 联想一体机电脑处理器i35005u和i3
- 下一篇: 孵化机多少钱一台啊?
