多线程循环输出abcc++
Program 1:
程序1:
#include <iostream>
int main()
{
int i;
for (i = NULL; i < 5; i++) {
std::cout << "Hello ";
}
return 0;
}
Output:
输出:
Hello Hello Hello Hello Hello
Explanation:
说明:
The above code will print "Hello " 5 times.
上面的代码将打印“ Hello” 5次。
Let's understand the program step by step.
让我们逐步了解程序。
In the above program, we declared an uninitialized variable i. And initialize variable i by NULL in the "for" loop. The value of NULL is 0. So the loop will run five times from 0 to 4, it means the condition will be valid up to 4.
在上面的程序中,我们声明了一个未初始化的变量i 。 并在“ for”循环中通过NULL初始化变量i 。 NULL的值为0。因此循环将在0到4之间运行五次,这意味着条件将一直有效到4。
Now, come to the below statement:
现在,进入以下语句:
std::cout<
In the above program, we did not include namespace std. so we have to use std with a scope resolution operator to access the cout object.
Program 2:
Output:
It may not print anything on the console screen.
Explanation:
The above code may not print anything, because, in the above code, we did not initialize variable i, then it will contain garbage value. So the loop will not execute, the condition will be false for the first time.
It may print something if the garbage value of i is less than 4.
Program 3:
Output:
On 32-bit system: Hello Hello Hello Hello
On 64-bit system: Hello Hello Hello Hello Hello Hello Hello Hello
Explanation:
We compiled my program on a 32-bit system, it printed "Hello " 4 times. .
In the above program, we declared a variable i with initial value 0 and we also declared a void pointer.
As we know that the size of any type of pointer is fixed, it depends on the architecture of the system, we are using a 32-bit system, so that size of the pointer will be 4. Then the loop will execute 4 times.
Recommended posts
翻译自: https://www.includehelp.com/cpp-tutorial/looping-find-output-programs-set-1.aspx
多线程循环输出abcc++